I. Overview
Application lifecycle details can be referenced in the MSDN documentation for Windows8.1 development http://msdn.microsoft.com/library/windows/apps/hh464925.aspx
There are three states in the application life cycle: Running (running), Suspended (suspend, pause), notrunning (terminating).
So what's the difference between suspended and notruning? Suspended means that when the user switches to another program, your application is likely to be suspended for a period of time until the user switches back to your application. During this time, the application is dormant, in other words, the code in your application does not run, but the objects and variables of the application, including the frame and state of the backstack, are saved in memory. This allows users to switch between fast and fluid applications. However, the phone's operating system may choose to terminate your application, which means that the system will remove objects and variables from memory with the application. This means that the next time the user launches the application, it does not have any memory of the state of the application, but instead creates a new instance.
What happens when the phone pauses the application and then terminates the program? Most occur when the system requires an application to occupy memory. But it can also happen if the phone runs out of battery or the user restarts the phone. When your application is suspended or suspended, it may terminate without notice and enter the notruning state, so we need to quickly save the current state of the program (including variables, objects, etc.) before the program is suspended or suspended, i.e. suspended. So, we need to use techniques to save the state, here are 3 tricks.
Second, master the Suspensionmanager
Most of this saved state work will be done in the app class. The App class is App.xaml.cs. You're going to rewrite the way you want to work with your application in App class. The most important thing is to rewrite the OnLaunched () method that is called every time the application starts and the onsuspending () method that is called before the program pauses. (The following code needs to add a basic template because some of the classes in the basic template need to be added)
1, in App.xaml.cs, find Onlaunched () method, add Suspensionmanager method between.
protected Async Override voidOnLaunched (Launchactivatedeventargs e) {#ifDEBUGif(System.Diagnostics.Debugger.IsAttached) { This. Debugsettings.enableframeratecounter =true; }#endifFrame Rootframe= Window.Current.Content asFrame; //do not repeat application initialization when the window already contains content.//just make sure the window is active if(Rootframe = =NULL) { //Create a frame to act as the navigation context and navigate to the first pageRootframe =NewFrame (); suspensionmanager.registerframe (rootframe, "appframe"); //Register, Suspensionmanager.registerframe tells Suspensionmanager which object (Rootframe, the only frame in our application) will be paused//TODO: Change this value to the cache size that is appropriate for your applicationRootframe.cachesize =1; if(E.previousexecutionstate = =applicationexecutionstate.terminated) {//TODO: Loading status from a previously suspended application await Suspensionmanager.restoreasync (); } //Place the frame in the current windowWindow.Current.Content =Rootframe; } if(Rootframe.content = =NULL) { //removes the revolving door navigation used to start. if(Rootframe.contenttransitions! =NULL) { This. Transitions =Newtransitioncollection (); foreach(varCinchrootframe.contenttransitions) { This. Transitions. ADD (c); }} rootframe.contenttransitions=NULL; Rootframe.navigated+= This. rootframe_firstnavigated; //When the navigation stack has not been restored, navigate to the first page,//and configure it by passing in the required information as a navigation parameter//New Page if(!rootframe.navigate (typeof(Page1), e.arguments)) { Throw NewException ("Failed to create initial page"); } } //Make sure the current window is activeWindow.Current.Activate (); }
2, in App.xaml.cs, find Onsuspending () method, add Suspensionmanager method between
Private Async void Onsuspending (object sender, Suspendingeventargs e) { var deferral = E. Suspendingoperation.getdeferral (); // TODO: Save application state and stop any background activity await suspensionmanager.saveasync (); // storage State, which saves the current state of the frame when the app is suspended. deferral.complete (); }
---------------------------------------------------------------------------------------The following two methods use the data storage method to save the state.
Second, master the Windows.Storage.ApplicationData.Current.LocalSettings
LocalSettings is a simple dictionary that is saved to an application-allocated storage area. There is also a storage area called Windows.Storage.ApplicationData.Current.RoamingSettings, which saves data to folders that are synchronized with all devices. Now, use localsettings below. (Localsetting is easy to use)
1. In the Valuetextbox_textchanged event handling method, add the following code:
Private void valuetextbox_textchanged (object= windows.storage.applicationdata.current.localsettings;//Create localsettinglocalsettings.values[" Value"] = valuetextbox.text;}
2. In the Navigationhelper_loadstate () method, add
Private void Navigationhelper_loadstate (object sender, Loadstateeventargs e) { = Windows.Storage.ApplicationData.Current.LocalSettings; Create localsetting
if (LocalSettings.Values.ContainsKey ("value"= localsettings.values[" value"
Third, master the Navigationhelper
Similar to the Localsetting,navigationhelper class application is equally simple.
1. Add in Navigationhelper_savestate ()
Private void Navigationhelper_savestate (object sender, Savestateeventargs e) { e.pagestate[" testvalue"] = valuetext.text; }
2. Add in Navigationhelper_loadstate ()
Private void Navigationhelper_loadstate (object sender, Loadstateeventargs e) { if (E. pagestate!=null&&e.pagestate.containskey ("testvalue")) { valuetext.text=e.pagestate["testvalue"]. ToString (); } }
Most of the above content comes from Bob's instructional videos, if any, please point out.
Life cycle management of wp8.1 Study6:app