Save user application specific settings in Windows Phone 7

Hi,

Lets look at this scenario, you’re developing a facebook enabled application for Windows Phone 7. and what facebook returns is the AccessToken (which is of type string) and Expires (when is that access token going to expire and this is of type DateTime). Or lets keep this simple. Suppose you have an application which displays tips (Fantastic Facts, OMG Tips or any such). and application has feature wherein a user can turn off tips (if tips keep bugging him now and then).  So you end up in saving some application settings that are specific to your application and the user. So Here’s a quick code snippet how you can accomplish any of these.

Well this is one of the features provided by the Phone Framework, and is also known as IsolatedStorageSettings. Isolated Storage is one of the features of the phone, where an application can save data to the flash drive of the phone. Its known as Isolated Storage because One Application cannot access another applications IsolatedStorage (just because of Security Issues). Now you have a property of IsolatedStorgaeSettings class called ApplicationSettings which returns IsolatedStorageSettings Object for the particular application and you can save user settings from there. Here a quick code sample.

using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.IO.IsolatedStorage;

namespace Project.Phone.Settings
{
    public class Settings
    {
        // Our isolated storage settings
        IsolatedStorageSettings isolatedStore;

        // The isolated storage key names of our settings
        const string FbAccessTokenKeyName = "FBAccessToken";
        const string FbAccessTokenExpiresKeyName = "FBAccessTokenExpires";

        // The default value of our settings
        const string FbAccessTokenDefault = "";
        DateTime FbAccessTokenExpiresDefault = DateTime.Now.AddDays(-30); //Datetime value that already expired. This is default.

        ///
 /// Constructor that gets the application settings. /// 

        public Settings()
        {

                // Get the settings for this application.
                isolatedStore = IsolatedStorageSettings.ApplicationSettings;

        }

        ///
 /// Update a setting value for our application. If the setting does not /// exist, then add the setting. /// 

        ///
        ///
        ///
        public bool AddOrUpdateValue(string Key, Object value)
        {
            bool valueChanged = false;

            // If the key exists
            if (isolatedStore.Contains(Key))
            {
                // If the value has changed
                if (isolatedStore[Key] != value)
                {
                    // Store the new value
                    isolatedStore[Key] = value;
                    valueChanged = true;
                }
            }
            // Otherwise create the key.
            else
            {
                isolatedStore.Add(Key, value);
                valueChanged = true;
            }

            return valueChanged;
        }

        ///
 /// Get the current value of the setting, or if it is not found, set the /// setting to the default setting. /// 

        ///
        ///
        ///
        ///
        public valueType GetValueOrDefault(string Key, valueType defaultValue)
        {
            valueType value;

            // If the key exists, retrieve the value.
            if (isolatedStore.Contains(Key))
            {
                value = (valueType)isolatedStore[Key];
            }
            // Otherwise, use the default value.
            else
            {
                value = defaultValue;
            }

            return value;
        }

        ///
 /// Save the settings. /// 

        public void Save()
        {
            isolatedStore.Save();
        }

        ///
 /// Property to get and set Facebook AccessToken /// 

        public string FacebookAccessToken
        {
            get
            {
                return GetValueOrDefault(FbAccessTokenKeyName, FbAccessTokenDefault);
            }
            set
            {
                AddOrUpdateValue(FbAccessTokenKeyName, value);
                Save();
            }
        }

        ///
 /// Property to get and set Facebook Access Token Expires value /// 

        public DateTime FacebookAccessTokenExpires
        {
            get
            {
                return GetValueOrDefault(FbAccessTokenExpiresKeyName, FbAccessTokenExpiresDefault);
            }
            set
            {
                AddOrUpdateValue(FbAccessTokenExpiresKeyName, value);
                Save();
            }
        }

    }
}

Now,We’re going to use this Settings class to store and retrieve application settings.
In any part of the project where you need to enable this functionality. Just type in this code as its shown below.

                Project.Phone.Settings.Setting = new Project.Phone.Settings.Settings();
                     Setting.FacebookAccessToken = result.AccessToken;
                     Setting.FacebookAccessTokenExpires = result.Expires;

Note : IsolatedStorageSettings.ApplicationSettings throws a System.IO.IsolatedStorage.IsolatedStorageException or InvalidOperationException when the application is closed. This is reported as a bug in Windows Phone 7.0 (Your spps still get approved in the marketplace if they end up throwing this exception) and is fixed in Windows Phone 7.1 Mango Release.

Hope this quick tip helps!.. :)

About these ads

About hotcomputerworks

I love writing code. I love building better products for people. I want to change the way the world think about technology. I’ve been with Java for 2 years and Now I’m into .NET Platform. I love .NET just because of its productivity. Building better projects in less time,Competing in Imagine Cup and other Global Student Technology Contest. I’m a Microsoft Student Partner (MSP) who connects my college mates to technology and Microsoft. I breathe technology day in and day out and I currently work for myself and am the founder of IntraDevelop Softworks – a small company based in India. I’m Nawaz Dhandala. Follow me on Twitter @hotcomputerwork
This entry was posted in C#, Microsoft .NET Framework, Windows Phone 7 and tagged , , , . Bookmark the permalink.

3 Responses to Save user application specific settings in Windows Phone 7

  1. Pingback: Save user application specific settings in Windows Phone 7

  2. Pingback: Save user application specific settings in Windows Phone 7 – www.nalli.net

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s