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.

For all other details on this function, refer to the WinJS.xhr documentation and its links to<br />

associated tutorials.<br />

Page Controls and Navigation<br />

Now we come to an aspect of Windows Store apps that very much separates them from typical web<br />

applications. In web applications, page-to-page navigation uses hyperlinks or setting<br />

document.location from JavaScript. This is all well and good; oftentimes there’s little or no state to pass<br />

between pages, and even when there is, there are well-established mechanisms for doing so, such as<br />

<strong>HTML</strong>5 sessionStorage and localStorage (which work just fine in Store apps).<br />

This type of navigation presents a few problems for Store apps, however. For one, navigating to a<br />

wholly new page means a wholly new script context—all the JavaScript variables from your previous<br />

page will be lost. Sure, you can pass state between those pages, but managing this across an entire app<br />

likely hurts performance and can quickly become your least favorite programming activity. It’s better<br />

and easier, in other words, for client apps to maintain a consistent in-memory state across pages.<br />

Also, the nature of the <strong>HTML</strong>/<strong>CSS</strong> rendering engine is such that a blank screen appears when<br />

switching pages with a hyperlink. Users of web applications are accustomed to waiting a bit for a<br />

browser to acquire a new page (I’ve found many things to do with an extra 15 seconds!), but this isn’t an<br />

appropriate user experience for a fast and fluid Windows Store app. Furthermore, such a transition<br />

doesn’t allow animation of various elements on and off the screen, which can help provide a sense of<br />

continuity between pages if that fits with your design.<br />

So, although you can use direct links, Store apps typically implement “pages” by dynamically<br />

replacing sections of the DOM within the context of a single page like default.html, akin to how<br />

AJAX-based apps work. By doing so, the script context is always preserved and individual elements or<br />

groups of elements can be transitioned however you like. In some cases, it even makes sense to simply<br />

show and hide pages so that you can switch back and forth quickly. Let’s look at the strategies and tools<br />

for accomplishing these goals.<br />

WinJS Tools for Pages and Page Navigation<br />

Windows itself, and the app host, provide no mechanism for dealing with pages—from the system’s<br />

perspective, this is merely an implementation detail for apps to worry about. Fortunately, the engineers<br />

who created WinJS and the templates in Visual Studio and Blend worried about this a lot! As a result,<br />

they’ve provided some marvelous tools for managing bits and pieces of <strong>HTML</strong>+<strong>CSS</strong>+JS in the context of<br />

a single container page:<br />

• WinJS.UI.Fragments contains a low-level “fragment-loading” API, the use of which is necessary<br />

only when you want close control over the process (such as which parts of the <strong>HTML</strong> fragment<br />

get which parent). We won’t cover it in this book; see the documentation and the Loading <strong>HTML</strong><br />

fragments sample.<br />

121

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

Saved successfully!

Ooh no, something went wrong!