Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Using application settings in Windows Phone 7 apps

DZone's Guide to

Using application settings in Windows Phone 7 apps

· Mobile Zone
Free Resource

Discover how to focus on operators for Reactive Programming and how they are essential to react to data in your application.  Brought to you in partnership with Wakanda

Many applications have user input that should be somehow preserved in order to be restored when the application starts again. This includes preferences, URLs, general information and whatnot. As in any other .NET application, a Windows Phone 7 application supports application settings.

Although the concept is similar, the way it is implemented is a bit different compared to regular console applications, for example. The way you are used to access them is through Properties.Settings.Default or through the Application Properties dialog. Both of these options are not available in a Windows Phone 7 application.

Instead, you should use IsolatedStorageSettings.ApplicationSettings, which is a member of the System.IO.IsolatedStorage namespace. The application settings are stored in a local application-scoped file that can, in fact, be created via IsolatedStorageFile.GetUserStoreForApplication().CreateFile(path). This, however, would involve direct data manipulation and ApplicationSettings provides the necessary abstraction layer so that you can work with an existing structure without manually working with settings file contents.

ApplicationSettings by itself is an instance of IEnumerable, so you can apply any action to it that applies to enumerable types.



The actual settings can be added and saved via the Add and Save methods, the latter being used to store the modified collection. Without it, once the application is closed, the settings are lost.

NOTE: Make sure you use unique names for each key. Otherwise, an ArgumentException will be thrown:



One important thing that should be mentioned about new application settings is that you can actually pass an object as the value for a specified key.

This makes it easier to store various class instances and one of the benefits it brings to the table is that you won’t have to serialize the instance manually. The object is restored in the same state it was passed to the Add method.

To retrieve saved settings, you can use two methods. First of all, you can use TryGetValue:

The advantage of this method is that no KeyNotFoundException will be thrown in case the key is not found. searchedValue will be empty, but at the same time, you won't have to deal with exception handling for this specific method.

The other way would be looking for the needed setting by directly passing the searched key:
 

Be aware of the fact that in case no key is found, a KeyNotFoundException will be thrown and you need to properly handle it in order to avoid an application crash.

You can also iterate through existing values and keys by using the Values and Keys ICollection-defined properties:

The setting removal is performed pretty much the same way as adding. However, this time you are only referencing the key by which the removed object is identified:

NOTE: No exception will be thrown if the key passed to the Remove method is not found, so you would have to make sure that the entered key is valid.

Learn how divergent branches can appear in your repository and how to better understand why they are called “branches".  Brought to you in partnership with Wakanda

Topics:

Opinions expressed by DZone contributors are their own.

The best of DZone straight to your inbox.

SEE AN EXAMPLE
Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.
Subscribe

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}