在 Windows 8 C# 应用程序中导航回时再次调用页面构造函数 [英] Page constructor gets called again when navigating back in Windows 8 C# App
问题描述
我发现在 Windows 8 应用程序中,导航回此页面时,页面的构造函数总是会再次被调用,但在 Windows Phone 7 应用程序中并非如此.
I have found that in Windows 8 apps, a Page's constructor always gets called again when navigating back to this page, however this is not the case in Windows Phone 7 apps.
由于这种行为,该页面的所有属性都会丢失,您需要以某种方式存储它们并在页面重新激活后重新分配它们.目前我正在使用 SaveState
方法来保存数据并在 LoadState
方法中重新分配数据.这两种方法都内置在示例 Grid/Split App 中.
Because of this behavior all the properties of that page get lost and you need to somehow store them and reassign them after the page is reactivated. At the moment I am using the SaveState
method to save the data and reassign the data in LoadState
method. Both of the methods are built-in in the sample Grid/Split App.
我想知道为什么它是这样设计的,是否有更好的方法来维护页面的实例?
I wonder why it's designed this way and if there's any better way to maintain the instance of the page?
推荐答案
您可以为每个要保留在内存中的页面设置 Page.NavigationCacheMode = NavigationCacheMode.Required,但请注意,您必须确保用户无法导航无休止地浏览这些页面,因为这会泄漏内存.
You can set Page.NavigationCacheMode = NavigationCacheMode.Required for each page you want to keep in memory, but note that you have to make sure users can't navigate through these pages endlessly as that would leak memory.
这篇关于在 Windows 8 C# 应用程序中导航回时再次调用页面构造函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!