Search code examples
windows-runtimemicrosoft-metroc#-5.0winrt-async

Metro App SuspensionManager Override SaveState Behaviour


How do I modify the SuspensionManager object in a Metro App, to SaveState for the MainPage only and discard State and Navigation for any child pages?

E.g. I have a MainPage that allows you to Navigate to a ChildPage. If the Metro App is Closed or Suspended, I want the ChildPage to override the MainPage state values.

Now, the next time the user opens the app, the MainPage should open and not the ChildPage. Also the MainPage should show State that the ChildPage updated before the app was Closed or Suspended.

Any ideas on how the SuspensionManager object can be safely modified to accomplish this?


Solution

  • Using the suggestion from Danielovich (which is NOT complete), the solution is 2 parts:

    Firstly:

    • No longer save state in the pageState object, instead save state using the SessionState object. Why? This is so that all Pages can share the same State information.

    Secondly:

    • To ensure, that NavigationState is not saved so that MainPage is always the default page, we need to change the SaveFrameNavigationState in SuspensionManager as follows:

      private static void SaveFrameNavigationState(Frame frame)
      {
          var frameState = SessionStateForFrame(frame);
          frame.GetNavigationState();
          frameState["Navigation"] = "1,1,0,15,Skycap.MainPage,12,0";
      }