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")]
    public string MailServer { get; set; }

    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 = "";

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 ( 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


No comments yet.