Windows Phone: adding settings to your app
Mood: cheerful
Posted on 2012-12-13 23:09:00
Tags: windowsphone wpdev
Words: 861
I like including a lot of settings in my apps - it gives the user a lot of control about how the app looks/behaves. But it's a little tedious to add the code to save them to Isolated Storage, notify when they've changed, and allowing the user to change them on a settings page. After just doing this for a new release of PhotoNotes, I thought I'd write a step-by-step guide to adding settings to an app.
In this example, I was adding a boolean setting for whether PhotoNotes's tile should be live. So, let's get started!
Step 1: Add a UserSettings class
This class will provide access to the settings and handle saving/loading to Isolated Storage, as well as have an event for when the settings have changed.
Here's the actual UserSettings class for PhotoNotes. If you prefer you can also download UserSettings.cs here.
using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.IO.IsolatedStorage;
using System.Collections.ObjectModel;
namespace NoteLens.wp8
{
public class UserSettings
{
IsolatedStorageSettings settings;
public event EventHandler SettingsChanged;
const string TilePictures = "TilePictures";
const bool TilePicturesDefault = true;
public bool ShowTilePictures
{
get
{
return GetValueOrDefault(TilePictures, TilePicturesDefault);
}
set
{
if (AddOrUpdateValue(TilePictures, value))
{
Save();
}
}
}
public static UserSettings Instance = null;
// This can't be private because it's called from the XAML in App.xaml,
// but don't create one of these in code!
public UserSettings()
{
try
{
settings = IsolatedStorageSettings.ApplicationSettings;
if (Instance == null)
{
Instance = this;
}
else
{
System.Diagnostics.Debug.Assert(false,
"Created multiple UserSettings!");
}
}
catch (Exception)
{
settings = null;
}
}
///
/// Update a setting value for our application. If the setting does not
/// exist, then add the setting.
///
///
///
///
public bool AddOrUpdateValue(string Key, Object value)
{
bool valueChanged = false;
// If the key exists
if (settings.Contains(Key))
{
// If the value has changed
if (settings[Key] != value)
{
// Store the new value
settings[Key] = value;
valueChanged = true;
}
}
// Otherwise create the key.
else
{
settings.Add(Key, value);
valueChanged = true;
}
return valueChanged;
}
///
/// Get the current value of the setting, or if it is not found, set the
/// setting to the default setting.
///
///
///
///
///
public T GetValueOrDefault<T>(string Key, T defaultValue)
{
T value;
// If the key exists, retrieve the value.
if (settings.Contains(Key))
{
value = (T)settings[Key];
}
// Otherwise, use the default value.
else
{
value = defaultValue;
}
return value;
}
///
/// Save the settings.
///
public void Save()
{
settings.Save();
EventHandler settingsChanged = SettingsChanged;
if (settingsChanged != null)
{
settingsChanged(this, new EventArgs());
}
}
}
}
xmlns:nl="clr-namespace:NoteLens.wp8"
<nl:UserSettings x:Key="appSettings"/>
<controls:PivotItem Header="settings">
<StackPanel Orientation="Vertical">
<toolkit:ToggleSwitch IsChecked="
{Binding Source={StaticResource appSettings},
Path=ShowTilePictures, Mode=TwoWay}">
Live Tile
</toolkit:ToggleSwitch>
</StackPanel>
</controls:PivotItem>
UserSettings.Instance.SettingsChanged += appSettings_SettingsChanged;
UserSettings.Instance.SettingsChanged -= appSettings_SettingsChanged;
void appSettings_SettingsChanged(object sender, EventArgs e)
{
Util.UpdateLiveTile();
}
UserSettings.Instance.ShowTilePictures
This backup was done by LJBackup.