locked
Save Page State Problem

    Question

  • Hello,

    my App has 3 Pages. I want to Cache the Data of the Second Page like that:

    Frame.Navigate(typeof(SecondPage));

    I want to save the State of the Second Page:

    this.NavigationCacheMode = NavigationCacheMode.Required;
    
    then Navigate to third Page:
    Frame.Navigate(typeof(ThirdPage));

    After I finished my Work on the Third Page i want to GoBack to Second Page:

    Frame.GoBack();

    Now, what happens is that it just builds the Second Page twice. One SecondPage with the State i've saved and another completly new SecondPage above the initial. If i Navigate to the ThirdPage again it will just render another SecondPage so i do have 3 Second Pages.

    What the hell is wrong with my Code?


    • Edited by B.Ke Wednesday, October 15, 2014 7:13 AM
    Tuesday, October 14, 2014 1:49 PM

Answers

  • Hi B,   This is because the sample has a bug.  Everytime SecondPage.OnNavigatedTo is called it registers a new ScriptNotify handler.   The ThirdPage is being navigated to from this handler which is called twice in your scenario for the repro.  

    To resolve the issue,  in the OnNavigatedFrom method unsubscribe the event handler. 

            protected override void OnNavigatedFrom(NavigationEventArgs e)
            {
                MyWebView.ScriptNotify -= Handle_ScriptNotify;
                base.OnNavigatedFrom(e);
            }

    Now your Navigation should work as expected.


    Bret Bentzinger (MSFT) @awehellyeah


    Thursday, October 16, 2014 4:46 PM
    Moderator
  • Holy... such a obvious mistake. Thanks for your patience and your help :)

    But still doesn't save the Page State does it?

    It doesn't save the Page State because it goes into OnNavigatedTo and my WebView

    navigates to "ms-appx-web:///WebViewElements/index.html"

    again. 

    I just have to ask:

    var source = MyWebView.Source;
    if(source == null)
        MyWebView.Navigate(new Uri("ms-appx-web:///"+e.Parameter));

    • Edited by B.Ke Friday, October 17, 2014 7:02 AM
    • Marked as answer by B.Ke Friday, October 17, 2014 7:03 AM
    Friday, October 17, 2014 6:52 AM

All replies

  • Hard to tell from your description.  First, why does your app have 3 frames?  it should only have one.

    Second put a breakpoint on the constructor for SecondPage.  This should only ever be hit once due to the NavigateCachemode.Required.  If not, look at the callstack to see why it is being constructed multiple times.  if you are creating it outside of Frame.Navigate method then that would explain that.

    Lastly, look into your NavigatedFrom, and NavigatedTo events on your SecondPage.   This is where you save page state, and restore page state respectively. 


    Bret Bentzinger (MSFT) @awehellyeah

    Tuesday, October 14, 2014 10:23 PM
    Moderator
  • My Fault. Of course i meant 3 Pages not 3 Frames.

    I've just written a Show Case App.

    My Show Case App

    I think i do have a problem to understand the NavigationCache. 

    For me it is like that:

    Frame.Navigate(typeof(ThirdPage));

    Always creates a new ThirdPage. 

    this.NavigationCacheMode = Windows.UI.Xaml.Navigation.NavigationCacheMode.Disabled;

    Doesn't save the Page inside of the Cache. So it should be "thrown away" after i do

    Frame.GoBack();

    And Frame.GoBack() should open up the SecondPage as it was before, because of

    this.NavigationCacheMode = Windows.UI.Xaml.Navigation.NavigationCacheMode.Required;

    But currently it does open up a new SecondPage over the old SecondPage, so i have 2 SecondPages.

    When i do Frame.Navigate(typeof(ThirdPage)); it invokes the Navigate Method 2 Times and ThirdPage comes up 2 Times.

    FIRST EDIT:

    Like i said. I just don't understand the Navigation Handling.

    Show case Apps really help a lot!

    Now, it seems like Frame.GoBack() doesn't only Suspend the Current Page, when you set NavigationCacheMode.Required, like i thought before. Of course it goes into OnNavigatedTo and Builds the whole Xaml Content again. It then overlaps your Cached Page.

    I just have to ask if there is already Content loaded.

    SECOND EDIT:

    Still not the Solution.

    When i want to GoBack to first Page i don't want my Second Page to be in the cache anymore.

    My workaround:

    Frame.CacheSize = 1;

    • Edited by B.Ke Wednesday, October 15, 2014 2:18 PM
    Wednesday, October 15, 2014 9:13 AM
  • Not sure I am following what you are asking anymore. Here is a quickstart on Page navigation:
    http://msdn.microsoft.com/en-us/library/windows/apps/xaml/hh771188.aspx

    And more information on how it works:
    http://msdn.microsoft.com/en-us/library/windows/apps/xaml/hh757631.aspx

    http://msdn.microsoft.com/en-us/library/windows/apps/xaml/jj215600.aspx

    GoBack() does not delete the page.  Because you can call GoForward().   Do all of the page initialization in the OnNavigatedTo().


    Bret Bentzinger (MSFT) @awehellyeah

    Wednesday, October 15, 2014 5:44 PM
    Moderator
  • you just have to install the Show Case App on your Windows Phone and try it yourself.

    If you go from Second Page to Third Page and then back to Second Page and again to Third Page, it builds Third Page 2 times.

    Well, i think i solved my problem with Cache Size...

    Thursday, October 16, 2014 7:21 AM
  • Hi B,   This is because the sample has a bug.  Everytime SecondPage.OnNavigatedTo is called it registers a new ScriptNotify handler.   The ThirdPage is being navigated to from this handler which is called twice in your scenario for the repro.  

    To resolve the issue,  in the OnNavigatedFrom method unsubscribe the event handler. 

            protected override void OnNavigatedFrom(NavigationEventArgs e)
            {
                MyWebView.ScriptNotify -= Handle_ScriptNotify;
                base.OnNavigatedFrom(e);
            }

    Now your Navigation should work as expected.


    Bret Bentzinger (MSFT) @awehellyeah


    Thursday, October 16, 2014 4:46 PM
    Moderator
  • Holy... such a obvious mistake. Thanks for your patience and your help :)

    But still doesn't save the Page State does it?

    It doesn't save the Page State because it goes into OnNavigatedTo and my WebView

    navigates to "ms-appx-web:///WebViewElements/index.html"

    again. 

    I just have to ask:

    var source = MyWebView.Source;
    if(source == null)
        MyWebView.Navigate(new Uri("ms-appx-web:///"+e.Parameter));

    • Edited by B.Ke Friday, October 17, 2014 7:02 AM
    • Marked as answer by B.Ke Friday, October 17, 2014 7:03 AM
    Friday, October 17, 2014 6:52 AM