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.

Tip To prevent the app bar from appearing, you can do one of two things. First, to prevent the appbar<br />

from appearing at all (for any gesture), set the app bar’s element’s winControl.disabled property to<br />

true. Second, if you want to prevent it for, say, a right-click on a particular element (such as a canvas),<br />

listen to the contextmenu (right click) event for that element and call eventArgs.preventDefault()<br />

within your handler.<br />

For apps written in <strong>HTML</strong> and JavaScript, the app bar control is implemented as a WinJS control:<br />

WinJS.UI.AppBar. As with all other WinJS controls, you declare an app bar in <strong>HTML</strong> and instantiate it<br />

with a call to WinJS.UI.process or WinJS.UI.processAll. For a first example, we don’t need to look any<br />

farther than some of the Visual Studio/Blend project templates like the Grid App project, where a<br />

placeholder app bar is included in default.html (initially commented out):<br />

<br />

<br />

<br />

<br />

The super-exciting result of this markup, using the ui-dark.css stylesheet, is as follows:<br />

Because the app bar is declared in default.html, which is the container for all other page controls, this<br />

same app bar will apply to all the pages in the app. With this approach you can declare all your<br />

commands within a single app bar and assign different classes to the commands that allow you to easily<br />

show and hide command sets as appropriate for each page. This also centralizes those commands that<br />

appear on multiple pages, and you can wire up event handlers for them in your app’s primary activation<br />

code (such as that in default.js).<br />

Alternately, you can declare an app bar within the markup for individual page controls. Since an app<br />

bar will still be in the DOM, the Windows gestures will invoke it on each particular page. In the Grid App<br />

project, for example, you can move the markup above from default.html into groupedItems.html,<br />

groupDetail.html, and itemDetail.html with whatever modifications you like for each page. This might<br />

be especially useful if your app’s pages don’t share many commands in common.<br />

In these cases, each page’s ready method should take care of wiring up the commands on its<br />

particular app bar. Note also that you can add handlers within a page’s ready method even for a central<br />

app bar; it’s just a matter of calling addEventListener on the appropriate child element within that app<br />

bar.<br />

Let’s look now at how all this works through the <strong>HTML</strong> AppBar control sample. (This chapter’s<br />

companion content also has a modified version.) We’ll start with the basics and the standard<br />

command-oriented configuration for app bars, look at how to display menus for some of those<br />

commands, and then see how to create custom layouts as is used for a top navigation bar.<br />

277

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

Saved successfully!

Ooh no, something went wrong!