如何在导航过程中保持内容的状态? [英] How do I keep the status of content during the navigation?
问题描述
我在 MainPage
中有一个 Frame
,MainPage
中有内容,例如,一个 List
由MainPage
的Loaded
事件调用的函数(我们称之为loadList()
)动态生成.
I have a Frame
in MainPage
, and there's content in MainPage
, for example, a List
which is dynamically generated by a function (let's call it loadList()
) which is invoked by the Loaded
event of MainPage
.
在用户点击 List
中的任何项目后,Frame
可以导航到另一个页面(我们称之为 DetailPage
).每次我从 DetailPage
导航回 MainPage
时,MainPage
都会调用 Loaded
事件,因此会调用 <再次代码>loadList().
After user clics on any items in the List
, The Frame
could be navigated to another page (lets call this DetailPage
). Every time I navigate back to the MainPage
from DetailPage
, the MainPage
invokes the Loaded
event, therefore invokes the loadList()
again.
我的问题是,有没有什么办法可以保持内容的状态,并且一旦生成就不会再次生成?
My question is, that, is there any way I can keep the status of the content and keep it from being generated again once it is generated?
推荐答案
默认情况下,UWP 页面在用户从页面导航时被卸载.这很有用,因为它可以节省内存.当用户返回页面时,页面会再次加载.
By default in UWP pages are unloaded when the user navigates from them. This is useful because it conserves memory. When the user returns to the page, the page is loaded again.
您可以通过设置 NavigationCacheMode
属性在您的页面上:
You can change this behavior by setting the NavigationCacheMode
propertyon your page:
<Page
...
NavigationCacheMode="Required">
如果您将 NavigationCacheMode
设置为 Enabled
或 Required
,页面将被缓存,并在用户导航回该页面时恢复.页面的 Loaded
事件仍将被调用,但您可以通过直接检查控件或检查 bool
标志变量来检查数据是否已经初始化初始化后设置:
If you set NavigationCacheMode
to Enabled
or Required
, the page will be cached and will be restored when user navigates back to it. The page's Loaded
event will still be called, but you can just check if the data is already initialized or not either by checking the control directly or by checking a bool
flag variable you set after initialization:
private void MainPage_OnLoaded(object sender, RoutedEventArgs e)
{
if (!initialized)
{
loadList();
initialized = true;
}
}
Enabled
和 Required
之间的区别在于 Enabled
遵守 Frame<上的
CacheSize
设置/代码>.一旦存储的缓存页面超过限制,Frame
将自动从内存中删除最旧的页面.必需
页面不计入此限制,并且永远不会被删除.
Difference between Enabled
and Required
is that Enabled
honors the CacheSize
setting on your Frame
. Once there is more cached pages stored than the limit, the Frame
will automatically remove the oldest one from memory. Required
pages however don't count towards this limit and are never removed.
此解决方案的一个更好的替代方案是使用 MVVM 设计模式.在这种模式中,视图的数据存储在 ViewModel
类中,许多 MVVM 框架在内存中为导航堆栈中的页面维护 ViewModel
.这意味着您的页面将被重新加载,但数据在视图模型中已经可用,因此加载速度会很快.MVVM 是开发 UWP 应用程序的推荐模式,因此我强烈建议您尝试 Prism、
A slightly better alternative to this solution would be to use the MVVM design pattern. In this pattern data for your views are stored inside ViewModel
classes and many MVVM frameworks maintain the ViewModels
for the pages in your navigation stack in memory. This means that your page will be reloaded, but the data will be already available in the view model, so it will load fast. MVVM is the recommended pattern for developing UWP apps so I highly recommend you to try out either Prism, MvvmLight or MvvmCross.
这篇关于如何在导航过程中保持内容的状态?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!