Save the page state when navigating out

2019-07-20 15:12发布

问题:

Say I have two pages, A and B.

The user can modify things on page A then navigate to page B. When he is on page B, he clicks the "Back" button to go to page A. Everything that has been done previously is lost.

There is a way to get the exact same state by using

this.NavigationCacheMode = 
        Windows.UI.Xaml.Navigation.NavigationCacheMode.Enabled;

But is there a way to know whether the page is opened for the first time a by using the back button?

回答1:

yes it is:

protected override void OnNavigatedTo(NavigationEventArgs e)
{
  if (e.NavigationMode == NavigationMode.Back)
  {
    // LoadPreviousSate...
  }
}

You also should've a look at ApplicationExecutionState (in OnLaunched event in App.xaml). If you navigate to Page B, Apps suspends, App continues, user navigates to Page A the NavigationMode will be New!