Nikolay Rumyantsev
Nikolay Rumyantsev

Reputation: 153

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]);
        }
    }

Upvotes: 5

Views: 1077

Answers (2)

Artur Alexeev
Artur Alexeev

Reputation: 338

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.

Upvotes: 1

Igor Ralic
Igor Ralic

Reputation: 15006

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

Upvotes: 11

Related Questions