Custom App Preferences in Windows Phone 8 C#

Custom App Preferences in Windows Phone 8 C#

I wanted to be able to determine the volume state of the Windows Phone 8 device, and searching around on the internet proved useless, not to mention the fact there’s a couple of settings in the SDK that look like they might be what I’m looking for, but documentation and blogs on Windows 7.1 say it’s not possible, so I’m not getting my hopes up. If anyone knows of a way to determine if a Windows Phone 8 device is set to vibrate or silent vs. not (in C#) please post a comment!

In the meantime I added a button to the app using IsolatedStorageSettings. This can be used for virtually anything you want to store locally without having to use a database or other config or settings file. I implemented this like so:

try
{
    muted = Convert.ToBoolean(IsolatedStorageSettings.ApplicationSettings["Key"]);
}
catch
{
    IsolatedStorageSettings.ApplicationSettings["Key"] = 0;
    IsolatedStorageSettings.ApplicationSettings.Save();
}

Just substitute your key name where you see the word “Key” above. This gave me the ability to toggle the app’s sound settings on click by doing something like this on the “Tap” event:

private void ChangeSound(object sender, System.Windows.Input.GestureEventArgs e)
{
    if (IsolatedStorageSettings.ApplicationSettings[Key].ToString() == "0")
    {
        IsolatedStorageSettings.ApplicationSettings["Key"] = 1;
        IsolatedStorageSettings.ApplicationSettings.Save();
        Sound.Source = new BitmapImage(new Uri("Assets/soundoff.png", UriKind.Relative)); 
        muted = Convert.ToBoolean(IsolatedStorageSettings.ApplicationSettings["Key"]);
    }
    else
    {
        IsolatedStorageSettings.ApplicationSettings["Key"] = 0;
        IsolatedStorageSettings.ApplicationSettings.Save();
        Sound.Source = new BitmapImage(new Uri("Assets/soundon.png", UriKind.Relative));
        muted = Convert.ToBoolean(IsolatedStorageSettings.ApplicationSettings["Key"]);
    }
}

Now, you may be wondering if there’s a class to handle many settings? Well, I’m not aware of anything built into the SDK, but I did find something that could help. A StackOverflow post revealed a simple class that handles this fairly well:

public static class AppSettings
{
    private static IsolatedStorageSettings Settings = System.IO.IsolatedStorage.IsolatedStorageSettings.ApplicationSettings;

    public static void StoreSetting(string settingName, string value)
    {
        StoreSetting(settingName, value);
    }

    public static void StoreSetting(string settingName, TValue value)
    {
        if (!Settings.Contains(settingName))
            Settings.Add(settingName, value);
        else
            Settings[settingName] = value;
        Settings.Save();
    }

    public static bool TryGetSetting(string settingName, out TValue value)
    {            
        if (Settings.Contains(settingName))
        {
            value = (TValue)Settings[settingName];
            return true;
        }

        value = default(TValue);
        return false;
    }
}

Thanks to Jacob for that one! Hope this helps

Comments are closed.