导航页面时数据丢失

时间:2015-02-03 19:16:07

标签: 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="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();
        }
    }

1 个答案:

答案 0 :(得分:1)

在文件&#39; page0.xaml.cs&#39;你正在实现一个可变持有者&#39;对象,你不能将它作为参数传递。 请注意,在文件&#39; page1.xaml.cs&#39;你正在再次实现一个新的可变持有者&#39;对象,而不是从构造函数或类似的东西接收它。