我正在创建一个Windows Universal 8.1应用程序。每次我导航到一个页面然后导航回然后再次返回页面时,页面的新实例将被保存在内存中。很明显,垃圾收集器会在一段时间后释放内存,但是如果不需要,我宁愿不使用内存。有没有办法回收或处理这些页面?
答案 0 :(得分:2)
在Windows Uriversal App中,我们可以将 NavigationCacheMode 用于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设置为已禁用。当 OnNavigatedFrom 时,页面的内存将发布。
有一个与SO类似的问题:Page constructor gets called again when navigating back in Windows 8 C# App