简体   繁体   中英

Persist DataContext changes after back button in Win8 Metro app

I am building a Windows 8 app (C#/XAML) and using the Frame.Navigate() property to move between pages. Each page has an AppBar icon that will refresh that data on a page, and set the DataContext property. This works, and the UI updates accordingly after the button is pressed.

The problem I'm seeing shows up when I navigate to a different page, then click the back arrow to return to the previous page.

When the OnNavigatedTo(NavigationEventArgs e) method runs (after clicking the back arrow), the e.Parameter value is and old value (before I clicked the refresh button, and the DataContext was updated).

I don't know how to update the parameter value any other way than using Frame.Navigate(typeof(PageTypeName), paramValue); , but I don't want to initiate a navigation action.

My question is two fold.

  1. How can I persist DataContext changes so that when I return to pages, the value I've set is exposed via e.Parameter in the OnNavigatedTo(NavigationEventArgs e) method.

  2. Is there some kind of reference metrial that explains the Navigation lifecycle in Win8 Apps?

... or should I be doing this a different way?

I found the solution.

NavigationCacheMode = Windows.UI.Xaml.Navigation.NavigationCacheMode.Required;

This tells the frame that it should always cache the old instance of my page, and not create a new one when i navigate back to it.

This way my new DataContext value is not overwritten when I return to the page.

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