Windows 8 Application Development-Local Data Storage

Source: Internet
Author: User

In applications, users usually store information actively or passively. When the application is closed, the data is still stored on the local device, and the data is automatically loaded when the application is re-activated. The following uses a small instance to demonstrate how to store user data locally. First, create two input boxes in the Windows 8 Application: name and age.

 <  Stackpanel  Grid. Row  = "1"  Margin  = ","> <  Stackpanel  Orientation  = "Horizontal"  Margin  = ","> <  Textblock  Text  = "Name :" Style  = "{  Staticresource  Basictextstyle  }"  Width  = "50"/> <  Textbox  X  :  Name  = "Nameinput"  Width  = "200"/> </  Stackpanel  > <  Stackpanel Orientation  = "Horizontal"  Margin  = ","> <  Textblock  Text  = "Age :"  Style  = "{  Staticresource  Basictextstyle  }"  Width  = "50"/> <  Textbox  X  : Name  = "Ageinput"  Width  = "200"/> </  Stackpanel  > <  Stackpanel  Orientation  = "Horizontal"  Margin  = ","> <  Button  Content  = "Save"  Click  = "Save_button_click"/> < Button  Content  = "Reset"  Click  = "Reset_button_click"/> </  Stackpanel  > </  Stackpanel  > 

Open mainpage. XAML. cs. In order to use the data storage API, add the using Windows. Storage namespace first. Use the applicationdata. localsettings attribute in mainpage to obtain the local bucket of the application settings.

 
ApplicationdatacontainerLocalsettings =Applicationdata. Current. localsettings;

In the save_button_click event, set two values for localsettings to store the name and age data respectively.

 
Private voidSave_button_click (ObjectSender,RoutedeventargsE) {localsettings. Values ["Name"] = Nameinput. Text; localsettings. Values ["Age"] = Ageinput. Text ;}

The following is how to load the data that has been saved locally when the application is activated, in mainpage. XAML. CSProgramThere is already a loadstate method used to load the application data or State. We can load the data here.

 Protected override void  Loadstate (  Object Navigationparameter,  Dictionary  <  String  ,  Object  > Pagestate ){  If  (Localsettings. Values. containskey (  "Name"  ) {Nameinput. Text = localsettings. Values [  "Name"  ]. Tostring ();}  If  (Localsettings. Values. containskey ( "Age"  ) {Ageinput. Text = localsettings. Values [  "Age"  ]. Tostring ();}} 

Local data storage is used to save important information such as application settings. Of course, the data is stored on the local device and can be eliminated if the data is no longer used. As follows:CodeThis means that the previous name and age information will be deleted. After the data is deleted, the app cannot be loaded after it is re-activated.

Private voidReset_button_click (ObjectSender,RoutedeventargsE) {localsettings. Values. Remove ("Name"); Localsettings. Values. Remove ("Age"); Nameinput. Text =""; Ageinput. Text ="";}
Source code download

Http://sdrv.ms/RxauDH

Related Article

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.