Hands-on experiment
Experiment 6: Settings and preferences
September 2012
Brief introduction
Experiment 3 introduces the contract and demonstrates how the application can easily integrate with shared and search contracts. The contract also contains the set Super button, which modifies the settings that are applied to the active Windows application store. In the settings pane that appears when you choose to set the Super button, the operating system provides a permission command that allows the user to enable or disable certain features of the program, such as access to the webcam and microphone. It is worth noting that you can add commands to the settings pane and connect them to the Settings page. This provides users with easy access to preferences, information about boxes and other application-specific settings.
In this experiment, you will add the about and preferences commands to the Setup pane of Contoso Cookbook. You will expose a simple user preference that can be turned on and off with a toggle switch, and you will use roaming settings to store this preference so that it moves with the user.
Goal
This experiment will show you how to:
Add the About command and the about page to the Settings pane.
Add the Preferences (Preferences) command and preferences page to the Settings pane.
Use roaming settings to store user preferences.
System Requirements
You need the following software to complete this experiment:
Microsoft Windows 8
Microsoft Visual Studio 2012
Set up
You must perform the following steps to prepare the computer for this experiment:
1. Install Microsoft Windows 8.
2. Install Microsoft Visual Studio 2012.
Practice
This hands-on experiment includes the following exercises:
1. Add About Page
2. Add Preferences page
3. Implementation Preferences
Estimated time of completion of this experiment: 30-40 minutes.
Exercise 1: Add about pages
In this exercise you will add a simple about page to Contoso Cookbook. You will use the Settingsflyout class in the Callisto library that you added in experiment 4 to work on the page.
Task I add about commands
The first step is to add an about command to the Settings menu, which we will do by handling the settingspane.commandsrequested event.
1, open the Contosocookbook project that you completed in experiment 4 in Visual Studio. If you have not completed experiment 4 or want to start with a reference copy, you can find the completed version of the experiment in the start material.
2, open App.xaml.cs and add the following using statement.
C#
Using Windows.UI.ApplicationSettings;
Using Callisto.controls;
Using Windows.ui;
3, add the following fields to the App class.
C#
Private Color _background = Color.FromArgb (255, 0, 77, 96);
4. In the Onlaunched method, add the following statement immediately after the statement that registers the event handler for the Suggestionsrequested event.
C#
Registering an event handler for the Commandsrequested event in the Settings pane
Settingspane.getforcurrentview (). commandsrequested + = oncommandsrequested;
5, add the same statement to the Onsearchactivated method (once again next to the statement that registered the event handler for the Suggestionsrequested event) to ensure that even if the application is activated from the Windows 8 Search pane, The Commandsrequested event will also be processed.
6, add the following event handlers to App.xaml.cs.
C#
void oncommandsrequested (Settingspane sender, Settingspanecommandsrequestedeventargs args)
{
//Add about command
var about = new Settingscommand ("about", "about", (handler) =>
{
var settings = new Settingsflyout ();
Settings. Content = new Aboutusercontrol ();
Settings. Headerbrush = new SolidColorBrush (_background);
Settings. Background = new SolidColorBrush (_background);
Settings. HeaderText = "about";
Settings. IsOpen = true;
});
Args. Request.ApplicationCommands.Add (about);
}
Note: Here you add a command to the settings menu by adding a Settingscommand object to the Applicationcommands collection passed to the Commandsrequested event. The third parameter of the Settingscommand constructor is the handler that is invoked when the command is invoked. In this example, you use the Settingsflyout class in Callisto to display about the page from a handler. Of course, the page has not yet been created. You will create it in the next task.
See more highlights of this column: http://www.bianceng.cnhttp://www.bianceng.cn/Programming/net/