Isolated Storage in WP8 and WP8.1 RT

We have been using IsolatedStorageSettings in our Windows Phone 8 App from a long time.

IsolatedStorageSettings is basically a Key-Value pair mechanism used to store small amounts of data. The data stored are mostly settings or some smaller data related to user or the application. IsolatedStorageSetting has been an integral part of the app’s life cycle since it has always been used to store and retrieve the data.

With the advent of Windows Phone 8.1, we no longer have access to IsolatedStorageSettings class and it’s namespace when creating RT based app. So keeping this in mind, I am writing this post to assist developers who are trying to migrate their app or creating a new WP8.1 RT based app.

From my previous post at ProgramFreaks, I am fetching some of the examples which will make it easier to understand the use of IsolatedStorage. Let’s look at the code below for WP8-Silverlight

// Method to Save data in IsolatedStorageSettings
private void SaveData()
{
    IsolatedStorageSettings settings = IsolatedStorageSettings.ApplicationSettings;
    //Assuming you have the variable declared in App.Xaml.cs.
    if (!settings.Contains("userName"))
    {
        settings.Add("userName", App.UserName);
    }
    else
    {
        settings["userName"] = App.UserName;
    }
    settings.Save();
}
// Method to Load data from IsolatedStorageSettings
private void LoadData()
{
    if (IsolatedStorageSettings.ApplicationSettings.Contains("userName"))
    {
        //Assuming you to have the variable present in App.Xaml.cs
        App.UserName = IsolatedStorageSettings.ApplicationSettings["userName"] as string;
    }
}

In the above code, we have two methods which are called in the event handlers Application_Deactivated, Application_Closing and Application_Launching, Application_Activated.

On thing to notice is that we always try to store the data as part of ApplicationSettings. Now we will try to do the same in WP8.1 RT.

Let’s look at the code below and see the defference.

private void SaveData()
{
    ApplicationDataContainer settings = Windows.Storage.ApplicationData.Current.LocalSettings;
    //Assuming you have the variable declared in App.Xaml.cs.
    settings.Values["userName"] = App.UserName;
}
// Method to Load data from IsolatedStorageSettings
private void LoadData()
{
    ApplicationDataContainer settings = Windows.Storage.ApplicationData.Current.LocalSettings;
    if (settings.Values.Count <= 0) return;
    {
        //Assuming you to have the variable present in App.Xaml.cs
        App.UserName = settings.Values.ContainsKey("userName")? settings.Values["userName"] as String : String.Empty;
    }
}

In the above code, we have used Windows.Storage.ApplicationData.Current.LocalSettings instead of System.IO.IsolatedStorageSettings.ApplicationSettings. The working principle is almost the same. This also makes use of Key-Value pair mechanism like Dictionary. Although, for RT there is a storage limit in the ApplicationDataContainer.

The name of each setting can be 255 characters in length at most. Each setting can be up to 8K bytes in size and each composite setting can be up to 64K bytes in size.

I am working on a sample to show this. It will be available soon.

26-Aug-2014: Update: Sample is done. You can get the sample at https://github.com/anubhavranjan/WPIsoStorage

Anubhav Ranjan
Latest posts by Anubhav Ranjan (see all)

Leave a Reply