How to cache a page in Windows Phone 8.1

北城以北 提交于 2019-12-19 08:21:12

问题


Previously in Windows Phone 8.0 apps, we could navigate deeper to the same page this way:

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:

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).


回答1:


In your page constructor you'll have to specify

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



回答2:


In App.cs you can set RootFrame.CacheSize which hints the OS how many pages it should try to keep in cache. 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.



来源:https://stackoverflow.com/questions/23426765/how-to-cache-a-page-in-windows-phone-8-1

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!