Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
361 views
in Technique[技术] by (71.8m points)

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

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:

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

The solution can be downloaded here: https://github.com/MyToolkit/MyToolkit/wiki/Paging-Overview

Update:

The page class now also provides the OnNavigatingFromAsync method to show for example an async popup and cancel navigation if required...


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...