我正在创建一个 Windows Universal 8.1 应用程序。每次我导航到一个页面,然后导航回来,然后再次导航到该页面,该页面的新实例就会保存在内存中。显然,垃圾收集器会在一段时间后释放内存,但是如果不需要,我宁愿不使用内存。有没有办法回收或处理这些页面?
在Windows Uriversal App中,我们可以使用导航缓存模式 to recycle
页面。它可以在页面的构造函数中设置。比如有一个我们要回收的MainPage:
public MainPage()
{
this.InitializeComponent();
// Set the NavigationCacheMode of Page to Enabled.
// The page is cached, but the cached instance is discarded when the size
// of the cache for the frame is exceeded.
this.NavigationCacheMode = Windows.UI.Xaml.Navigation.NavigationCacheMode.Enabled;
// OR Set the NavigationCacheMode of Page to Required.
// The page is cached and the cached instance is reused for every visit
// regardless of the cache size for the frame.
// this.NavigationCacheMode = Windows.UI.Xaml.Navigation.NavigationCacheMode.Required;
}
设置完成后,我们可以返回MainPage,无需重新创建它。
如果 NavigationCacheMode 设置为Disabled。页的内存将是released when 导航自从中。
有一个与SO类似的问题:返回 Windows 8 C# 应用程序时,页面构造函数再次被调用 https://stackoverflow.com/questions/12633758/page-constructor-gets-called-again-when-navigating-back-in-windows-8-c-sharp-app
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)