Settings Provider

Out of the box the phone gives us IsolatedStorageSettings.ApplicationSettings which is not too bad, but it is static, not type safe, and relies on string based keys (meaning you need to copy/paste the string keys, or preferably define constants). The settings provider makes application settings a much nicer experience!

Start off creating a settings class, inheriting from ISettings this is a marker interface only. Then you can create properties on it, and decorate them with attributes. For example:

public class MyApplicationSettings : ISettings
{
    [DisplayName("Mail Server")]
    [DefaultValue("http://example.com")]
    public string MailServer { get; set; }

    [DefaultValue(2)]
    public int NumberOfWeeksToSync { get; set; }
}


To load or save your settings:

     var settingsProvider = new SettingsProvider();
     var settings = settingsProvider.Load<MyApplicationSettings>();
     //settings.NumberOfWeeksToSync == 2

    settings.MailServer = "http://example2.net";
    settingsProvider.Save(settings);


You can have as many settings classes as you want, so feel free to break settings up into logical groupings.

Settings will also be cached in the SettingsProvider to save reloading values that haven't changed from Iso storage, this means currently if you are using multiple instances of SettingsProviders then you may get stale settings.

Note currently the settings provider only supports IConvertable types (http://msdn.microsoft.com/en-us/library/dsfy6sz9.aspx). If only supporting basic types is an issue, then maybe I will use json as storage.

Last edited Jun 6, 2011 at 6:55 AM by JakeGinnivan, version 1

Comments

No comments yet.