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