WinRT/UWP Frame and Page caching: How to create new page instance on Navigate() and keep the page instance on GoBack()

Viewed 17092

I'm trying to create an UWP (Universal Windows App) application with C#. My problem is the Frame control: If I use it without NavigationCacheMode = Required, every time the user goes back, the page is not kept in memory and will be recreated. If I set NavigationCacheMode to Required or Enabled, going back works correctly (no new page object) but if I navigate to another page from the same type, the previous page object is recycled and reused (no new page instance).

Desired behavior:

Is there a way to have the following behaviour with the original Frame control (like in Windows Phone):

  1. Create new page instance on Navigate()
  2. Keep the page instance on GoBack()

The only solution I know is to create an own Frame control but this leads to other problems (e.g.: missing SetNavigationState() method, etc...)

Sample scenario:

Simple application example with three pages: TvShowListPage, TvShowDetailsPage, SeasonDetailsPage.

  1. TvShowListPage is the entry page. After clicking on a TvShow navigate to TvShowDetailsPage.
  2. Now in TvShowDetailsPage select a season in the list and navigate to the TvShowDetailsPage.
  3. If navigating back, the pages should stay in memory to avoid reloading the pages.
  4. But if the users goes back to TvShowListPage and selects another TvShow the TvShowDetailsPage gets recycled and is maybe in the wrong state (eg showing the cast pivot instead of the first, seasons pivot)

I'm looking for the default Windows Phone 7 behavior: Navigating creates a new page on the page stack, going back removes the top page from the stack and displays the previous page from the stack (stored in the memory).

Solution:

Because there was no solution to this problem, I had to reimplement all paging relevant classes: Page, Frame, SuspensionManager, etc...

The library MyToolkit which provides all these classes can be downloaded here: https://github.com/MyToolkit/MyToolkit/wiki/Paging-Overview

References:

7 Answers

I achieved this by:

  • Setting NavigationCacheMode to Required/Enabled for required Pages.
  • On clicking on Page2 button/link:

    Traverse the Frame BackStack and find out if the Page2 is in BackStack. If Page2 is found call Frame.GoBack() required number of times. If not found just navigate to the new Page. This will work for any no of pages.

Code Sample:

public void Page2Clicked(object sender, RoutedEventArgs e)
{
int isPresent = 0;
int frameCount = 0;
//traverse BackStack in reverse order as the last element is latest page
for(int index= Frame.BackStack.Count-1; index>=0;index--)
{
    frameCount += 1;
    //lets say the first page name is page1 which is cached
    if ("Page2".Equals(Frame.BackStack[index].SourcePageType.Name))
    {
        isPresent = 1;
        //Go back required no of times
        while (frameCount >0)
        {
            Frame.GoBack();
            frameCount -= 1;
        }
        break;
    }

}
    if (isPresent == 0)
    {
    Frame.Content = null;
    Frame.Navigate(typeof(Page2));
    }
}

This will be helpful if forward/backward buttons will not be made much use of. as this solution will affect the forward/backward navigation. If you wish to use forward/backward navigation as well, then some additional cases has to be handled.

When NavigationCacheMode is enabled, you can call Dispose() (conditionally) in OnNavigatedFrom() to make sure that a new instance of the page will be created the next time the application navigates to it.

Related