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

  • On-demand Event Event Date: December 18, 2014 The Internet of Things (IoT) incorporates physical devices into business processes using predictive analytics. While it relies heavily on existing Internet technologies, it differs by including physical devices, specialized protocols, physical analytics, and a unique partner network. To capture the real business value of IoT, the industry must move beyond customized projects to general patterns and platforms. Check out this webcast and join industry experts as …

  • Today's agile organizations pose operations teams with a tremendous challenge: to deploy new releases to production immediately after development and testing is completed. To ensure that applications are deployed successfully, an automatic and transparent process is required. We refer to this process as Zero Touch Deployment™. This white paper reviews two approaches to Zero Touch Deployment--a script-based solution and a release automation platform. The article discusses how each can solve the key …

Most Popular Programming Stories

More for Developers

RSS Feeds