Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Save scroll state in Windows Phone 8.1 when user navigate between pages

I navigate between pages with Navigation Helper class which VS 2013 added when solution created, But scroll state most of controls (Like Pivot, Hub) does not saved like in Windows Phone 8.x Silverlight.

What should I do for implement this behaviour? Should I handle scrolling state by myself and restore scroll when i go back in visited page?

Thanks.

UPDATE1:

I need save selected pivot/hub item etc, when i go back to page.

UPDATE2:

    void navigationHelper_SaveState(obj sender,SaveStateEventArgs e)
    {
    e.PageState["SelectedSection"] = MainHub.SectionsInView;
    }
    void navigationHelper_LoadState(obj sender,LoadStateEventArgs e)        
    {
        if (e.PageState != null)
        {
            var sections = e.PageState["SelectedSection"] as IList<HubSection>;
            if (sections != null && sections.Any())
                MainHub.ScrollToSection(sections[0]);
        }
    }
like image 838
Nikolay Rumyantsev Avatar asked May 24 '14 14:05

Nikolay Rumyantsev


2 Answers

On the page where you use the hub, set the navigation cache mode in constructor:

this.NavigationCacheMode = NavigationCacheMode.Enabled;

or in XAML:

<Page
    x:Class="App.HubPage"
    ....
    xmlns:data="using:App.Data"
    NavigationCacheMode="Enabled"
    ....
like image 142
Igor Ralic Avatar answered Sep 22 '22 12:09

Igor Ralic


Better use:

this.NavigationCacheMode = NavigationCacheMode.Required;

before:

this.InitializeComponent();

And add:

protected override void OnNavigatedFrom(NavigationEventArgs e)
{
    if (e.NavigationMode == NavigationMode.Back)
    this.NavigationCacheMode = NavigationCacheMode.Disabled;
}

to remove cache on back navigation from page.

like image 22
Artur Alexeev Avatar answered Sep 21 '22 12:09

Artur Alexeev