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"];
}
}