2
votes

I am working on porting a Windows Phone 8 App to Windows Store 8.1 and I am confused about how page navigation works on Win 8.1. It seems that it is quite different to WP 8:

Given: PageA as main page and some other page PageB. Navigation: Launch the App, navigating forward to B and back to A

Windows Phone 8:

 - Constructor of PageA
 - PageA.OnNavigatedTo()...
 - PageA was just created? Ok, create ViewModel   

 - Click some Button to navigate to PageB
 - Creation of PageB + actual Navigation...  
 - Click some Button to navigate back to PageA

 - PageA.OnNavigatedTo()...
 - PageA was just created? No, use existing ViewModel

Windows Store App 8.1:

 - Constructor of PageA
 - PageA.OnNavigatedTo()...
 - PageA was just created? Ok, create ViewModel   

 - Click some Button to navigate to PageB
 - Creation of PageB + actual Navigation...  
 - Click some Button to navigate back to PageA

 - Constructor of PageA
 - PageA.OnNavigatedTo()...
 - PageA was just created? Ok, create ViewModel

The difference is in how the back navigation is handled. While on WP 8 simply the existing instance of PageA is used Win 8.1 creates a complete new instance of PageA. Thus the ViewModel has to be re-created as well...

It seems in Win 8.1 there is no difference in navigating from or to a page. Is this right? Or am I doing something wrong?

On some pages creating the view model is quite a lot of work. How could it make sense to throw away the existing PageA when navigating away to PageB and re-creating it when navigating back? Of course this saves memory, but if Windows Phone can handle different pages on stack Win 8.1 should be able to do the same, shouldn't it?

Of course the view model can be saved when navigating away from PageA and loaded instead of re-created when navigating back. But therefore the view model has to be serializable and the concreate ViewModel is quite complex.

Is there a way to keep Pages in memory?

1

1 Answers

5
votes

You can set NavigationCacheMode on a Page to Enabled or Required (by default it is Disabled):

public BasicPage1()
{
    this.InitializeComponent();

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

You can also set how many Pages you want to be cached for a Frame with Frame.CacheSize (10 by default).

msdn links:

http://msdn.microsoft.com/en-us/library/windows/apps/windows.ui.xaml.controls.page.navigationcachemode.aspx

http://msdn.microsoft.com/en-us/library/windows/apps/windows.ui.xaml.controls.frame.cachesize.aspx