Isolatedstorageset usage-program running click Start button to restart program Restoration

Source: Internet
Author: User

This is what I just learned.

This processing mechanism is very good, and the principle of the tombstone mechanism is somewhat different:

The tombstone mechanism is executed in application_launching, application_activated, application_deactivated, application_closing, and other methods of the app file, or in the background code onnavigatedfrom, onnavigatedto.

This article describes how to use isolatedstoragesettings in the background code for independent storage.

This example uses textbox as an example.

First, add a button on the mainpage and add an event to the click to jump to the new Add. XAML button in the form.

Before writing the Add. XAML file, you also need to judge how to add and load the mainpage background for the first time.

Assume that the user adds. after writing content on the XAML page, click the start button and return to the Start Screen, and then start the application. At this time, you need to determine whether the user has saved the content that was not saved last time.

First, add the following code to the loaded event:

Private void phoneapplicationpage_loaded (Object sender, routedeventargs E)
{
// First instantiate isolatedstoragesettings
Isolatedstoragesettings settings = isolatedstoragesettings. applicationsettings;
// Define a variable to store status information
String state = "";
// Check whether the status information "state" exists in isolatedstoragesettings"
If (settings. Contains ("state "))
{
// If yes, read the state information to the state variable.
If (settings. trygetvalue <string> ("State", out State ))
{
// Determine whether the obtained status information is "add ".
If (State = "add ")
{
// If the read status information is "add", if yes, it indicates that the user has not saved the content, the page will jump to the "Add. XAML" status page before closing.
Navigationservice. navigate (New uri ("/toubtest; component/Add. XAML", urikind. Relative ));
}
}
}
Isolatedstoragetextbox. Focus ();
}

The following is the initialization code on the Add. XAML page.
Define a global isolatedstoragesettings

Private isolatedstoragesettings settings = isolatedstoragesettings. applicationsettings;

Then initializing

Void add_loaded (Object sender, routedeventargs E)
{
String state = "";
// Determine whether the State is included.
If (settings. Contains ("state "))
{
// If it contains, the retrieved state is assigned to the State.
If (settings. trygetvalue <string> ("State", out State ))
{
// If the State content is add
If (State = "add ")
{
String value = "";
// Determine whether the value is included
If (settings. Contains ("value "))
{
// Retrieve Value
If (settings. trygetvalue <string> ("value", out value ))
{
// Display the value in the Textbox Control
This. addtextbox. Text = value;
}
}
}
}
}
// The content must be initialized in the following way to determine whether or not the State exists
Settings ["state"] = "add ";
Settings ["value"] = addtextbox. text;
Addtextbox. Focus ();
// Move the cursor to the end of the text
Addtextbox. selectionstart = addtextbox. Text. length;
}

Add the textchanged event to the textbox to save the status content in time.

Private void addtextbox_textchanged (Object sender, textchangedeventargs E)
{
Settings ["value"] = addtextbox. text;
}

Finally, add a clearing event to the backkeypress return button.

Private void phoneapplicationpage_backkeypress (Object sender, system. componentmodel. canceleventargs E)
{
// Clear the original state content add
Settings ["state"] = "";
}

Source code: http://vdisk.weibo.com/s/6MeYw

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.