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()
我知道的唯一解决方案是创建自己的 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...
提供所有这些类的 library 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, etc...
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/Paging-Overview
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屋!