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()(WinRT/UWP 帧和页面缓存:如何在 Navigate() 上创建新页面实例并将页面实例保留在 GoBack() 上)
本文介绍了WinRT/UWP 帧和页面缓存:如何在 Navigate() 上创建新页面实例并将页面实例保留在 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:

  • 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

解决方案

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...

这篇关于WinRT/UWP 帧和页面缓存:如何在 Navigate() 上创建新页面实例并将页面实例保留在 GoBack() 上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

DispatcherQueue null when trying to update Ui property in ViewModel(尝试更新ViewModel中的Ui属性时DispatcherQueue为空)
c# Generic Setlt;Tgt; implementation to access objects by type(按类型访问对象的C#泛型集实现)
InvalidOperationException When using Context Injection in ASP.Net Core(在ASP.NET核心中使用上下文注入时发生InvalidOperationException)
quot;Overflowquot; compiler error with -9223372036854775808L(编译器错误-9223372036854775808L(Q;溢出Q))
Visual Studio 2010 ReportViewer Assembly References(Visual Studio 2010 ReportViewer程序集引用)
Weird behaviour when I open a reportviewer in WPF(在WPF中打开报表查看器时出现奇怪的行为)