Windows 8 Apps Revealed Using HTML5 and JavaScript Adam Freeman Apress Apress Media Windows 8 Apps Revealed: Using HTML5 and JavaScript Copyright © 2012 by Adam Freeman Th is work is subject to copyright. All rights are reserved by Apress Media, whether the whole or part of the material is concerned, specifi cally the rights of translation, reprinting, reuse of illustrations, recitation, broadcasting, reproduction on microfi lms or in any other physical way, and transmission or information storage and retrieval, electronic adaptation, computer software, or by similar or dissimilar methodology now known or hereafter developed. Exempted from this legal reservation are brief excerpts in connection with reviews or scholarly analysis or material supplied specifi cally for the purpose of being entered and executed on a computer system, for exclusive use by the purchaser of the work. Duplication of this publication or parts thereof is permitted only under the provisions of the Copyright Law of the Publisher’s location, in its current version, and permission for use must always be obtained from Springer. Permissions for use may be obtained through RightsLink at the Copyright Clearance Center. Violations are liable to prosecution under the respective Copyright Law. ISBN 978-1-4302-5013-5 ISBN 978-1-4302-5014-2 (eBook) Trademarked names, logos, and images may appear in this book. Rather than use a trademark symbol with every occurrence of a trademarked name, logo, or image we use the names, logos, and images only in an editorial fashion and to the benefi t of the trademark owner, with no intention of infringement of the trademark. Th e use in this publication of trade names, trademarks, service marks, and similar terms, even if they are not identifi ed as such, is not to be taken as an expression of opinion as to whether or not they are subject to proprietary rights. While the advice and information in this book are believed to be true and accurate at the date of publication, neither the authors nor the editors nor the publisher can accept any legal responsibility for any errors or omissions that may be made. Th e publisher makes no warranty, express or implied, with respect to the material contained herein. President and Publisher of Apress: Paul Manning Lead Editor: Ewan Buckingham Technical Reviewer: Fabio Claudio Ferracchiati Apress Editorial Board: Steve Anglin, Ewan Buckingham, Gary Cornell, Louise Corrigan, Morgan Ertel, Jonathan Gennick, Jonathan Hassell, Robert Hutchinson, Michelle Lowman,James Markham, Matthew Moodie, Jeff Olson, Jeff rey Pepper, Douglas Pundick, Ben Renow-Clarke, Dominic Shakeshaft, Gwenan Spearing, Matt Wade, Tom Welsh Coordinating Editor: Christine Ricketts Copy Editor: Kim Wimpsett Compositor: SPi Global Indexer: SPi Global Artist: SPi Global Cover Designer: Anna Ishchenko Distributed to the book trade worldwide by Springer Science+Business Media New York, 233 Spring Street, 6th Floor, New York, NY 10013. Phone 1-800-SPRINGER, fax (201) 348-4505, e-mail [email protected], or visit www.springeronline.com. For information on translations, please e-mail [email protected], or visit www.apress.com. Apress and friends of ED books may be purchased in bulk for academic, corporate, or promotional use. Ebook versions and licenses are also available for most titles. For more information, reference our Special Bulk Sales–eBook Licensing web page at www.apress.com/bulk-sales. Any source code or other supplementary materials referenced by the author in this text is available to readers at www.apress.com. For detailed information about how to locate your book’s source code, go to www.apress.com/source-code Dedicated to my lovely wife, Jacqui Griff yth. —Adam Freeman Contents at a Glance About the Author ..............................................................................xi About the Technical Reviewer ........................................................xiii Acknowledgments ...........................................................................xv Introduction ...................................................................................xvii ■ Chapter 1: Getting Started ..............................................................1 ■ Chapter 2: Data and Bindings .......................................................21 ■ Chapter 3: Application Controls ....................................................47 ■ Chapter 4: Layouts and Tiles ........................................................73 ■ Chapter 5: Life-Cycle Events .......................................................103 Index ..............................................................................................125 v Contents About the Author ..............................................................................xi About the Technical Reviewer ........................................................xiii Acknowledgments ...........................................................................xv Introduction ...................................................................................xvii ■ Chapter 1: Getting Started ..............................................................1 About This Book ......................................................................................1 What Do You Need to Know Before You Read This Book? .........................................1 Do You Need to Know About HTML5? .......................................................................2 What Software Do You Need for This Book? .............................................................2 What Is the Structure of This Book? .........................................................................3 Tell Me More About the Example Windows 8 App .....................................................4 Is There a Lot of Code in This Book? .........................................................................5 Getting Up and Running ..........................................................................6 Creating the Project ..................................................................................................6 Exploring the default.html File ..................................................................................8 Exploring the default.css File .................................................................................11 Exploring the default.js File ....................................................................................14 Starting and Debugging a JavaScript Windows 8 App ..........................16 Reloading the Windows 8 App ...............................................................................17 Debugging Windows Store apps .............................................................................19 Summary ...............................................................................................20 vii ■ CONTENTS ■ Chapter 2: Data and Bindings .......................................................21 Creating the JavaScript File ..................................................................22 Using Code Completion...........................................................................................23 Reducing Global Namespace Pollution ...................................................................24 Returning to the View Model ..................................................................................25 Using Data Binding ................................................................................26 Using Basic Declarative Bindings ...........................................................................27 Creating Dynamic Bindings ....................................................................................29 Updating an Observable Data Item .........................................................................32 Creating Observable Arrays ...................................................................35 Using Templates ....................................................................................38 Using the Template .................................................................................................39 Responding to List Changes ...................................................................................41 Tracking the Selected Item .....................................................................................41 Applying the Template to the App ...........................................................................42 Summary ...............................................................................................45 ■ Chapter 3: Application Controls ....................................................47 Adding an AppBar ..................................................................................48 Implementing App Bar Buttons ...............................................................................52 Adding Flyouts .......................................................................................53 Associating the Flyout with the AppBar Command .................................................55 Importing the Flyout Content ..................................................................................56 Wiring Up the Controls in a Flyout ..........................................................................57 Using Pages ...........................................................................................58 Defi ning the HTML ..................................................................................................59 Creating the JavaScript Callback ............................................................................59 Loading and Displaying the HTML ..........................................................................61 viii ■ CONTENTS Loading a Complete HTML Document ...................................................62 Switching Between Pages ......................................................................................65 Displaying External Content ..................................................................66 Adding the Callback ................................................................................................67 Showing the Page ...................................................................................................68 Checking Manifest Permissions .............................................................................70 Summary ...............................................................................................71 ■ Chapter 4: Layouts and Tiles ........................................................73 Dealing with Windows 8 Orientations and Views ..................................74 Simulating Orientation and View Changes..............................................................74 Simulating Views Changes .....................................................................................75 Adapting to Orientation and View Changes ............................................................77 Programmatically Exiting the Snapped View ..........................................................83 Using Tiles and Badges .........................................................................86 Improving Static Tiles .............................................................................................87 Applying the Images ...............................................................................................88 Updating Tiles .........................................................................................................90 Updating Wide Tiles ................................................................................................95 Applying Badges ....................................................................................98 Summary .............................................................................................101 ■ Chapter 5: Life-Cycle Events .......................................................103 Dealing with the App Life Cycle ...........................................................103 Correcting the Visual Studio Event Code ..............................................................104 Simulating Life-Cycle Events Using Visual Studio ...............................................106 Triggering Life-Cycle Events ................................................................................107 Adding an Asynchronous Activity ..........................................................................108 ix ■ CONTENTS Implementing the Search Contract ......................................................116 Declaring Support for the Contract .......................................................................116 Handling the Search .............................................................................................117 Implementing the Activated Event Handler ..........................................................119 Testing the Search Contract .................................................................................122 Summary .............................................................................................123 Index ..............................................................................................125 x About the Author Adam Freeman is an experienced IT professional who has held senior positions in a range of companies, most recently serving as chief technology offi cer and chief operating offi cer of a global bank. Now retired, he spends his time writing and running. His other upcoming publications include: Pro Windows 8 Development in HTML5 and JavaScript Pro ASP.NET MVC 4 His other publications include: Th e Defi nitive Guide to HTML5 Pro ASP.NET 4 in C# 2010 4 Edition Applied ASP.NET 4 in Context Pro ASP.NET 4 in VB 2010 3rd Edition Pro ASP.NET MVC 3 Framework 3rd Edition Pro LINQ Pro jQuery Pro .NET 4 Parallel Programming in C# Introducing Visual C# 2010 Visual C# 2010 Recipes . xi
Description: