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()

查看:179
本文介绍了WinRT / UWP框架和页面缓存:如何在Navigate()上创建新的页面实例,并在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):


  1. 创建新的页面实例在 Navigate()

  2. GoBack()上保留页面实例/ li>
  1. Create new page instance on Navigate()
  2. 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.


  1. TvShowListPage 是输入页面。单击 TvShow 后,导航至 TvShowDetailsPage

  2. 现在在 TvShowDetailsPage 中,在列表中选择一个季节,然后导航至 TvShowDetailsPage

  3. 如果向后浏览,则页面应保留在内存中,以避免重新加载页面。

  4. 但是,如果用户返回 TvShowListPage 并选择另一个 TvShow TvShowDetailsPage 被回收并且可能处于错误状态(例如,显示演员表关键点,而不是第一个季节关键点)

  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)

我正在寻找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/: 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆