Win8 app Development-Save and restore application state

Source: Internet
Author: User

Because the Win8 app only allows one app to run in the foreground at the same time, that is, when switching to another app, the current app will be suspended and stopped, and if we are doing some state information entry then we should temporarily save the information so that it can be restored when needed.

OK, start: Create a new blank application, add Class Statedate to your project, and the code is as follows:

public classstatedata:inotifypropertychanged {//Save state information for key name Private Const string key_title = "TITLE"; Private Const string key_content = "CONTENT"; Static property of the current class instance public static Statedata currentinstance {get; set; }//Title private stringM_title; public stringTitle {Get{return this. M_title; } set{if (value!=this. m_title) {This.m_title =Value OnPropertyChanged ("Title"); }}}//Content private stringM_content; public stringContent {Get{return this. m_content; } set{if (value!=this. m_content) {this.m_content =Value OnPropertyChanged ("Content"); }}}//constructor privateStatedata () {this.m_title = string. Empty; This.m_content = string. Empty; }//Static constructor staticsStatedata () {currentinstance = newStatedata (); }///<summary>//////</summary> public static void before loading LoadState () {Applicationdatacontainer container =  ApplicationData.Current.LocalSettings; if  ( Container. Values.containskey (Key_title)) {Currentinstance.title = (string ) container. Values[key_title]; } if  (container. Values.containskey (key_content)) {currentinstance.content= (string ) container. Values[key_content]; }}///<summary>//Save status information///</summary> public static void  SaveState () {Applicationdatacontainer Co Ntainer =  ApplicationData.Current.LocalSettings; container. Values[key_title] =  currentinstance.title; container. Values[key_content] =  currentinstance.content;} public event  PropertyChangedEventHandler propertychanged ; private void OnPropertyChanged (String  prop) {if (propertychanged! = null ) {propertychanged (this, new  P Ropertychangedeventargs (prop)); } } }

Then open MainPage.xaml and add two TextBox controls on the page:

<grid background= "{ThemeResource Applicationpagebackgroundthemebrush}" >        <stackpanel orientation= " Vertical "margin=" >            <textblock fontsize= "text=" Mail title: "margin=" 2,2,0,3 "/>            <textbox x:name = "Txttitle" margin= "3,0,5,3"/>            <textblock fontsize= "text=" message body: "margin=" 2,21,0,3 "/>            < TextBox x:name= "txtcontent" margin= "3,0,5,3" height= "$" textwrapping= "Wrap"/>        </stackpanel></ Grid>

Then add the following code to the Onnavigatedto method in the MainPage code:

protected override void Onnavigatedto (NavigationEventArgs e)        {            //title            Binding mytitlebinding = new Binding ();            Mytitlebinding.path = new PropertyPath ("Title");            Mytitlebinding.source = statedata.currentinstance;            Mytitlebinding.mode = Bindingmode.twoway;            This. txttitle.setbinding (Textbox.textproperty, mytitlebinding);//content Binding mycontextbinding = new  Binding (); Mycontextbinding.path = new PropertyPath ("Content"); mycontextbinding.source = statedata.currentinstance; Mycontextbinding.mode = Bindingmode.twoway; this. txtcontent.setbinding (Textbox.textproperty, mycontextbinding); }

Open the App.xaml.cs file and add the following code after the "TODO" comment in the Onlaunched method:

if (e.previousexecutionstate = = applicationexecutionstate.terminated) {   //todo:  loading State from a previously suspended application   statedata.loadstate ();} 

Add the following code after the "TODO" comment in the Onsuspending method:

Statedata.savestate ();

Note: 1, the relevant article refers to the onlaunched,onsuspending and other methods of use see msdn:https://msdn.microsoft.com/zh-cn/library/windows/apps/ Windows.ui.xaml.application.onlaunched.aspx, there are not many wordy.

2. LocalSettings: Used to get the application settings container in the local application data store.

Press F5 run will see this interface (I choose the simulator here, see a person likes)

On the Debug Location toolbar, select Suspend and Close

Then press F5 again to run and you'll find the title and content still exist.

Win8 app Development-Save and restore application state

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.