Free Trial

Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.

  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint
Share this Page URL
Help

5. Working with Local Data > Working with IsolatedStorageSettings

Working with IsolatedStorageSettings

Working with IsolatedStorageSettings Applies to Silverlight 3, 4, and 5 In the previous recipe, we used the IsolatedStorageFile class to manually perform the saving to and reading from isolated storage. While this option is the most powerful one—it allows us to save anything varying from raw data to images—it's quite a cumbersome task to perform all the serializing and deserializing of objects ourselves. Although we used the easy-to-work-with LINQ-to-XML, there is an even easier solution: letting Silverlight take care of this detail. In this recipe, we'll do just that by using the IsolatedStorageSettings class. Getting ready In this recipe, we are going to refactor the code from the previous recipe. You can therefore continue working on that codebase. Alternatively, you can use the starter solution provided with this book, which is located in the Chapter05/IsolatedStorageSettings_Starter folder. The complete solution for this recipe can be found in the Chapter05/IsolatedStorageSettings_Completed folder. How to do it... Instead of doing all the save and read operations manually through the use of LINQ-to-XML, we are going to make use of the IsolatedStorageSettings class that comes with Silverlight. This class offers us a Dictionary<string, object> through the ApplicationSettings property, which we can use to save and retrieve any type of object. In the following steps, we are going to refactor the previous recipe to make use of this simpler approach: As mentioned in the Getting ready section, open the starter solution or use your solution from the previous project. Start by adding a class to the Silverlight project and name it IsoStoreSettingsHelper. This class will be our wrapper around IsolatedStorageSettings.ApplicationSettings. Make the class static as follows: public static class IsoStoreSettingsHelper { } In the IsoStoreSettingsHelper class, add the following code to allow us to save objects by key: public static void SaveObject<T>(string key, T value) { IsolatedStorageSettings.ApplicationSettings[key] = value; IsolatedStorageSettings.ApplicationSettings.Save(); } To this generic method, we are passing a key and a value, which can basically be of any type we want. Inside the method, we use the ApplicationSettings property, which implements the IDictionary<string, object> to add a value to the dictionary. Finally, we call Save() to persist the changes to isolated storage.Again in the IsoStoreSettingsHelper class, add the following method to be able to retrieve a value based on a given key: public static T GetObject<T>(string key) where T : class { if (IsolatedStorageSettings.ApplicationSettings .Contains(key)) { return IsolatedStorageSettings.ApplicationSettings[key] as T; } return default(T); } This method first checks using the Contains() method if a value is present for the key. If so, the value is searched and cast to the passed-in type. If not, the default for the given type is returned.Also add the following methods to the IsoStoreSettingsHelper class to perform a delete and a check to see if a value for a given key is present: public static void DeleteObject(string key) { if (IsolatedStorageSettings.ApplicationSettings .Contains(key)) { IsolatedStorageSettings.ApplicationSettings.Remove(key); } } public static bool ContainsObject(string key) { if (IsolatedStorageSettings.ApplicationSettings .Contains(key)) { return true; } return false; } Let's now look at the code behind the CityView class, CityView.xaml.cs. We'll change this class to use our newly-created wrapper class instead of doing the persisting itself. Up first is the LoadAllCities() method, which is shown next. As can be seen, this method now uses ContainsObject from IsoStoreSettingHelper to check for the presence of a value for the key Cities. If this method returns true, the value is retrieved using the GetObject() method. private void LoadCities() { if (IsoStoreSettingsHelper.ContainsObject("Cities")) { CityDataGrid.ItemsSource = IsoStoreSettingsHelper. GetObject<ObservableCollection<City>>("Cities"); } else { LoadCitiesFromService(); } } The callback from the LoadCitiesFromService, proxy_GetCitiesCompleted()can also be changed. Instead of saving to XML, it can now use the IsoStoreSettingsHelper to save the cities. This is shown in the following code: void proxy_GetCitiesCompleted (object sender, GetCitiesCompletedEventArgs e) { if (e.Error == null) { CityDataGrid.ItemsSource = e.Result; IsoStoreSettingsHelper.SaveObject <ObservableCollection<City>>("Cities", e.Result); } } Finally, we can change the refresh functionality so that it uses the DeleteObject() method as follows: private void RefreshCitiesButton_Click (object sender, RoutedEventArgs e) { using (var isoStore = IsolatedStorageFile.GetUserStoreForApplication()) { IsoStoreSettingsHelper.DeleteObject("Cities"); } LoadCities(); } With this code in place, we are now letting Silverlight take care of saving and retrieving the data from isolated storage using IsolatedStorageSettings.ApplicationSettings. In the completed solution, you can also see the use of this class in the code-behind for the HotelView page. How it works... While IsolatedStorageFile allows us to write and read any file from and to isolated storage, it can be a lot of work to do this task ourselves. As shown in the previous recipe, we as developers needed to take care of serialization of the data. For this purpose, the IsolatedStorageSettings.ApplicationSettings exists. With it, we get access to an IDictionary<string, object> that's being persisted automatically for us to isolated storage. Silverlight itself takes care of saving the object that we pass it and it associates the object with a key. This key can then later be used to retrieve the value again. Behind the scenes, Silverlight is creating a text file named __LocalSettings where the data can be read. This file is in the same location as where the data in isolated storage is being stored. As this is again a plain text/XML file, it's not a best practice to store sensitive data such as passwords in the IsolatedStorageSettings. See also In the previous recipe, we looked at working with the IsolatedStorageFile which shows another way of working with isolated storage.

  

You are currently reading a PREVIEW of this book.

                                                                                                                    

Get instant access to over $1 million worth of books and videos.

  

Start a Free Trial


  
  • Safari Books Online
  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint