IsolatedStorageSettings for Windows Phone independent storage

Source: Internet
Author: User

Isolated Storage [independent Storage] can store your data locally in two ways. The first is through the key/value pair in the database, called IsolatedStorageSettings. The second is to create a real file and directory called IsolatedStorageFile.
IsolatedStorageSettings allows you to store key/value pairs in a dictionary (note that no setting is required) and then read them out. The data is always stored, whether the application is stopped/started or shut down. It always exists unless you delete it or the user detaches your application. However, it cannot be read before it is added to the dictionary.
IsolatedStorageSettings provides a convenient way to store user-specific data as key/value pairs in local IsolatedStorageSettings. A typical purpose is to save settings. In addition, the "value" in the key-value pairs that can be saved here is not only a string, but also an instantiated object. The following describes how to store strings and instantiate objects.
1. String-type data processing
1. Save the key/value pair

View Code

1 // obtain IsolatedStorageSettings
2 var settings = IsolatedStorageSettings. ApplicationSettings;
3 // define the key/value pair to be stored
4 String key = "username ";
5 String value = "Yu zhile ";
6 // determine whether there is an existing modification or not
7 if (settings. Contains (key ))
8 {
9 settings [key] = value;
10}
11 else
12 {
13 settings. Add (key, value );
14}
15 // Save the information. If it is not called, data will be lost after restart.
16 settings. Save ();

2. Get Data

View Code

String key = "username ";
String value = String. Empty;
Try
{
// Obtain string information from IsolatedStorageSettings
Var settings = IsolatedStorageSettings. ApplicationSettings;
Bool isFind = settings. TryGetValue (key, out value );
If (! IsFind)
{
Value = String. Empty;
}
}
Catch (System. Exception ex)
{
// Not found
}

2. Processing of instantiated objects
1. Define an object

View Code

    [DataContract]
public class UserInfo
{
[DataMember]
public String UserName { get; set; }
[DataMember]
public String BlogAddress { get; set; }
}

Note: [DataContract] (Data contract) and [DataMember] must be used here. Only instances of such serialized objects can be saved to IsolatedStorageSettings. To use [DataContract], you need to add reference System. Runtime. Serialization.
2. Save the instantiated object

View Code

// Obtain IsolatedStorageSettings
Var settings = IsolatedStorageSettings. ApplicationSettings;
// Define the key to be stored
String key = "username ";
// Instantiate the object
UserInfo user = new UserInfo ();
User. UserName = "Yu zhile ";
User. BlogAddress = "http://www.cnblogs.com/huizhang212 ";
Try
{
// Determine whether there is an existing modification or not.
If (settings. Contains (key ))
{
Settings [key] = user;
}
Else
{
Settings. Add (key, user );
}
// Save the information. If it is not called, data will be lost after restart.
Settings. Save ();
}
Catch (System. Exception ex)
{
// Failed to save. if the object is not instantiated, an exception is reported.
}

3. Get the stored instantiated object

View Code

String key = "username ";
UserInfo user = null;
// Obtain string information from IsolatedStorageSettings
Var settings = IsolatedStorageSettings. ApplicationSettings;
If (settings. Contains (key ))
{
User = settings [key] as UserInfo;
}

 

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.