WinRT / UWP框架和页面缓存:如何在Navigate()上创建新的页面实例,并在GoBack()上保留页面实例 [英] WinRT/UWP Frame and Page caching: How to create new page instance on Navigate() and keep the page instance on GoBack()
问题描述
我正在尝试使用C#创建UWP(通用Windows应用)应用程序。我的问题是 Frame
控件:如果我在没有 NavigationCacheMode = Required
的情况下使用它,则每次用户返回时,页面未保存在内存中,将被重新创建。如果我将 NavigationCacheMode
设置为 Required
或 Enabled
,则返回可以正常工作(没有新的页面对象)但是,如果我从相同类型导航到另一个页面,则先前的页面对象将被回收并重新使用(没有新的页面实例)。
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).
所需行为:
是否可以通过以下方式实现以下行为:原始的 Frame
控件(如Windows Phone):
Is there a way to have the following behaviour with the original Frame
control (like in Windows Phone):
- 创建新的页面实例在
Navigate()
中 - 在
GoBack()
上保留页面实例/ li>
- Create new page instance on
Navigate()
- Keep the page instance on
GoBack()
我知道的唯一解决方案是创建自己的 Frame
控件,但这会导致其他问题(例如:缺少 SetNavigationState()
方法,等等...)
The only solution I know is to create an own Frame
control but this leads to other problems (e.g.: missing SetNavigationState()
method, etc...)
示例场景:
具有三个页面的简单应用程序示例: TvShowListPage
, TvShowDetailsPage
, SeasonDetailsPage
。
Simple application example with three pages: TvShowListPage
, TvShowDetailsPage
, SeasonDetailsPage
.
-
TvShowListPage
是输入页面。单击TvShow
后,导航至TvShowDetailsPage
。 - 现在在
TvShowDetailsPage
中,在列表中选择一个季节,然后导航至TvShowDetailsPage
。 - 如果向后浏览,则页面应保留在内存中,以避免重新加载页面。
- 但是,如果用户返回
TvShowListPage
并选择另一个TvShow
,TvShowDetailsPage
被回收并且可能处于错误状态(例如,显示演员表关键点,而不是第一个季节关键点)
TvShowListPage
is the entry page. After clicking on aTvShow
navigate toTvShowDetailsPage
.- Now in
TvShowDetailsPage
select a season in the list and navigate to theTvShowDetailsPage
. - If navigating back, the pages should stay in memory to avoid reloading the pages.
- But if the users goes back to
TvShowListPage
and selects anotherTvShow
theTvShowDetailsPage
gets recycled and is maybe in the wrong state (eg showing the cast pivot instead of the first, seasons pivot)
我正在寻找Windows Phone 7的默认行为:导航在页面堆栈上创建一个新页面,后退会从堆栈中删除首页,并显示堆栈中的上一页(存储在内存中)。
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).
解决方案:
由于无法解决此问题,重新实现所有与页面相关的类:Page,Frame,SuspensionManager等...
Because there was no solution to this problem, I had to reimplement all paging relevant classes: Page, Frame, SuspensionManager, etc...
提供所有这些类的库MyToolkit 可以下载此处: https://github.com/MyToolkit/MyToolkit/wiki/Paging-Overview
The library MyToolkit which provides all these classes can be downloaded here: https://github.com/MyToolkit/MyToolkit/wiki/Paging-Overview
参考文献:
- http://www.jayway.com/2012/ 05/25 / clearing-the-windows-8-page-cache / :没有好的解决方案
- http://social.msdn.microsoft.com/Forums/en-US/winappswithcsharp/thread / 88e6d1b3-1fa6-4ab4-a816-e77c86ef236f / :实施设置自己的Frame类不是解决方案,因为它与
SuspensionManager
- http://www.jayway.com/2012/05/25/clearing-the-windows-8-page-cache/: No good solution
- http://social.msdn.microsoft.com/Forums/en-US/winappswithcsharp/thread/88e6d1b3-1fa6-4ab4-a816-e77c86ef236f/: Implementing of an own Frame class is no solution as it doesn't work with
SuspensionManager
推荐答案
由于没有解决此问题的方法,我不得不重新实现所有与页面调度相关的类:Page,Frame,SuspensionManager等...
Because there was no solution to this problem, I had to reimplement all paging relevant classes: Page, Frame, SuspensionManager, etc...
解决方案可在此处下载:
https://github.com/MyToolkit / MyToolkit / wiki /分页概述
The solution can be downloaded here: https://github.com/MyToolkit/MyToolkit/wiki/Paging-Overview
更新:
页面类现在还提供了 OnNavigatingFromAsync 方法,以显示例如异步弹出窗口,并在需要时取消导航...
The page class now also provides the OnNavigatingFromAsync method to show for example an async popup and cancel navigation if required...
这篇关于WinRT / UWP框架和页面缓存:如何在Navigate()上创建新的页面实例,并在GoBack()上保留页面实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!