23.01.2018 Views

MICROSOFT_PRESS_EBOOK_PROGRAMMING_WINDOWS_8_APPS_WITH_HTML_CSS_AND_JAVASCRIPT_PDF

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

• WinJS.UI.Pages is a higher-level API intended for general use and is employed by the<br />

templates. Think of this as a generic wrapper around the fragment loader that lets you easily<br />

define a “page control”—simply an arbitrary unit of <strong>HTML</strong>, <strong>CSS</strong>, and JS—that you can easily pull<br />

into the context of another page as you do other controls. 23 They are, in fact, implemented like<br />

other controls in WinJS (as we’ll see in Chapter 4), so you can declare them in markup, instantiate<br />

them with WinJS.UI.process[All], use as many of them within a single host page as you like,<br />

and even nest them. See Scenario 1 of the <strong>HTML</strong> Page controls sample for examples.<br />

These APIs provide only the means to load and unload individual pages—they pull <strong>HTML</strong> in from<br />

other files (along with referenced <strong>CSS</strong> and JS) and attach the contents to an element in the DOM. That’s<br />

it. To actually implement a page-to-page navigation structure, we need two additional pieces:<br />

something that manages a navigation stack and something that hooks navigation events to the<br />

page-loading mechanism of WinJS.UI.Pages.<br />

For the first piece, you can turn to WinJS.Navigation, which through about 150 lines of CS101-level<br />

code supplies a basic navigation stack. This is all it does. The stack itself is just a list of URIs on top of<br />

which WinJS.Navigation exposes state, location, history, canGoBack, and canGoForward properties. The<br />

stack is manipulated through the forward, back, and navigate methods, and the WinJS.Navigation<br />

object raises a few events—beforenavigate, navigating, and navigated—to anyone who wants to listen<br />

(through addEventListener). 24<br />

For the second piece, you can create your own linkage between WinJS.Navigation and<br />

WinJS.UI.Pages however you like. In fact, in the early stages of app development of Windows 8, even<br />

prior to the first public developer preview releases, people ended up writing just about the same<br />

boilerplate code over and over. In response, the team at Microsoft responsible for the templates<br />

magnanimously decided to supply a standard implementation that also adds some keyboard handling<br />

(for forward/back) and some convenience wrappers for layout matters. Hooray!<br />

This piece is called the PageControlNavigator. Because it’s just a piece of template-supplied code<br />

and not part of WinJS, it’s entirely under your control, so you can tweak, hack, or lobotomize it however<br />

you want. 25 In any case, because it’s likely that you’ll often use the PageControlNavigator in your own<br />

apps, let’s look at how it all works in the context of the Navigation App template.<br />

Note Additional samples that demonstrate basic page controls and navigation, along with handling<br />

session state, can be found in the following SDK samples: App activate and suspend using WinJS (using<br />

the session state object in a page control), App activated, resume and suspend (described earlier; shows<br />

using the suspending deferral and restarting after termination), and Navigation and navigation history.<br />

23 If you are at all familiar with user controls in XAML, this is the same idea.<br />

24 The beforenavigate event can be used to cancel the navigation, if necessary. Either call args.preventDefault (args<br />

being the event object), return true, or call args.setPromise where the promise returns true.<br />

25 The Quickstart: using single-page navigation topic also shows a clever way to hijack <strong>HTML</strong> hyperlinks and hook them into<br />

WinJS.Navigation.navigate. This can be a useful tool, especially if you’re importing code from a web app.<br />

122

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

Saved successfully!

Ooh no, something went wrong!