×
Namespaces

Variants
Actions
Revision as of 10:44, 9 November 2012 by jinek (Talk | contribs)

Introduction and best practices for IsolatedStorageSettings

From Nokia Developer Wiki
Jump to: navigation, search

This article shows how to work with IsolatedStorageSettings.

WP Metro Icon File.png
SignpostIcon XAML 40.png
WP Metro Icon WP8.png
SignpostIcon WP7 70px.png
Article Metadata
CompatibilityArticle
Created: (16 Nov 2012)
Last edited: jinek (09 Nov 2012)

Contents

Introduction

In Windows Phone you have to store all your files and permanent values in special place - Isolated Storage (link). There is a special class for storing values: IsolatedStorageSettings. You can store there anything you want: user settings, layout state or value to determine if program us running first tume. In this tutorial we will create a simple application, that keeps a value in IsolatedStorageSettings and then extend it to look more professional.

Basic Sample

  1. Run Visual Studio 2012
  2. Create new Windows Phone application (7.5 or 8.0)
  3. Navigate to MainPage.xaml and insert a checkBox into content grid: code here
  4. submit to checked and unchecked events
  5. now, when page is creating insert copying value from isolated storage settings to checkbox: If it is not exist - set true by default: code here
  6. When Checked - set setting to true and automatocally save it. Isolated Storage in windows phone is not thread safe, so you have to use locking the thread every time you doing something with isolated storage

code here

  1. When unchecked - setting to false and save it again.
  2. Run the application: screenshot here

Improvement

As you can see, everything works fine. But we have to check if value in storage exists, we have to call Save every time we update anything and we have to use lock and finnaly we have to keep the property key name. More simplier way to work with IsolatedStorageSettings is to create a wrapper class around it. It will save the settings every time we update any value (in a thread safe way), it will automatically setup default value if setting does not exist (and does not throw an exception when you try to access not existent value), and finally it keeps the name of property - we have to provide it only once during property declaration. Create new class and name it IsolatedStorageProperty:

using System.IO.IsolatedStorage;
 
namespace IsolatedStorageSample
{
internal static class IsolatedStoragePropertyHelper
{
public static readonly object ThreadLocker = new object();
public static readonly IsolatedStorageSettings Store = IsolatedStorageSettings.ApplicationSettings;
}
 
public class IsolatedStorageProperty<T>
{
private readonly object _defaultValue;
private readonly string _name;
private readonly object _syncObject = new object();
private bool _isInitialized;
private T _localValue;
 
public IsolatedStorageProperty(string name, T defaultValue = default(T))
{
_name = name;
_defaultValue = defaultValue;
}
 
public bool Exists
{
get { return IsolatedStoragePropertyHelper.Store.Contains(_name); }
}
 
public T Value
{
get
{
if (!_isInitialized)
{
InitializeLocalValue();
}
 
return _localValue;
}
set
{
lock (_syncObject)
{
IsolatedStoragePropertyHelper.Store[_name] = _localValue = value;
_isInitialized = true;
Save();
}
}
}
 
private void InitializeLocalValue()
{
lock (_syncObject)
if (!_isInitialized)
{
if (!Exists) SetDefault();
else
{
_localValue = (T) IsolatedStoragePropertyHelper.Store[_name];
_isInitialized = true;
}
}
}
 
private static void Save()
{
lock (IsolatedStoragePropertyHelper.ThreadLocker)
{
IsolatedStoragePropertyHelper.Store.Save();
}
}
 
public void SetDefault()
{
Value = (T) _defaultValue;
}
}
}

This class is pretty simple, but you can extend it as you want. Using is simple as well:

namespace IsolatedStorageSample
{
using System.Windows;
 
public partial class MainPage
{
static readonly IsolatedStorageProperty<bool> SwitchProperty = new IsolatedStorageProperty<bool>("switch",true);
 
public MainPage()
{
InitializeComponent();
chck.IsChecked = SwitchProperty.Value;
}
private void chck_Checked(object sender, RoutedEventArgs e)
{
SwitchProperty.Value = true;
}
 
private void chck_Unchecked(object sender, RoutedEventArgs e)
{
SwitchProperty.Value = false;
}
}
}

As you can see, a lot of lines of code are gone - very cool, when you store a lot of values. Lets create another setting: Good practice is to create a special static class, that will contain all your settings in it, so you can access any property in a program in any time you want, like this:

static readonly IsolatedStorageProperty<DateTime> FirstRunTimeProperty = new IsolatedStorageProperty<DateTime>("firstruntime", DateTime.Now);
 
public MainPage()
{
InitializeComponent();
chck.IsChecked = SwitchProperty.Value;
Loaded+=OnLoaded;
}
 
private void OnLoaded(object sender, RoutedEventArgs routedEventArgs)
{
MessageBox.Show(FirstRunTimeProperty.Value.ToString());
}

FirstRunTimeProperty lets me know when was first application run.

Every time you run the application message box shows only first time you run it. The "platform categories" will be displayed here in preview only - Copy paste relevant categories into text here

Version Hint

Windows Phone: [[Category:Windows Phone]]
[[Category:Windows Phone 7.5]]
[[Category:Windows Phone 8]]

Nokia Asha: [[Category:Nokia Asha]]
[[Category:Nokia Asha Platform 1.0]]

Series 40: [[Category:Series 40]]
[[Category:Series 40 1st Edition]] [[Category:Series 40 2nd Edition]]
[[Category:Series 40 3rd Edition (initial release)]] [[Category:Series 40 3rd Edition FP1]] [[Category:Series 40 3rd Edition FP2]]
[[Category:Series 40 5th Edition (initial release)]] [[Category:Series 40 5th Edition FP1]]
[[Category:Series 40 6th Edition (initial release)]] [[Category:Series 40 6th Edition FP1]] [[Category:Series 40 Developer Platform 1.0]] [[Category:Series 40 Developer Platform 1.1]] [[Category:Series 40 Developer Platform 2.0]]

Symbian: [[Category:Symbian]]
[[Category:S60 1st Edition]] [[Category:S60 2nd Edition (initial release)]] [[Category:S60 2nd Edition FP1]] [[Category:S60 2nd Edition FP2]] [[Category:S60 2nd Edition FP3]]
[[Category:S60 3rd Edition (initial release)]] [[Category:S60 3rd Edition FP1]] [[Category:S60 3rd Edition FP2]]
[[Category:S60 5th Edition]]
[[Category:Symbian^3]] [[Category:Symbian Anna]] [[Category:Nokia Belle]]

Add categories below using category selector.

852 page views in the last 30 days.
×