Frage

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?

War es hilfreich?

Lösung 2

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";
    }
    

Andere Tipps

Why not just add your data from the ChildPage to the SessionState dictionatry and fill it into the MainPage when the app resumes ?

You don't even need to modify the SuspensionManager for doing that!

public BasicPage1() {
    this.InitializeComponent();

    Application.Current.Suspending += Current_Suspending;
    Application.Current.Resuming += Current_Resuming;
}

void Current_Resuming( object sender, object e ) {
    var name = SuspensionManager.SessionState["name"].ToString();
}

void Current_Suspending( object sender, Windows.ApplicationModel.SuspendingEventArgs e ) {
    SuspensionManager.SessionState.Add( "name", "danielovich" );
}
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top