(Difference between revisions)

Saving application preferences in Windows Phone

From Nokia Developer Wiki
Jump to: navigation, search
kiran10182 (Talk | contribs)
m (Kiran10182 -)
kiran10182 (Talk | contribs)
m (Kiran10182 -)
Line 1: Line 1:
[[Category:Windows Phone]][[Category:Silverlight]]
[[Category:Windows Phone]][[Category:Silverlight]][[Category:Code Snippet]]
{{Abstract|This article explains how to save and load application preferences on Windows Phone 7 devices. }}  
{{Abstract|This article explains how to save and load application preferences on Windows Phone 7 devices. }}  
{{ArticleMetaData <!-- v1.1 -->
{{ArticleMetaData <!-- v1.1 -->

Revision as of 15:55, 30 January 2012

This article explains how to save and load application preferences on Windows Phone 7 devices.

WP Metro Icon File.png
SignpostIcon XAML 40.png
WP Metro Icon WP8.png
SignpostIcon WP7 70px.png
Article Metadata
Tested with
Devices(s): 'Nokia Lumia 800
Keywords: IsolatedStorageSettings -->
Created: manikantan (30 Jan 2012)
Last edited: kiran10182 (30 Jan 2012)



In most applications, saving user preferences is a common task. Common scenarios can be - Saving user's country in applications like Groupon, saving and loading top scores in games, saving user's preferences for sounds in a game. This articles discusses the procedure to save and load such user preferences.


The class IsolatedStorageSettings comes to our rescue to save and load these preferences. The class helps you store dictionary-like, key-value store. IsolatedStorageSettings are preserved across device reboots. The settings are stored inside the Isolated Storage space of an application, hence inaccessible to other applications. Hence, we can claim they are secure.

Sample application

Below, we create a simple application that stores the user's preferences in a game. Three elements such as Game Music, Timed Mode and Difficulty level have been identified as preferences for a game. Since Game Music and Timed Mode essentially hold boolean values, in the UI of application we use ToggleSwitch. ToggleSwitch does not come along with the Windows Phone SDK and has to be downloaded separately from codeplex and installed. This has been described here. This is rich resource for Windows Phone controls. Lets create the user interface for the settings page in XAML.

mc:Ignorable="d" d:DesignWidth="480" d:DesignHeight="768"
FontFamily="{StaticResource PhoneFontFamilyNormal}"
FontSize="{StaticResource PhoneFontSizeNormal}"
Foreground="{StaticResource PhoneForegroundBrush}"
SupportedOrientations="Portrait" Orientation="Portrait"
<!--LayoutRoot is the root grid where all page content is placed-->
<Grid x:Name="LayoutRoot" Background="Transparent">
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
<!--TitlePanel contains the name of the application and page title-->
<StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="12,17,0,28">
<TextBlock x:Name="ApplicationTitle" Text="MY GAME" Style="{StaticResource PhoneTextNormalStyle}"/>
<TextBlock x:Name="PageTitle" Text="Settings" Margin="9,-7,0,0" Style="{StaticResource PhoneTextTitle1Style}"/>
<!--ContentPanel - place additional content here-->
<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
<toolbox:ToggleSwitch Name="gameMusic" Header="Game Music"></toolbox:ToggleSwitch>
<toolbox:ToggleSwitch Name="timed" Header="Timed Mode" Margin="6,119,6,371"></toolbox:ToggleSwitch>
<Slider Height="84" HorizontalAlignment="Left" Margin="12,310,0,0" Name="slider1" VerticalAlignment="Top" Width="418" Value="1" Minimum="1" />
<TextBlock Height="30" HorizontalAlignment="Left" Margin="12,274,0,0" Name="Difficulty" Text="Difficulty Level" VerticalAlignment="Top" Width="135" />
<!--Sample code showing usage of ApplicationBar-->
<shell:ApplicationBar IsVisible="True" IsMenuEnabled="True">
<shell:ApplicationBarIconButton IconUri="/Images/appbar_button1.png" Text="Button 1"/>
<shell:ApplicationBarIconButton IconUri="/Images/appbar_button2.png" Text="Button 2"/>
<shell:ApplicationBarMenuItem Text="MenuItem 1"/>
<shell:ApplicationBarMenuItem Text="MenuItem 2"/>

This gives us an interface like this.

Application UI

Code Behind

Instead of having a save and load button on the UI, it is more intuitive to simply load and save preferences when the user navigates TO and FROM the page. Hence in the MainPage.xaml.cs Class, we override the OnNavigatedTo() and OnNavigatedFrom() methods.

namespace Pref
public partial class MainPage : PhoneApplicationPage
private IsolatedStorageSettings settings;
// Constructor
public MainPage()
settings = IsolatedStorageSettings.ApplicationSettings;
protected override void OnNavigatedTo(NavigationEventArgs e)
System.Diagnostics.Debug.WriteLine("into the app");
System.Diagnostics.Debug.WriteLine("Retrieving values");
gameMusic.IsChecked = (bool)settings["gamemusic"];
timed.IsChecked = (bool)settings["timed"];
slider1.Value = ( Int16)settings["diff"];
catch(KeyNotFoundException ex)
System.Diagnostics.Debug.WriteLine("First Time using the app");
settings.Add("timed", false);
settings.Add("gamemusic", false);
settings.Add("diff", 1);
protected override void OnNavigatedFrom(NavigationEventArgs e)
System.Diagnostics.Debug.WriteLine("Exiting, so save now");
settings["timed"] = timed.IsChecked;
settings["diff"] = (Int16) slider1.Value;
settings["gamemusic"] = gameMusic.IsChecked;

So as you can observe, I am accessing the values in the IsolatedStorageSettings in the form of Key Value pairs. Remember to cast the values when retrieving or saving to this file. It is also reminded that Save() method needs to be called to store or flush these values. If a key is non-existent in the IsolatedStorageSettings, then it may raise KeyNotFoundException.


This article describes one of the fundamental tasks in application development - saving and loading user preferences. This is can be extended to many minimal storage use-cases such as top scores in games.

Further reading

1. How to: Store and Retrieve Application Settings Using Isolated Storage

2. IsolatedStorageSettings class

440 page views in the last 30 days.

Was this page helpful?

Your feedback about this content is important. Let us know what you think.


Thank you!

We appreciate your feedback.