Over the next several posts I’ll show you how to work with local data on the Windows Phone 8 platform and compare it to working with data on the Android platform.
In Android development, if you have a small collection of key-value pairs that you want to persist from session to session you use the SharedPreferences APIs. In Windows Phone development these key-value pairs are often referred to as Application Settings.
To save or retrieve data as a key-value pair in Windows Phone 8 you use theIsolatedStorageSettings class. The IsolatedStorageSettings class is nothing more than a dictionary that enables you to store any serializable object, such as a string, and is ideally suited for saving small pieces of data. Data stored with the IsolatedStorageSettings class is persisted in the app’s local folder when the app is closed or deactivated and automatically populated with previously persisted data when the app is launched.
|The SharedPreferences class in Android is similar to the IsolatedStorageSettings class in Windows Phone apps.|
|Windows phone tip|
|Since application settings are tied to the lifetime of the app on a device, you should not use application settings to store information your users might thing as valuable or irreplaceable. You should consider using the user’s libraries or SkyDrive for permanent data.|
It is important to note that once an application is removed, all of its data stores, including those associated with settings are deleted from the device.
Let’s now look at how you write and read application settings.
To create a Shared Preference in Android a key of Name and a value of Me you would write code similar to the following:
SharedPreferences sharedPref = getActivity().getPreferences(Context.MODE_PRIVATE); SharedPreferences.Editor editor = sharedPref.edit(); editor.putString("Name", "Me"); editor.commit();
|In Android you can create preference files using MODE_WORLD_READABLE andMODE_WORLD_WRITEABLE which allows other apps that know your file identifier to access you data. In Windows Phone applications are sandboxed and their settings cannot be accessed by other apps.|
To create a similar key-value pair in a Windows Phone app you would write the following:
var localSettings = IsolatedStorageSettings.ApplicationSettings; localSettings.Add("Name", "Me");
Reading an Application Setting
To retrieve a Shared Preference in Android with a key of Name and a default value of Me (i.e. the value returned if the key is not found) you would write the following:
SharedPreferences sharedPref = getActivity().getPreferences(Context.MODE_PRIVATE); string nameValue = sharedPref.getString("Name", "Me");
To perform a similar operation on the settings store in a Windows Phone app you would write:
var localSettings = IsolatedStorageSettings.ApplicationSettings; string nameValue = (string) localSettings["Name"];
Deleting an Application Setting
To remove a Shared Preference in Android you use the following code:
SharedPreferences sharedPref = getActivity().getPreferences(Context.MODE_PRIVATE); SharedPreferences.Editor editor = sharedPref.edit(); editor.remove("Name"); editor.commit();
To delete a setting from a Windows Phone 8 app you would use:
var localSettings = IsolatedStorageSettings.ApplicationSettings; localSettings.Remove("Name");
|WINDOWS PHONE HINT|
|Do not be tempted to use the LocalSettings and/or RoamingSettings properties of theApplicationData class in place of the ApplicationSettings property of theIsolatedStorageSettings class. While the API’s are available, they have not been implemented in Windows Phone 8. An exception will be thrown if they are called.|