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

Migrating WP8 Silverlight based App to WP8.1 RT

From past one week I was working on migrating my app TV Shows Reminder. To be frank, my findings are, On the XAML side, I can say I have made almost 50% changes where as in the Code Behind, it was almost 40% changes. So a message to all the Devs, just go ahead and start migrating your apps. It doesn’t take a lot to migrate.

TV Shows Reminder is an App that works with the site http://reminders.programfreaks.com where you can search for your favorite TV Shows and then subscribe it right after you are logged in. The site will then send you an email whenever a new episode for the subscribed show is out!

Before migrating, you should list down all the features that your current app has. This will allow you to keep track of the features that you need to migrate. So let’s see all the features that I can think of before migrating.

  • Isolated Storage
  • Social Authentication
    • Facebook
    • Google
    • Microsoft
  • Live Tiles
    • Primary Tiles
    • Secondary Tiles
  • Background Agent
  • Lock screen Notification
  • Web APIs
  • WP Toolkit (Hub Tiles)
  • Appointments
  • System Tray
  • Message Box
  • Device Properties

Since my project architecture contained PCL, it was easier for me to migrate most of my code.

In order to migrate my app from Silverlight to RT, we tend to create a new WP 8.1 RT project.
Once we create the project, for making our task easier we simply start copying and start renaming the files. What we always forget is that the Xaml makes use of the code behind and sometimes we miss updating the references for the class file. Namespaces and class names are very important in this aspect as you entire application is dependent on them. In case you are making use of ReSharper, please suspend it at the moment. Since it confuses the developer with improper suggestions and warnings.

In order to achieve this, I added a new Pivot App (Windows Phone) Project.

Project Template in VS2013

Project Template in VS2013

Once, you add this project, it will automatically create a blank page which will help you in understanding the UI changes that are needed for your App to work properly. Also, you can reuse the newly created page for faster development.

In case you are copying and pasting your .xaml or the .cs code, make sure to update your class references properly.

Let’s look at some of the changes I had done for my app to run. Basically these changes were nothing but updating the class names and their respective namespaces. We will look at some of them below.

My App was also making use of the HubTiles which is present in the WPToolkit. Because the platform is now WinRT, I had to update my layout to display some other controls like Telerik Controls. Thankfully, Telerik provides free controls to MVPs and it has some controls which can be used as a replacement for my HubTile. Otherwise, I will have to make use of some images and text over it.

Also, my app has social authentication using Facebook, Google and Microsoft. In the current scenario, for Facebook , I was making use of the Facebook SDK. This SDK was solely built for Silverlight based apps. Since, all of the providers support OAuth, so I have switched all my authentication to make use of the WebAuthenticationBroker. It makes it easier to migrate the authentication for WP8.1 RT.

As discussed above, I am making use of the IsolatedStorageSettings to store the user’s data for the App. Since, in RT, IsolatedStorage is not available, I had to make use of the LocalSettings and had to store the images in the App Area.

For the live tiles and secondary tiles, I had to update my entire code since tiles in RT have completely different set of implementations than on Silverlight.

I am yet to migrate my Background Agent since it has completely different set of implementations in RT.

Let’s conclude here and look at some of the points to remember

  • Make sure you have updated your namespace and class names.
    • If missed, can give errors in InitializeComponent()
    • Many members of page would not be available
  • It is always better to reuse the xaml given in the new project than just copy pasting the entire code from earlier project.
    • Allows you to make use of the correct markup
    • Can easily view the similarity between the RT and Silverlight markup, thus allowing safe pasting in the xaml
  • The App.xaml.cs has different methods in RT than Silverlight. When  copying methods, it is better to copy the method definition to the appropriate method.
  • Some event names have got changed in the RT, make sure to update them in respective places.
  • Many event handlers in Silverlight were using the base class EventArgs which in RT is replaced by child class RoutedEventArgs as the parameters
  • Don’t forget to check the capabilities section to update the required capabilities.
  • In case of third party controls, check their WinRT counterpart to see if you can make use of those controls in WP8.1 RT.