One of the nice things about Windows 8 for developers adoption of some clearly defined (and long needed) user experience standards. While in the past we relied on conventions (Help -> About for example) those conventions were not always followed and often didn’t make a lot of sense (Help -> About for example). A great example of these UX standards are the Charms. Here we’ll take a look at the Charm you’re most likely to encounter; the Settings Charm.
The first time I worked with the Settings Charm it took a minute to figure out what was going on. Unlike the App Bar which you add to your app, you register against a Settings Charm event. It can seem a bit backwards until you realize that the Settings Charm being an part of Windows itself and therefore exists outside your application. Rather than creating the Settings Pane in your app you instead inject your commands into the existing Settings Pane.
The first step of this process is to register against the CommandsRequested event of the current Settings Pane like this:
Dissecting that line of code shows that we first grab the existing Settings Pane for our application that Windows created for us and register the SettingsPane_CommandsRequested method to the CommandsRequested event. One thing we need to be careful of however is registering against this event more than once.
To solve that we’ll use a simple Boolean to determine if we’ve registered this already:
Because we want to show these commands on every page of our application we’ll add this even to the OnLaunched event of our App.xaml.cs file.
Next we need to add our commands when the application requests them:
Finally we act on those commands being selected:
As you can see, it is pretty simple to wire this up. In an upcoming post I’ll cover adding more complex UI elements such as toggle boxes to the Settings Charm.