Application Settings

,

Saving objects in persistent state is such a common task that the isolated storage API includes an IDictionary<string, object> class named IsolateStorageSettings that is automatically persisted to isolated storage when your app is closed or tombstoned. IsolateStorageSettings is a convenient way to store global application state (see Figure 28.2).

Image

FIGURE 28.2 An app’s IsolatedStorageSettings instance is retrieved using the IsolatedStorageSettings.ApplicationSettings property.

IsolateStorageSettings is a singleton, accessible via the IsolatedStorageSettings.ApplicationSettings property. The ApplicationSettings property can be used the same as you would any other IDictionary.

To place an object in persistent state, add a key/value pair to the collection, like so:

IsolatedStorageSettings.ApplicationSettings["Foo"] = "Bah";

To retrieve the item from persistent state, the following can be used:

string value = (string)IsolatedStorageSettings.ApplicationSettings["Foo"];

If it is uncertain whether an item exists in ApplicationSettings, use the TryGetValue method as shown:

string value;
if (!IsolatedStorageSettings.ApplicationSettings.TryGetValue("Foo",
                                                             out value))
{
    value = "Unknown";
}

The Contains method can also be used to test whether an item exists in the dictionary.

Serialization Requirements

Objects placed in ApplicationSettings have the same serialization requirements as transient application state (see Chapter 3, “Understanding the Application Execution Model”); data placed in ApplicationSetting must be a primitive type, a known serializable type (for example, Decimal), or be capable of being serialized using a DataContractSerializer. If an object is unable to be serialized, an exception is raised.


Caution

If even a single object in the ApplicationSettings raises an exception when the IsolatedStorageSettings.Save method is called, all settings are prevented from being saved.


When the user exits your app, Windows Phone automatically calls Save to preserve the IsolatedStorageSettings object so that it can be automatically reinstated when your application starts again.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.129.211.165