-1

I am making a app in wpf and i got some problem when navigate page0 to page1 then it lost properties data.like as i set "vh.Pageid=1;" but i get in text box "0". what should i do? 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 Answers1

1

In the file 'page0.xaml.cs' you are instanciating a 'Variableholder' object and you don't pass it as parameter. Note that in the file 'page1.xaml.cs' you are instanciating one more time a new 'Variableholder' object, instead of receive it from constructor or something like that.

Icaro Camelo
  • 382
  • 1
  • 10
  • This is correct, although I would also highly recommend the OP use WPF's binding sytem, or data will not be retained properly. WPF unloads objects that are not visible, so doing something like minimizing and restoring the application will clear any data entered unless it's bound to something in the DataContext. @ssararf, if you're interested I like to blog about beginner WPF concepts, and [this SO answer and associated links](http://stackoverflow.com/a/15684569/302677) may help you with understanding the overall WPF design. – Rachel Feb 03 '15 at 22:10
  • @camelo if i don't want to use parameter and use property to store data? is it work? – surajsararf Feb 05 '15 at 14:24
  • @ssararf If you want to create a property in the form 2, such as a string labled as Value, you could assign this property. It's going to work. – Icaro Camelo Feb 05 '15 at 15:44
  • @camelo i get some problem in property .i atteched my program.please correct me. [mywpf](https://drive.google.com/file/d/0B00D2H2ZkbK8MVgtQW1ZcG11ZW8/view?usp=sharing) – surajsararf Feb 05 '15 at 19:06