Shi
Shi

Reputation: 79

Back navigation loads the page as its first state

I pass a value to a page

NavigationService.Navigate("/SportsPage.xaml?Number=5",UriKind.Relative);

the Number value changes while user is in the page, it gets 10 for example. then he goes to another page, but after a back navigation from that page to this page, the number is still 5. like it is the first time. but I want it to be the number when the user left the page (i.e 10).

I can save it to the storage and retrieve it on navigationTo, but it is not the case.

Is it possible to return back to the page in its last state?

Upvotes: 0

Views: 100

Answers (2)

Developer
Developer

Reputation: 4321

You can make a global variable in App.xaml.cs like so:

public int Number { get; set; }.

And manipulate with it using:

(App.Current as App).Number = 5;

Upvotes: 0

Kevin Gosse
Kevin Gosse

Reputation: 39007

When you press the back button, the page returns to its last state. The problem is that the OnNavigatedTo event is executed once more when going back to the page, so you must be careful not to overwrite the value of the variables.

Basically, your code is something like:

protected virtual void OnNavigatedTo(NavigationEventArgs e)
{
    this.Number = this.NavigationContext.QueryString["Number"];
}

You should change it to something like:

protected virtual void OnNavigatedTo(NavigationEventArgs e)
{
    if (e.NavigationMode != System.Windows.Navigation.NavigationMode.Back)
    {
         this.Number = this.NavigationContext.QueryString["Number"];
    }
}

This way, you won't be overwriting the last value of the variable when going back to the page.

Upvotes: 2

Related Questions