locked
Basic Lifecycle and saving state question

    Question

  • I have a question about saving the state on a page.  When the application enters a page, the navigationHelper_LoadState method is called.  The LoadStateEventArgs contain navigationParameter that is used to initialize the view model.

    Do I need to save those parameters to a RoamingProfile session variable?  How does that work?  The documentation states that on entering the LoadState method I need to check the page state, then
    if (e.PageState != null)
    {
     // get e.PageState["key"] and set to whatever UI element
    }
    //  then resore the values in the app data.
    Windows.Storage.ApplicationDataContainer roamingSettings =
                    Windows.Storage.ApplicationData.Current.RoamingSettings;
    if (roamingSettings.Values.ContainsKey("userName"))
    {
     nameInput.Text = roamingSettings.Values["userName"].ToString();
    }

    The above restores the user name.  Thats fine, but the page needs the navigation parameters to restore itself.  Are they preserved in the e.NavigationParamter automatically for me? or do I need to retieve them again from the roamingSettings?

    How does one distinguish between initializing the page based on the e.navigationParameters and from the roamingSettings?
    The examples that I have seen sets the roaming settings outside of the e.PageState != null) {} statement.

    Should it be inside the statement?  Like
    if (e.PageState != null)
    {
     //restore from pageState and roamingSettings
    }
    else
    {
     // this is first time so just use the e.NavigationParameters
    }

    Thanks for your help!

    Robotuner

    Friday, March 21, 2014 3:59 AM

All replies

  • Hi Robotuner - what's your end goal here?

    Matt Small - Microsoft Escalation Engineer - Forum Moderator
    If my reply answers your question, please mark this post as answered.

    NOTE: If I ask for code, please provide something that I can drop directly into a project and run (including XAML), or an actual application project. I'm trying to help a lot of people, so I don't have time to figure out weird snippets with undefined objects and unknown namespaces.

    Friday, March 21, 2014 2:16 PM
    Moderator
  • End goal? To understand how to properly suspend and restore the application under the various scenarios. 

    I"m just trying to figure out how the design pattern is supposed to be used.


    Robotuner

    Friday, March 21, 2014 2:55 PM