Customizing Windows Phone Applications using the Settings File

Introduction

Most applications have some sort of support for storing user preferences or application state. Typically, this is done in a settings file or the registry. However in the case of Windows Phone platform, there is a restriction to what operating system components a user application can access.

Windows Phone application supports the concept of isolated storage. Isolated Storage is defined as a storage mechanism that provides process isolation. Only the application that owns the isolated storage has access to the contents inside the storage.

To save user preferences or application state, a Windows Phone application can use isolated storage as the repository.

Hands-On

To see how you can use isolated storage to store application state, let's create a sample application, which saves user settings and reads from it on application startup.

Create a new Windows Phone project called WindowsPhoneSettingsDemo. Add a textbox on the MainPage and a Button titled Quit.

Add a class called ApplicationSettings to the project.

In our sample application, we will save the following information to the application settings file:

  • textBoxLastAccessTimeHour - displays the hour the application was last started.
  • textBoxLastAccessTimeMinute - displays the minute the application was last started.

Add a few properties to the ApplicationSettings to store the content of "textBoxLastAccessTimeHour" and "textBoxLastAccessTimeMinute".

Add a static object of type IsolatedStorageSettings class. We keep this static because we will access this class from multiple locations within our application.

        public static string lastAccessHourSettingName = "LastAccessHour";
        public static string lastAccessMinuteSettingName = "LastAccessMinute";
 
 
        public static IsolatedStorageSettings storedSettings = IsolatedStorageSettings.ApplicationSettings; 
 

Now add a method to save and get the value of your settings.

        public static void SaveSettings(string settingName, object value)
        {
            if (storedSettings.Contains(settingName))
            {
                storedSettings[settingName] = value;
            }
            else
            {
                storedSettings.Add(settingName, value);
            }
 
        }
 
        public static T GetSettingValue<T>(string settingName, T defaultValue)
        {
            T returnValue = defaultValue;
            if (storedSettings.Contains(settingName))
                returnValue = (T)storedSettings[settingName];
            return returnValue;
        }
 

Finally, add a method to save the settings to the isolated storage file.

        public static void SaveSettings()
        {
            storedSettings.Save();
        }

Now that the ApplicationSettings class is complete, we can focus on wiring up where we read and write the settings.

Double click the button "Quit" we added on MainPage and on the Click event handler, add the following code to save the settings.

// MainPage.xaml.cs
private void buttonQuit_Click(object sender, RoutedEventArgs e)
        {
            ApplicationSettings.SaveSettings(ApplicationSettings.lastAccessHourSettingName, DateTime.Now.Hour);
            ApplicationSettings.SaveSettings(ApplicationSettings.lastAccessMinuteSettingName, DateTime.Now.Minute);
            ApplicationSettings.SaveSettings();
        }

The above code saves the current hour and time as values for the settings.

Now, we need to write code to populate the values from the settings file. Open App.xaml.cs and add the following code in Application_Launching and Application_Activated events.

        private void Application_Launching(object sender, LaunchingEventArgs e)
        {
            lastAccessHour = ApplicationSettings.GetSettingValue<int>(ApplicationSettings.lastAccessHourSettingName, 0);
            lastAccessMinute = ApplicationSettings.GetSettingValue<int>(ApplicationSettings.lastAccessMinuteSettingName, 0);
        }
 
        public static int lastAccessHour = 0;
        public static int lastAccessMinute = 0;
 
        // Code to execute when the application is activated (brought to foreground)
        // This code will not execute when the application is first launched
        private void Application_Activated(object sender, ActivatedEventArgs e)
        {
            lastAccessHour = ApplicationSettings.GetSettingValue<int>(ApplicationSettings.lastAccessHourSettingName, 0);
            lastAccessMinute = ApplicationSettings.GetSettingValue<int>(ApplicationSettings.lastAccessMinuteSettingName, 0);
 
        }

Finally, we need to populate our textboxes with the settings value we retrieved above.

On MainPage.xaml.cs, double click the PageLoaded event and add the following code.

private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
        {
            textBoxLastAccessTimeHour.Text = App.lastAccessHour.ToString();
            textBoxLastAccessTimeMinute.Text = App.lastAccessMinute.ToString();
        }

There, we have now added support for saving and retrieving application settings from a configuration file for a Windows Phone application.

If you are having trouble following along, you can check out the code sample for this article.

Summary

In this article, we learned how to save and retrieve settings in a Windows Phone application.



About the Author

Vipul Vipul Patel

Vipul Patel is a Software Engineer currently working at Microsoft Corporation, working in the Office Communications Group and has worked in the .NET team earlier in the Base Class libraries and the Debugging and Profiling team. He can be reached at vipul_d_patel@hotmail.com

Related Articles

Downloads

Comments

  • There are no comments yet. Be the first to comment!

Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Live Event Date: October 29, 2014 @ 11:00 a.m. ET / 8:00 a.m. PT Are you interested in building a cognitive application using the power of IBM Watson? Need a platform that provides speed and ease for rapidly deploying this application? Join Chris Madison, Watson Solution Architect, as he walks through the process of building a Watson powered application on IBM Bluemix. Chris will talk about the new Watson Services just released on IBM bluemix, but more importantly he will do a step by step cognitive …

  • Live Event Date: October 23, 2014 @ 12:00 p.m. ET / 9:00 a.m. PT Despite the current "virtualize everything" mentality, there are advantages to utilizing physical hardware for certain tasks. This is especially true for backups. In many cases, it is clearly in an organization's best interest to make use of physical, purpose-built backup appliances rather than relying on virtual backup software (VBA - Virtual Backup Appliances). Join us for this eSeminar to learn why physical appliances are preferable to …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds