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.

More by Author

Get the Free Newsletter!

Subscribe to Developer Insider for top news, trends & analysis

Must Read