23.01.2018 Views

MICROSOFT_PRESS_EBOOK_PROGRAMMING_WINDOWS_8_APPS_WITH_HTML_CSS_AND_JAVASCRIPT_PDF

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

There are two ways to implement this process in an app written in <strong>HTML</strong> and JavaScript: using WinRT<br />

directly, or using the helpers in WinJS. Let’s look at these in turn for a simple Help command.<br />

To know when the charm is invoked through WinRT, obtain the settings pane object through<br />

Windows.UI.ApplicationSettings.SettingsPane.getForCurrentView and add a listener for its<br />

commandsrequested event (this is a WinRT event, so be sure to remove the listener if necessary):<br />

// The n variable here is a convenient shorthand<br />

var n = Windows.UI.ApplicationSettings;<br />

var settingsPane = n.SettingsPane.getForCurrentView();<br />

settingsPane.addEventListener("commandsrequested", onCommandsRequested);<br />

Within your event handler, create Windows.UI.ApplicationSettings.SettingsCommand objects for<br />

each command, where each command has an id, a label, and an invoked function that’s called when<br />

the command is tapped or clicked. These can all be specified in the constructor as shown below:<br />

function onCommandsRequested(e) {<br />

// n is still the shortcut variable to Windows.UI.ApplicationSettings<br />

var commandHelp = new n.SettingsCommand("help", "Help", helpCommandInvoked);<br />

e.request.applicationCommands.append(commandHelp);<br />

}<br />

The second line of code is where you then add these commands to the settings pane itself. You do<br />

this by appending them to the e.request.applicationCommands object. This object is a WinRT<br />

construct called a vector that manages a collection with commands like append and insertAt. In this<br />

case we have a vector of SettingsCommand objects, and as you can see above, it’s easy enough to<br />

append a command. You’d make such a call for each command, or you can pass an array of such<br />

commands to the replaceAll method instead of append. What then happens within the invoked handler<br />

for each command is the interesting part, and we’ll come back to that in the next section.<br />

You can also prepopulate the applicationCommands vector outside of the commandsrequested event;<br />

this is perfectly fine because your settings commands should be constant for the app. The Quickstart:<br />

add app help topic shows an example of this, which I’ve modified here to show the use of replaceAll:<br />

var n = Windows.UI.ApplicationSettings;<br />

var settingsPane = n.SettingsPane.getForCurrentView();<br />

var vector = settingsPane.applicationCommands;<br />

//Ensure no settings commands are currently specified in the settings charm<br />

vector.clear();<br />

var commands = [ new settingsSample.SettingsCommand("Custom.Help", "Help", OnHelp),<br />

new n.SettingsCommand("Custom.Parameters", "Parameters", OnParameters)];<br />

vector.replaceAll(commands);<br />

This way, you don’t actually need to register for or handle commandsrequested directly.<br />

337

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!