简体   繁体   中英

why label changed only once

In my wpf app there are two pages, page1 an page2.

page1 looks something like:

public static int someVar;
public Page1()
{
  InitializeComponent();
}
.
.
.
// the user gives some value to "someVar"
    Page2 p2 = new Page2();
    void Next_btn(object sender , RoutedEventArgs e)
    {

        if (this.NavigationService.CanGoForward)
        {              
            p2.ChangLabel();
            this.NavigationService.GoForward();
        } else
            NavigationService.Navigate(new Page2());
    }

Page2 looks like:

public Page2()
{
   InitializeComponent();
   ChangeLable();
}
public void ChangeLable()
{
 MessageBox.Show("I'm here");

 if(Page1.someVar==1)
   myLabel.Content = "blabla";
 else
   myLabel.Content = "bbbbbb";
}
.
.
.
void Back_btn(object sender , RoutedEventArgs e)
{
     if (this.NavigationService.CanGoBack)
        this.NavigationService.GoBack();
}  

let's say that the user done the following:

the user have been in page1 and puts someVar=1 and then goes to page2 . therefore the function ChangeLabel() would change the lable to blabla . after that he went back to page1 and changed someVar=3 and then navigated again to page2 the problem in this scenario is that the label doesn't change to bbbbb even though the message box is showen

how to solve the following scenario?

I mean why in the second time the label does't changed to bbbb

You create two instances of Page2 . You should pass p2 to NavigationService.Navigate instead of creating a new one:

Page2 p2 = new Page2();
void Next_btn(object sender, RoutedEventArgs e)
{

    if (this.NavigationService.CanGoForward)
    {
        p2.ChangLabel();
        this.NavigationService.GoForward();
    }
    else
        NavigationService.Navigate(p2);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM