lunedì 24 settembre 2012

Windows Phone - Settings Page in 5 minutes

The best user experience for a settings page is: stay away from the save button.

People don't love to press the back button and lose all settings. Then it's better to save when the user does something.

Thanks to Jerry Nixon you can use this code and have a base framework for all your applications.

    public static bool EnableLocation
    {
        get { return Get("EnableLocation", false); }
        private set { Set("EnableLocation", value); }
    }

    public static event PropertyChangedEventHandler SettingChanged;

    private static T Get<T>(string key, T otherwise)
    {
        try
        {
            return (T)IsolatedStorageSettings.ApplicationSettings[key];
        }
        catch { return otherwise; }
    }

    private static void Set<T>(string key, T value)
    {
        IsolatedStorageSettings.ApplicationSettings[key] = value;
        if (SettingChanged != null)
            SettingChanged(null, new PropertyChangedEventArgs(key));
    }

The code is very easy:
  • the "Get" function return the default value if the key don't exists.
  • the "Set" function uses an event to immediatly reflect changes into your apps.
  • the functions use "Generics".

Enjoy!

1 commento: