简体   繁体   English

如何在Windows Phone 8.1中缓存页面

[英]How to cache a page in Windows Phone 8.1

Previously in Windows Phone 8.0 apps, we could navigate deeper to the same page this way: 以前在Windows Phone 8.0应用程序中,我们可以通过这种方式更深入地浏览同一页面:

NavigationService.Navigate(new Uri("/SamePage.xaml", UriKind.Relative));

Page was cached automatically, so after navigating back, user was on the same position on the list when he left. 页面自动缓存,因此在导航回来后,用户离开时列表上的位置相同。

But in Windows Phone Store Apps we navigate deeper to the same page this way: 但在Windows Phone Store Apps我们通过这种方式更深入地浏览同一页面:

Frame.Navigate(typeof(SamePage), id);

But after navigating back it loads data again so if user was on the middle of a long list, now he is on the top: 但是在导航回来之后它会再次加载数据,所以如果用户位于长列表的中间位置,那么现在他位于顶部:

private async void NavigationHelper_LoadState(object sender, LoadStateEventArgs e)
{
    // TODO: Create an appropriate data model for your problem domain to replace the sample data.
    var group = await SampleDataSource.GetGroupAsync((string)e.NavigationParameter);
    this.DefaultViewModel["Group"] = group;
}

How can I cache a page like the way it was done previously so user will be on the same position on a list where he left? 如何像以前那样缓存页面,这样用户将在他离开的列表中的相同位置?

(I included Windows apps too cause they are familiar with it from a longer time). (我也包括Windows应用程序,因为他们从较长时间就熟悉它)。

In your page constructor you'll have to specify 在您的页面构造函数中,您必须指定

    public MainPage()
    {
       this.InitializeComponent();
       this.NavigationCacheMode = NavigationCacheMode.Required;
    }

In App.cs you can set RootFrame.CacheSize which hints the OS how many pages it should try to keep in cache. 在App.cs中,您可以设置RootFrame.CacheSize,它提示操作系统应该尝试在缓存中保留多少页面。 Also you probably shouldn't reset the datacontext in NavigationHelper_LoadState - this method is called each time you navigate to the page, even if you navigate back. 此外,您可能不应在NavigationHelper_LoadState中重置datacontext - 每次导航到该页面时都会调用此方法,即使您向后导航也是如此。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM