MANNING Pete Brown C# and XAML www.it-ebooks.info Windows Store App Development www.it-ebooks.info ii www.it-ebooks.info Windows Store App Development C# AND XAML PETE BROWN MANNING SHELTER ISLAND www.it-ebooks.info For online information and ordering of this and other Manning books, please visit www.manning.com. The publisher offers discounts on this book when ordered in quantity. For more information, please contact Special Sales Department Manning Publications Co. 20 Baldwin Road PO Box 261 Shelter Island, NY 11964 Email: orders@manning.com ©2013 by Manning Publications Co. All rights reserved. No part of this publication may be reproduced, stored in a retrieval system, or transmitted, in any form or by means electronic, mechanical, photocopying, or otherwise, without prior written permission of the publisher. Many of the designations used by manufacturers and sellers to distinguish their products are claimed as trademarks. Where those designations appear in the book, and Manning Publications was aware of a trademark claim, the designations have been printed in initial caps or all caps. Recognizing the importance of preserving what has been written, it is Manning’s policy to have the books we publish printed on acid-free paper, and we exert our best efforts to that end. Recognizing also our responsibility to conserve the resources of our planet, Manning books are printed on paper that is at least 15 percent recycled and processed without elemental chlorine. Development editor: Jeff Bleiel Manning Publications Co. Copyeditor: Linda Recktenwald 20 Baldwin Road Technical proofreader: Thomas McKearney PO Box 261 Proofreader: Elizabeth Martin Shelter Island, NY 11964 Typesetter: Marija Tudor Cover designer: Marija Tudor ISBN: 9781617290947 Printed in the United States of America 1 2 3 4 5 6 7 8 9 10 – MAL – 18 17 16 15 14 13 www.it-ebooks.info v brief contents 1 ■ Hello, Modern Windows 1 2 ■ The Modern UI 19 3 ■ The Windows Runtime and .NET 35 4 ■ XAML 51 5 ■ Layout 69 6 ■ Panels 86 7 ■ Brushes, graphics, styles, and resources 112 8 ■ Displaying beautiful text 141 9 ■ Controls, binding, and MVVM 170 10 ■ View controls, Semantic Zoom, and navigation 211 11 ■ The app bar 241 12 ■ The splash screen, app tile, and notifications 265 13 ■ View states 300 14 ■ Contracts: playing nicely with others 319 15 ■ Working with files 342 16 ■ Asynchronous everywhere 369 17 ■ Networking with SOAP and RESTful services 388 www.it-ebooks.info BRIEF CONTENTS vi 18 ■ A chat app using sockets 423 19 ■ A little UI work: user controls and Blend 465 20 ■ Networking player location 482 21 ■ Keyboards, mice, touch, accelerometers, and gamepads 500 22 ■ App settings and suspend/resume 537 23 ■ Deploying and selling your app 559 www.it-ebooks.info vii contents preface xvii acknowledgments xx about this book xxii about the author xxviii about the cover illustration xxix 1 Hello, Modern Windows 1 1.1 Setting up the development environment 3 1.2 Configuring the project 3 The device pane 5 ■ Template solution items 7 1.3 Create the first Hello World UI 8 1.4 Integrating with Twitter 9 The Tweet class 10 ■ Updated UI 10 ■ Code-behind 11 1.5 Testing on different devices and resolutions 13 Debugging on the Simulator 13 ■ Debugging on a remote device 14 1.6 Summary 18 2 The Modern UI 19 2.1 Design inspiration 20 Direct influences 21 ■ Finding your way 22 www.it-ebooks.info CONTENTS viii 2.2 Governing principles 23 2.3 Typography 25 2.4 The importance of the layout grid 27 2.5 Design for touch but not only for touch 28 2.6 Modern apps on Windows 8 28 Consumer and enterprise apps 29 ■ Key Windows 8 UI elements and states 31 2.7 Device considerations 33 Desktop or laptop 33 ■ Tablet and smaller devices 33 Hybrid devices 34 2.8 Summary 34 3 The Windows Runtime and .NET 35 3.1 Windows Store app system architecture 36 The sandbox 38 ■ Deployment and the Windows Store 39 The driver model 40 3.2 COM + .NET metadata = WinRT 41 COM: back to the future 42 ■ Metadata 44 Projections 46 3.3 Client technologies and languages 47 3.4 A brief tour of WinRT and .NET 4.5 48 3.5 Summary 50 4 XAML 51 4.1 Elements and namespaces 52 Objects as elements 52 ■ Namespaces 54 4.2 Properties 56 Property syntax 56 ■ Dependency properties 58 Attached properties 61 ■ Property paths 62 4.3 Object trees and namescope 62 Object trees 63 ■ Namescope 66 4.4 Summary 68 5 Layout 69 5.1 Multipass layout—measuring and arranging 70 The measure pass 71 ■ The arrange pass 71 The LayoutInformation class 73 www.it-ebooks.info CONTENTS ix 5.2 UIElement layout properties 74 Width and Height, plus ActualWidth and ActualHeight 75 Horizontal and vertical alignment 77 ■ Padding 78 Margins 79 5.3 Layout rounding 80 5.4 Performance considerations 82 Keeping the tree shallow 82 ■ Caching 83 Virtualization 83 ■ Sizing and positioning 84 5.5 Summary 84 6 Panels 86 6.1 Canvas 87 Positioning in X,Y space 88 ■ Controlling the Z position using ZIndex 89 ■ Sizing child elements 91 6.2 StackPanel and VirtualizingStackPanel 91 Setting the orientation 92 ■ Sizing children 93 Virtualizing for performance 93 6.3 Grid 94 Defining rows and columns 95 ■ Adding and positioning elements in rows and columns 97 ■ Using alignment and margins for sizing and positioning 99 6.4 Creating a custom panel 102 Project setup 102 ■ The OrbitPanel class 103 ■ Orbits dependency property 103 ■ Orbit attached property 105 Custom layout 107 6.5 Summary 111 7 Brushes, graphics, styles, and resources 112 7.1 Brushes 113 Solid-color brushes 113 ■ Gradient brushes 116 Image brushes 118 7.2 Resources 120 Local and page resources 121 ■ Application resources 123 Resource dictionaries 123 7.3 Styles 127 Explicit or keyed styles 127 ■ Style inheritance 128 Implicit styles 130 www.it-ebooks.info [...]... of which version of Windows you use for building Windows Store apps If you’re already an awesome Windows developer familiar with Windows Store apps, WPF, or Silverlight, I’ve included deep topics to help you learn more about the platform and how things work under the covers After you’ve read this book, you should be able to confidently design, develop, and deliver Windows Store apps To facilitate the... you’ll have a great foundation for building your own apps Then, moving out of the source code and into the Store, in chapter 23 I cover how to prepare your app for Windows Store submission and how to get it into the Windows Store Code conventions and downloads You can find the source code for all the examples in the book at www.manning.com/ WindowsStoreAppDevelopment The following conventions are used throughout... version currently supported for Windows app development I named it HelloWorld, as shown in figure 1.2 www.it-ebooks.info 4 CHAPTER 1 Figure 1.2 Hello, Modern Windows Creating the Windows XAML app using the Blank App template Notice the other project templates: Grid App (XAML) and Split App (XAML) in particular Those are feature-rich templates, much like the business application templates and navigation... all at once 1 www.it-ebooks.info 2 CHAPTER 1 Hello, Modern Windows Because XAML and C# are one of the combinations of technologies you can use to develop for the Windows Store, if you’ve developed applications for the Windows Phone or desktop using Silverlight, you’ll find yourself well positioned to quickly learn Windows app development The Windows Runtime XAML stack and C#/.NET side of things were... ➥ symbol Author Online The purchase of Windows Store App Development includes free access to a private web forum run by Manning Publications, where you can make comments about the book, ask technical questions, and receive help from the author and from other users To access the forum and subscribe to it, point your web browser to www.manning.com/ WindowsStoreAppDevelopment This page provides information... Maréchal’s pictures xxix www.it-ebooks.info xxx ABOUT THE COVER ILLUSTRATION www.it-ebooks.info Hello, Modern Windows This chapter covers ■ Building your first Windows 8 app ■ Getting a developer license ■ Using the Simulator ■ Remotely debugging apps Welcome to the brave new world of Windows Store app development! Getting into a new platform as it first emerges is always exciting I love learning new things... use the latest recommended version of Visual Studio that will compile for the version of Windows you are using For Windows 8, here are the recommended versions: ■ Microsoft Visual Studio 2012 Pro or better for both Windows Store and web development, or the free Microsoft Visual Studio 2012 Express for Windows Store apps and, for the networking examples, the free Microsoft Visual Studio 2012 Express for... Setting up the development environment Windows 8 Modern apps can only be developed on Windows 8, so the first thing you’ll need to do is install Windows 8 Hardware selection is too large a topic for this book, but you’ll want to make sure the screen width is at least 1366 x 768 or larger so you can use snapped views for apps The machine will need to be based on an x86-compatible processor because Windows. .. package 566 ■ ■ 23.3 Enabling trial mode 567 Creating the mock license data for testing the license state 569 23.4 567 Listing your app in the Windows Store ■ Checking 570 Getting a Windows Store account 570 Reserving an app name 570 Submitting the app for review and approval 571 ■ ■ 23.5 Summary 573 index 575 www.it-ebooks.info preface I’ve been programming for fun since seventh grade in 1984 and... certain decisions Starting with Windows 8, we have a brand-new set of APIs and design approaches to become familiar with Chapter 1 introduces Windows and the concepts behind it Chapter 2 discusses the modern UI, along with its standards and history Finally, chapter 3 covers the Windows Runtime, what it is, how it has been designed, and why it’s so important for Windows Store apps XAML AND BASIC CONTROLS . MANNING Pete Brown C# and XAML www.it-ebooks.info Windows Store App Development www.it-ebooks.info ii www.it-ebooks.info Windows Store App Development C# AND XAML PETE BROWN MANNING SHELTER. 33 Hybrid devices 34 2.8 Summary 34 3 The Windows Runtime and .NET 35 3.1 Windows Store app system architecture 36 The sandbox 38 ■ Deployment and the Windows Store 39 The driver model 40 3.2 COM. sideload app package 566 23.3 Enabling trial mode 567 Creating the mock license data for testing 567 ■ Checking the license state 569 23.4 Listing your app in the Windows Store 570 Getting a Windows