Application Preferences
Preferences are application-specific settings that define the look and feel and behavior of an application. The user can update the preferences at any time to change the behavior of the application.
Both iPhone and Windows Phone provide easy means to update application settings. They both follow a similar philosophy of ease of use and simplicity. However, they follow different ways for presenting and implementing application preferences.
iPhone Application Preferences
On the iPhone, applications have two options for presenting preferences.
■ Display preferences inside the application.
■ Manage preferences from the system wide Settings application.
While the developer can use either mechanism to implement the preferences, the second way to manage application preferences via the Settings application is the preferred one. If the developer expects that the settings will be updated frequently, it may be better to include the preferences within the application. Developers may use both mechanisms.
The Settings application implements a hierarchical set of pages for managing application preferences. The main page shows the system preferences and links to the pages for third party applications. Selecting the application preferences link takes the user to the preferences page for that application. We will look at how to migrate the iPhone application preferences presented using the preferred way, i.e. via the Settings application.
Windows Phone 7 Application Preferences
Windows Phone 7 provides a Settings application for system and system application settings. In contrast to iPhone, third party application settings cannot be placed within this system Settings application. Application settings must be part of the application. This provides consistency and ease of use; users are not confused about where to look for the settings and avoids having to look for settings in both central settings application and the application pages.
Windows Phone 7 UI Design and Interaction Guide recommends that developers keep the application settings brief and clear to improve the usability, and also that they avoid complex multi-level, multi-page application settings. As such, the application should be designed for the most common use. If the application has several user-selectable settings, it should create a separate settings page within the application. This allows the user to update the settings without leaving the application.
Changes to application settings should be applied immediately without confirmation. This means that confirmation dialog such as "OK" is not needed. However, you may inform the user that the settings have been updated. Any settings that that cannot be undone or overwrite or delete data should allow the user to opt out by canceling the change.
Comparison between the two platforms
The following table provides the comparison between the two platforms.
Purpose |
iPhone |
Windows Phone 7 |
Application Preferences |
System wide settings application In-Application preference pages |
In Application preference page |
Preferred/Required System wide settings application |
In-app preferences required |
|
Preference UI |
Declarative syntax for System wide Settings Developer implements a page for in-app preferences |
Developer implements in-app page for in-app preferences |
Storage mechanism Application specific file store |
IsolatedStorage |
|
Preferences Saved as key-value pairs Saved as key- value pairs |
Migrating Application Preferences
Overview of iPhone Application Preferences
iPhone provides a Settings bundle to manage preferences from the Settings application. The Settings bundle consists of Root.plist and other .plist files as well as files that store localized string resources. The system uses the .plist files to present the UI for the application preference page. The Settings page .plist file consists of specifications for preference parameter types. For the preferences included within the application, the developer needs to implement the view to access and manipulate the preferences.
Regardless of how the application preferences are presented to the user, the application developer uses NSUserDefaults class to access the preferences from the code. The NSUserDefaults class provides a dictionary to store key-value pairs which are persisted in the .plist file in the application-specific file store.
Windows Phone 7 Application Preferences Application Preference UI
In Windows Phone 7 the developer needs to implement the pages for accessing and manipulating application preferences. The settings pages are no different from any other application page and one can use all available Windows Phone UI widgets to manipulate the preferences.
Let us look at the types of iPhone application settings and how they can be migrated to Windows Phone 7. The following table shows iPhone preference types and the types of controls. The right hand column shows the corresponding Windows Phone 7 controls that can be used to migrate them.
iPhone Preference control |
1 1 1 |
Purpose |
|
Corresponding Window Phone Control type |
|
Text Field |
PSTextFieldSpecifier |
Editable text field for string |
TextBox |
||
parameter |
|||||
Title |
PSTitleValueSpecifier |
Read-only string value |
TextBlock |
] |
|
Toggle switch |
PSToggleSwitchSpecifier |
Preference that can have |
Checkbox |
||
only two values |
|||||
Slider |
PSSliderSpecifier |
Preference that represents |
Slider |
||
range of values |
|||||
Multi-value |
PSMultiValueSpecifier |
Selection of one value from a list |
RadioButtons or ListBox |
||
Group |
PSGroupSpecifier |
Organize collection of preferences together |
StackPanel, Grid, or Table |
||
ChildPane |
PSChildPaneSpecifier |
Navigate to a different page |
Button, link to navigate |
||
of preferences |
Persisting User Preferences
Windows Phone 7 uses isolatedStorageSettings class. IsolatedStorageSettings is implemented using IsolatedStorage which provides complete isolation between applications. IsolatedStorage provides safety as one application cannot access settings of other applications or affect other applications. IsolatedStorageSettings class provides a dictionary to store preferences as key value pairs.
Application preferences presented via the preference page can be written to IsolatedStorage. The following code snippet shows how to write the settings to IsolatedStorage. All application preferences are stored as key-value pairs where the key represents the name of the preference.
On the iPhone, the Settings application handles the persisting of application settings to the application defaults database. The defaults database is provided via the on-device .plist file. However, if your application presents preferences via application pages, you will see that the Windows Phone 7 isolated storage is similar to NSUserDefaults.
Reading Application Preferences
iPhone uses NSUserDefaults to access application preferences. Here is a typical code snippet to access a preference.
The corresponding code snippet for retrieving application settings is shown below. As you can see, the iPhone application retrieval code can be migrated easily to Windows Phone 7.
Migration Sample
Let us look at migrating simple application preferences from iPhone to Windows Phone 7. In our ReadPoetry application, there are only two preferences, namely, font size and night mode. In the normal mode, the application uses black fonts on a white background whereas in the night mode it uses white fonts on a black background.
Create Initial Application
Open Visual Studio 2010 for Windows Phone and create a new application using Windows Phone Application template. Name it ReadingPoetry and click OK.
Open MainPage.xaml using Solution Explorer. Right click on the application title and select view XAML to edit the title to "READING POETRY". Similarly, edit the Page Title to "jabberwocky". Your TitlePanel XAML should look like following:
In the ContentPanel Grid, add the following TextBlock with the poem in it (with all those LineBreaks). Your XAML should now look like this:
You can now run your application with ‘F5′ to see the application display first two stanzas of Jabberwocky.
Add Application Bar
We will first add an icon for application settings to our application. In Solution Explorer, right click ReadingPoetry project and select Add followed by New Folder and rename it to Images. Using Windows Explorer, find your Windows Phone SDK Icons directory (typically at C:\Program Files (x86)\Microsoft SDKs\Windows Phone\v7.0\Icons) and copy appbar.feature.settings.rest.png to newly created Images folder from the "dark" subdirectory.
Right click on appbar.feature.settings.rest.png in Solution Explorer and select Properties. Change the Build Action to "Content" and Copy to Output Directory to "Copy Always."
Now we will add an Application Bar to our application. Uncomment XAML underneath "<!–Sample code showing usage of ApplicationBar–>" to add the application bar. Delete the Second ApplicationBarIconButton as well as the entire ApplicationBar.MenuItems node. Update the IconUri for the first ApplicationBarIconButton to "/Images/appbar. feature. settings .rest, png" .
Hit F5 to run the application again and now it should show the poem along with the application bar at the bottom as shown below. Of course, if you click on the settings icon, nothing will happens which is expected.In the next section we will create an event handler for it.