Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 导航页面时数据丢失_C#_.net_Wpf - Fatal编程技术网

C# 导航页面时数据丢失

C# 导航页面时数据丢失,c#,.net,wpf,C#,.net,Wpf,我正在wpf中制作一个应用程序,在将page0导航到page1时遇到一些问题,然后它丢失了属性数据。就像我设置的“vh.Pageid=1”;但我在文本框“0”中遇到了问题。 我该怎么办? mainwindow.xaml <Window x:Class="Pagenavigate.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="

我正在wpf中制作一个应用程序,在将page0导航到page1时遇到一些问题,然后它丢失了属性数据。就像我设置的“vh.Pageid=1”;但我在文本框“0”中遇到了问题。 我该怎么办? mainwindow.xaml

    <Window x:Class="Pagenavigate.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" MinHeight="350" MinWidth="525"
           Background="Transparent">
<Grid>
<Frame Source="pages/page0.xaml" NavigationUIVisibility="Hidden" Name="Mainframe" Margin="0,35,0,25"></Frame>
</Grid>
page0.xaml

<Page x:Class="Pagenavigate.Pages.Browse"
          xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
          xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
          xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
          xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
          mc:Ignorable="d" 
          d:DesignHeight="300" d:DesignWidth="300"
        Title="Browse">

        <Grid>
        <Button Grid.Column="0"  Content="btn" Style="{StaticResource Buttonred}"  Name="btn" Click="btn_Click"/>
    </Grid>
    </Page>

page0.xaml.cs

namespace GaanazoneID3.Pages
{
    /// <summary>
    /// Interaction logic for Browse.xaml
    /// </summary>
    public partial class Page1 : Page
    {
        Variableholder vh = new Variableholder();
        public Browse()
        {
            InitializeComponent();
        }
private void Browsebtn_Click(object sender, RoutedEventArgs e)
    {
                Variableholder vh = new Variableholder();
                vh.Pageid = 1;
                Mainframe.NavigationService.Navigate(new Uri("pages/page1.xaml", UriKind.Relative));
            }
    }
名称空间GaanazoneID3.Pages
{
/// 
///Browse.xaml的交互逻辑
/// 
公共部分类第1页:第页
{
Variableholder vh=新的Variableholder();
公众浏览()
{
初始化组件();
}
私有无效浏览器单击(对象发送方,路由目标)
{
Variableholder vh=新的Variableholder();
vh.Pageid=1;
Mainframe.NavigationService.Navigate(新Uri(“pages/page1.xaml”,UriKind.Relative));
}
}
第1.xaml页

<Page x:Class="Pagenavigate.Pages.Browse"
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
      xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
      mc:Ignorable="d" 
      d:DesignHeight="300" d:DesignWidth="300"
    Title="Browse">
    <Grid>
    <TextBox Grid.Column="1" Margin="0,15,0,15" MaxHeight="70" Name="txtbx" FontSize="20"/>
</Grid>
</Page>

page1.xaml.cs

using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace GaanazoneID3.Pages
{
    /// <summary>
    /// Interaction logic for Browse.xaml
    /// </summary>
    public partial class Page1 : Page
    {
        Variableholder vh = new Variableholder();
        public Browse()
        {
            InitializeComponent();
            txtbx.Text = vh.Pageid.ToString();
        }
    }
使用System.Text;
使用System.Threading.Tasks;
使用System.Windows;
使用System.Windows.Controls;
使用System.Windows.Data;
使用System.Windows.Documents;
使用System.Windows.Input;
使用System.Windows.Media;
使用System.Windows.Media.Imaging;
使用System.Windows.Navigation;
使用System.Windows.Shapes;
名称空间GaanazoneID3.Pages
{
/// 
///Browse.xaml的交互逻辑
/// 
公共部分类第1页:第页
{
Variableholder vh=新的Variableholder();
公众浏览()
{
初始化组件();
txtbx.Text=vh.Pageid.ToString();
}
}

在文件“page0.xaml.cs”中,您正在实例化一个“Variableholder”对象,而不是将其作为参数传递。
请注意,在文件“page1.xaml.cs”中,您将再次实例化一个新的“Variableholder”对象,而不是从构造函数或类似的东西接收它。

这是正确的,尽管我也强烈建议OP使用WPF的绑定系统,否则数据将无法正确保留。WPF卸载不可见的对象,这样做最小化和还原应用程序之类的操作将清除输入的任何数据,除非它绑定到DataContext中的某个内容。@ssararf,如果您感兴趣,我想在博客中介绍WPF的初学者概念,并可能帮助您理解WPF的总体设计。@camelo如果我不想使用参数和属性来存储数据,是吗工作?@ssararf如果您想创建一个形式为2的属性,例如标记为值的字符串,您可以分配这个属性。它会工作的。@camelo我在属性中遇到一些问题。我尝试了我的程序。请纠正我。
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace GaanazoneID3.Pages
{
    /// <summary>
    /// Interaction logic for Browse.xaml
    /// </summary>
    public partial class Page1 : Page
    {
        Variableholder vh = new Variableholder();
        public Browse()
        {
            InitializeComponent();
            txtbx.Text = vh.Pageid.ToString();
        }
    }