WP7 Application Data Storage IsolatedStorage

Source: Internet
Author: User

Windows Phone 7 provides two data Storage methods for the Isolated Storage function: IsolatedStorageFile and IsolatedStorageSettings ). Through independent storage, we can save application data, such as user settings and program running status. This article describes how to use IsolatedStorageSettings.

Isolatedstoragesettionary actually provides a Dictionary <TKey, TValue> generic class that stores the data of an application by ing the key-value Tkey to the value TValue. First, create a global settings Using the IsolatedStorageSettings class in the program, and define an integer variable for subsequent testing.

IsolatedStorageSettings settings = IsolatedStorageSettings.ApplicationSettings;int testInt = 10;

Add two buttons: one to perform the "+ 1" Operation for testInt and the other to display the current value of testInt.

private void addBtn_Click(object sender, RoutedEventArgs e){    testInt++;}private void showBtn_Click(object sender, RoutedEventArgs e){    MessageBox.Show(testInt.ToString());}

Next, add a Save button to Save the testInt and TextBox values. First, use the Contains (string key) method to check whether there is a "textbox" key value. If not, use the Add (string key, object value) method to Add the key value, the numeric type corresponding to the key can be customized according to development requirements. In this example, the String and Int types are used.

private void saveBtn_Click(object sender, RoutedEventArgs e){    if (!settings.Contains("textbox"))    {        settings.Add("textbox", textBox.Text);    }    else    {        settings["textbox"] = textBox.Text;    }    if (!settings.Contains("integer"))    {        settings.Add("integer", testInt);    }    else    {        settings["integer"] = testInt;    }    settings.Save();}

When you restart the program each time, you can directly obtain the corresponding data from settings. For the sake of insurance, use TryGetValue <T> (string key, out T value) to obtain the value of the specified key. If the key value does not exist, False is returned.

public MainPage(){    InitializeComponent();    string textVal;    if (settings.TryGetValue<string>("textbox", out textVal))    {        textBox.Text = textVal;    }    else    {        textBox.Text = "No Text";    }    int intVal;    if (settings.TryGetValue<int>("integer", out intVal))    {        testInt = intVal;    }    else    {        testInt = 10;    }}

Add the XAML code:

<TextBox x:Name="textBox" Width="460" Height="72" /><Button x:Name="addBtn" Content="Add" Width="160" Height="72"         Click="addBtn_Click"/><Button x:Name="showBtn" Content="Show" Width="160" Height="72"        Click="showBtn_Click"/><Button x:Name="saveBtn" Content="Save" Width="160" Height="72"        Click="saveBtn_Click"/>
Test Run

If you do not save the settings, "No Text" is displayed each time you start TextBox. The testInt value is 10.

 

Click "Save" and exit the program. The modified content is saved.

 

Related Materials

System. IO. IsolatedStorage

IsolatedStorageFile

IsolatedStorageSettings

IsolatedStorageSettings. TryGetValue <T>

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.