导航页面时数据丢失

data lost when navigate page

我正在 wpf 中制作一个应用程序,当我将第 0 页导航到第 1 页时遇到了一些问题,然后它丢失了属性 data.like,因为我设置 "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>

Variableholder.cs

class Variableholder
{
    public int Pageid { get; set; }
            }

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));
            }
    }

page1.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();
        }
    }

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