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

  • The 2014 State of DevOps Report — based on a survey of 9,200+ people in IT operations, software development and technology management roles in 110 countries — reveals: Companies with high-performing IT organizations are twice as likely to exceed their profitability, market share and productivity goals. IT performance improves with DevOps maturity, and strongly correlates with well-known DevOps practices. Job satisfaction is the No. 1 predictor of performance against organizational …

  • It's often said that data is the lifeblood of many businesses, yet many organizations continue to struggle with keeping their data safe -- safe from system crashes, human error, and natural disasters. There are two major factors preventing most businesses from adequately protecting their data: complexity and inefficiency. The combination of complexity and inefficiency leads to increased risk for the organization. It means more software licenses are required, specific skillsets are needed from employees, and …

Most Popular Programming Stories

More for Developers

RSS Feeds

Thanks for your registration, follow us on our social networks to keep up-to-date