Independent storage for Windows Phone development 2

Source: Internet
Author: User

Symptom:
In actual application, when operating a program, a new task is suddenly inserted, and the current task is submitted to the background to execute the new program. For example, if a user is registering a website and entering form information, but a task with a higher priority needs to be resolved, the back or start button will be used to select other operations. If the data in the registry form is not saved, data may be lost when the page is reloaded, which is a poor user experience. This article will solve this problem.

Program case:
Mainpage only has one textbox. The mytextbox_textchanged event of textbox and the root_loaded event of mainpage are available:
The process is as follows:
First, input the content in textbox and save it to phoneappservice according to the mytextbox_textchanged event. state, through app. XAML. application_launching, application_activated, application_deactivated, and application_closing events in the CS file to save and read phoneappservice. state data to the independent storage isolatedstoragesetting.
Because the phoneappservice. State data is deleted by the memory after the back or start, it can only be saved to the special functions of the independent storage.
Remember: The simulator will not be saved after being restarted (equivalent to restarting the real machine.

private void myTextBox_TextChanged (object sender, TextChangedEventArgs e)
        {// Save the content of the text box
            PhoneApplicationService phoneAppService = PhoneApplicationService.Current;
            phoneAppService.State ["myValue"] = myTextBox.Text;
        }
 private void Root_Loaded (object sender, RoutedEventArgs e)
        {PhoneApplicationService phoneAppService = PhoneApplicationService.Current;
            if (phoneAppService.State.ContainsKey ("myValue")) {
                myTextBox.Text = phoneAppService.State ["myValue"]. ToString ();
            }
        }
-------------------------------
APP.xaml.cs file:

 // Code executed when the application starts (for example, from the "Start" menu)
        // This code is not executed when the application is reactivated
        private void Application_Launching (object sender, LaunchingEventArgs e)
        {
            LoadMyValue ();
        }

        // Code executed when the application is activated (put in the foreground)
        // This code is not executed when the application is started for the first time
        private void Application_Activated (object sender, ActivatedEventArgs e)
        {
            LoadMyValue ();
        }

        // Code executed when the application is deactivated (sent to the background)
        // This code is not executed when the application is closed
        private void Application_Deactivated (object sender, DeactivatedEventArgs e)
        {
            SaveMyValue ();
        }

        // Code executed when the application is closed (for example, the user clicks "Back")
        // This code is not executed when the application is deactivated
        private void Application_Closing (object sender, ClosingEventArgs e)
        {
            SaveMyValue ();
        }
The
private void SaveMyValue () {

            PhoneApplicationService phoneAppService = PhoneApplicationService.Current;
            IsolatedStorageSettings setting = IsolatedStorageSettings.ApplicationSettings;
            if (phoneAppService.State.ContainsKey ("myValue"))
            {
                setting ["myValue"] = phoneAppService.State ["myValue"];
            }
        }
        private void LoadMyValue ()
        {
            PhoneApplicationService phoneAppService = PhoneApplicationService.Current;
            IsolatedStorageSettings setting = IsolatedStorageSettings.ApplicationSettings;
            if (setting.Contains ("myValue"))
            {
                phoneAppService.State ["myValue"] = setting ["myValue"];
            }
        }

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.