locked
how to restore the data when app is reloaded RRS feed

  • Question

  • not getting the data of textblock or textbox saved when we reload what i need to do ?????

    i have tried in this way:

    i added LayoutAwarePage.cs and SuspensionManager.cs in common folder

    then in app.xaml.cs file 

    1)i made some changes in onlaunched method(changes are indicated as //<---)

                    

    protected async override void OnLaunched(LaunchActivatedEventArgs args)
            {
                Frame rootFrame = Window.Current.Content as Frame;

                // Do not repeat app initialization when the Window already has content,
                // just ensure that the window is active
                if (rootFrame == null)
                {
                    // Create a Frame to act as the navigation context and navigate to the first page
                    rootFrame = new Frame();
                    ApplicationLifeCycle.Common.SuspensionManager.RegisterFrame
                     (rootFrame, "appFrame");    //<-----


                    if (args.PreviousExecutionState == ApplicationExecutionState.Terminated)
                    {
                        //TODO: Load state from previously suspended application
                        await ApplicationLifeCycle.Common.SuspensionManager.RestoreAsync();   //<----
                    }

                    // Place the frame in the current Window
                    Window.Current.Content = rootFrame;
                }

    2) i made some changes to onsuspending method

      private async void OnSuspending(object sender, SuspendingEventArgs e)
            {
                var deferral = e.SuspendingOperation.GetDeferral();
                //TODO: Save application state and stop any background activity
                await ApplicationLifeCycle.Common.SuspensionManager.SaveAsync();  //<----
                deferral.Complete();
            }

    in mainpage.xaml.cs 

    here nameInput is the name of textbox and greetingOutput is the name of textblock

    wrote a method Loadstate

            protected override void LoadState(Object navigationParameter, Dictionary<String, Object> pageState)
            {
                
                // Restore values stored in session state.
                if (pageState != null && pageState.ContainsKey("greetingOutputText"))
                {
                    greetingOutput.Text = pageState["greetingOutputText"].ToString();
                }

                // Restore values stored in app data.
                Windows.Storage.ApplicationDataContainer roamingSettings =
                    Windows.Storage.ApplicationData.Current.RoamingSettings;
                if (roamingSettings.Values.ContainsKey("userName"))
                {
                    nameInput.Text = roamingSettings.Values["userName"].ToString();
                   
                }
            }

    wrote a method Savestate:

     protected override void SaveState(Dictionary<String, Object> pageState)
            {
                pageState["greetingOutputText"] = greetingOutput.Text;
                pageState["userName"] = nameInput.Text;
            }

    changed public sealed partial class MainPage : Page

    to

    public sealed partial class MainPage : ApplicationLifeCycle.Common.LayoutAwarePage

    In the mainpage 

    1)page attribute is changed to common:LayoutAwarePage

    and added  xmlns:common="using:DataSuspension.Common"

    but still not getting the data of textblock or textbox saved when we reload what else i need to do ?????

    Friday, August 30, 2013 11:12 AM

Answers