Beginning Windows 8 Application Development: XAML Edition Kyle Burns Beginning Windows 8 Application Development: XAML Edition Copyright © 2012 by Kyle Burns This work is subject to copyright. All rights are reserved by the Publisher, whether the whole or part of the material is concerned, specifically the rights of translation, reprinting, reuse of illustrations, recitation, broadcasting, reproduction on microfilms 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 specifically 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-4566-7 ISBN 978-1-4302-4567-4 (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 benefit of the trademark owner, with no intention of infringement of the trademark. The use in this publication of trade names, trademarks, service marks, and similar terms, even if they are not identified 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. The publisher makes no warranty, express or implied, with respect to the material contained herein. President and Publisher: Paul Manning Lead Editor: Jonathan Hassell Technical Reviewer: George Johnston 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, Jeffrey Pepper, Douglas Pundick, Ben Renow-Clarke, Dominic Shakeshaft, Gwenan Spearing, Matt Wade, Tom Welsh Coordinating Editor: Kevin Shea 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. This book is dedicated to my wife, Lisa, who encouraged me to undertake the endeavor of writing it, and my son, Kolin, who spent a summer knowing me as the guy who came home from work and sat at the computer writing. Thank you both for your support in this and every other aspect of my life. Contents at a Glance About the Author ........................................................................................................xvii About the Technical Reviewer .....................................................................................xix Acknowledgments .......................................................................................................xxi Introduction ...............................................................................................................xxiii (cid:78) Chapter 1: Welcome to a Touch-First World ................................................................1 (cid:78) Chapter 2: The Windows Design Language ...............................................................11 (cid:78) Chapter 3: Designing Windows 8 Applications ..........................................................25 (cid:78) Chapter 4: Visual Studio 2012 and Windows Store Application Types ......................33 (cid:78) Chapter 5: XAML Controls in the Visual Studio Toolbox: The Common Controls .......45 (cid:78) Chapter 6: XAML Controls in the Visual Studio Toolbox: Other Controls ...................67 (cid:78) Chapter 7: Building the User Interface ......................................................................91 (cid:78) Chapter 8: Data Binding...........................................................................................105 (cid:78) Chapter 9: Introducing MVVM .................................................................................127 (cid:78) Chapter 10: Starting the ViewModel ........................................................................141 (cid:78) Chapter 11: Inversion of Control ..............................................................................165 (cid:78) Chapter 12: The Role of Service Agents ..................................................................175 (cid:78) Chapter 13: Asynchronous Programming Model .....................................................181 (cid:78) Chapter 14: Mocking the Service Agent ..................................................................199 (cid:78) Chapter 15: Connecting to Data in the Cloud ...........................................................209 (cid:78) Chapter 16: Completing the Service Agent ..............................................................233 v (cid:78) CONTENTS AT A GLANCE (cid:78) Chapter 17: Interacting with Windows Search and Share .......................................243 (cid:78) Chapter 18: Notifications and Tiles .........................................................................259 (cid:78) Chapter 19: Sensors, Devices, and the Location API ...............................................273 (cid:78) Chapter 20: Sharing Apps in the Windows Store .....................................................285 Index ...........................................................................................................................293 vi Contents About the Author ........................................................................................................xvii About the Technical Reviewer .....................................................................................xix Acknowledgments .......................................................................................................xxi Introduction ...............................................................................................................xxiii (cid:78) Chapter 1: Welcome to a Touch-First World ................................................................1 Moving to More Natural Interaction .........................................................................................2 Windows 8 Touch Language ....................................................................................................2 Press and Hold ................................................................................................................................................2 Tap ..................................................................................................................................................................3 Slide ................................................................................................................................................................4 Swipe ..............................................................................................................................................................4 Pinch ...............................................................................................................................................................5 Stretch ............................................................................................................................................................6 Swipe from Edge ............................................................................................................................................6 Turn .................................................................................................................................................................7 Keys to a Successful Touch Interface ......................................................................................8 Responsiveness ..............................................................................................................................................8 Touch Targets ..................................................................................................................................................8 Intuitive Interface............................................................................................................................................8 Beyond Touch ..........................................................................................................................8 Conclusion ...............................................................................................................................9 vii (cid:78) CONTENTS (cid:78) Chapter 2: The Windows Design Language ...............................................................11 Swiss Design Style ................................................................................................................11 Influence of Bauhaus ....................................................................................................................................11 Elements of Swiss Design ............................................................................................................................12 Windows Design Language ...................................................................................................18 Windows Design Principles ..........................................................................................................................18 Windows User Experience Guidelines ..........................................................................................................19 Styling in the Windows 8 User Interface .......................................................................................................23 Conclusion .............................................................................................................................24 (cid:78) Chapter 3: Designing Windows 8 Applications ..........................................................25 Communication Is Key ...........................................................................................................25 What Should the Application Be Good At? .............................................................................26 Identify Functional Requirements .........................................................................................27 Evaluate Identified Requirements .........................................................................................28 Decompose Requirements ....................................................................................................30 Build Interaction Flows ..........................................................................................................30 Wireframes ............................................................................................................................31 Visual Design .........................................................................................................................31 Conclusion .............................................................................................................................32 (cid:78) Chapter 4: Visual Studio 2012 and Windows Store Application Types ......................33 Visual Studio Editions ............................................................................................................33 Getting Started with Visual Studio .........................................................................................34 Windows Store Application Types ..........................................................................................39 Blank App (XAML) .........................................................................................................................................40 Grid App (XAML) ............................................................................................................................................40 Split App (XAML) ...........................................................................................................................................41 Conclusion .............................................................................................................................43 viii (cid:78) CONTENTS (cid:78) Chapter 5: XAML Controls in the Visual Studio Toolbox: The Common Controls .......45 Grid ........................................................................................................................................46 StackPanel ............................................................................................................................48 Border ....................................................................................................................................50 TextBlock ...............................................................................................................................51 TextBox ..................................................................................................................................52 Button ....................................................................................................................................53 RadioButton ...........................................................................................................................54 CheckBox ..............................................................................................................................55 ComboBox .............................................................................................................................57 Image ....................................................................................................................................59 FlipView .................................................................................................................................60 GridView and ListView ...........................................................................................................62 Rectangle ..............................................................................................................................64 A Word on Style .....................................................................................................................66 Conclusion .............................................................................................................................66 (cid:78) Chapter 6: XAML Controls in the Visual Studio Toolbox: Other Controls ...................67 AppBar ...................................................................................................................................67 Canvas ...................................................................................................................................70 CaptureElement .....................................................................................................................70 ContentControl ......................................................................................................................70 ContentPresenter ..................................................................................................................71 Ellipse ....................................................................................................................................71 Frame ....................................................................................................................................72 HyperlinkButton .....................................................................................................................73 ItemsControl ..........................................................................................................................74 ItemsPresenter ......................................................................................................................74 ListBox ...................................................................................................................................74 ix (cid:78) CONTENTS MediaElement .......................................................................................................................75 PasswordBox .........................................................................................................................76 Popup ....................................................................................................................................77 ProgressBar ...........................................................................................................................77 ProgressRing .........................................................................................................................78 RepeatButton .........................................................................................................................79 RichEditBox ...........................................................................................................................79 RichTextBlock and RichTextBlockOverflow............................................................................80 ScrollBar ................................................................................................................................82 ScrollContentPresenter .........................................................................................................82 ScrollViewer ..........................................................................................................................82 SemanticZoom ......................................................................................................................83 Slider .....................................................................................................................................85 ToggleButton .........................................................................................................................85 ToggleSwitch .........................................................................................................................86 ToolTip ...................................................................................................................................87 VariableSizedWrapGrid ..........................................................................................................88 ViewBox .................................................................................................................................88 VirtualizingStackPanel ...........................................................................................................89 WebView ................................................................................................................................89 WrapGrid ...............................................................................................................................90 Conclusion .............................................................................................................................90 (cid:78) Chapter 7: Building the User Interface ......................................................................91 Introducing Style ...................................................................................................................91 Where Styles Are Defined .............................................................................................................................92 StandardStyles.xaml .....................................................................................................................................97 DataTemplates .......................................................................................................................97 Where DataTemplates Are Defined ...............................................................................................................98 x