MANNING Timothy Binkley-Jones Massimo Perga Michael Sync IN ACTION Windows Phone 7 in Action Windows Phone 7 in Action TIMOTHY BINKLEY-JONES MASSIMO PERGA MICHAEL SYNC MANNING S HELTER I SLAND 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 the use of elemental chlorine. Manning Publications Co. Development editor: Jeff Bleiel 20 Baldwin Road Copyeditor: Benjamin Berg PO Box 261 Technical proofreader: Richard Reukema Shelter Island, NY 11964 Proofreader: Melody Dolab Typesetter: Dennis Dalinnik Cover designer: Marija Tudor ISBN: 9781617290091 Printed in the United States of America 1 2 3 4 5 6 7 8 9 10 – MAL – 18 17 16 15 14 13 12 v brief contents P ART 1 I NTRODUCING W INDOWS P HONE 1 1 ■ A new phone, a new operating system 3 2 ■ Creating your first Windows Phone application 29 P ART 2 C ORE W INDOWS P HONE 55 3 ■ Fast application switching and scheduled actions 57 4 ■ Launching tasks and choosers 93 5 ■ Storing data 121 6 ■ Working with the camera 149 7 ■ Integrating with the Pictures and Music + Videos Hubs 171 8 ■ Using sensors 199 9 ■ Network communication with push notifications and sockets 227 P ART 3 S ILVERLIGHT FOR W INDOWS P HONE 257 10 ■ ApplicationBar, Panorama, and Pivot controls 259 11 ■ Building Windows Phone UI with Silverlight controls 284 BRIEF CONTENTS vi 12 ■ Manipulating and creating media with MediaElement 310 13 ■ Using Bing Maps and the browser 341 P ART 4 S ILVERLIGHT AND THE XNA F RAMEWORK 369 14 ■ Integrating Silverlight with XNA 371 15 ■ XNA input handling 399 vii contents preface xv acknowledgments xvi about this book xvii about the cover illustration xxi P ART 1 I NTRODUCING W INDOWS P HONE 1 1 A new phone, a new operating system 3 1.1 Rebooting the Windows Phone platform 4 1.2 Windows Phone foundations 5 Hardware specs 6 ■ A new user interface 7 User experience 8 ■ Platform APIs and frameworks 10 AppHub and the Windows Phone Marketplace 11 1.3 Comparing Windows Phone to other mobile platforms 12 Windows Mobile 12 ■ Apple iOS 14 ■ Android 17 1.4 The Windows Phone Developer Tools 20 Visual Studio for Windows Phone 20 ■ Expression Blend for Windows Phone 20 ■ XNA Game Studio 20 Windows Phone Emulator 21 ■ Windows Phone Developer Registration tool 22 ■ XAP Deployment tool 23 CONTENTS viii WPConnect 24 ■ Isolated Storage Explorer tool 25 Marketplace Test Kit 25 1.5 Summary 28 2 Creating your first Windows Phone application 29 2.1 Generating the project 30 Debugging phone projects 33 ■ Application startup 34 2.2 Implementing Hello World 35 Customizing the startup page 35 ■ Adding application content 37 ■ Adding the greetings page 39 2.3 Interacting with the user 41 Touch typing 41 ■ Touch gestures 42 Adding a toolbar button 43 2.4 Page navigation 45 Navigating to another page 45 ■ Passing parameters between pages 47 ■ Changing the Back key behavior 48 Navigating with tiles 49 2.5 Application artwork 50 Customizing the splash screen 50 Customizing tile images and application icons 50 2.6 Try before you buy 52 2.7 Summary 53 P ART 2 C ORE W INDOWS P HONE 55 3 Fast application switching and scheduled actions 57 3.1 Fast application switching 58 Understanding lifetime events 59 Creating the Lifetime sample application 61 3.2 Launching the application 62 Construction 62 ■ First-time initialization 65 3.3 Switching applications 66 Going dormant 66 ■ Returning to action 68 Tombstoning 69 3.4 Out of sight 74 Obscuration 74 ■ Running behind the lock screen 75 CONTENTS ix 3.5 Working on a schedule 77 Introducing the Scheduled Action Service 78 Scheduling a reminder 81 ■ Editing a notification 83 Deleting a notification 84 3.6 Creating a background agent 85 Background agent projects 85 ■ Executing work from the background agent 86 ■ Scheduling a PeriodicTask 87 Scheduled tasks expire after two weeks 88 User-disabled tasks 89 ■ When things go awry 90 Testing background agents 91 3.7 Summary 92 4 Launching tasks and choosers 93 4.1 Tasks API 94 4.2 Launchers 96 Placing a phone call 97 ■ Writing an email 98 Texting with SMS 99 ■ Working with the Marketplace 100 Searching with Bing 103 4.3 Choosers 103 Completed events 104 ■ Saving a phone number 105 Saving an email address 106 ■ Saving a ringtone 107 Choosing a phone number 108 ■ Choosing an email address 109 ■ Choosing a street address 109 4.4 UserData APIs 110 Searching for contacts 111 ■ Reviewing appointments 115 4.5 Summary 119 5 Storing data 121 5.1 Creating the High Scores sample application 122 Displaying the high score list 123 ■ Managing the high score list 125 ■ Defining a high score repository 126 5.2 Storing data with application settings 127 5.3 Serializing data to isolated storage files 129 Serializing high scores with the XmlSerializer 130 Deleting files and folders 131 5.4 Working with a database 132 Attributing your domain model 133 ■ Defining the data context 135 ■ Creating the database 136 [...]... Mobile 6 and Windows Phone 6.5 Windows Windows Phone foundations 5 Mobile 6 is built on Windows CE 5 and includes the NET Compact Framework 2.0 SP1 Windows Mobile 6 comes in three editions—Standard, Professional, and Classic For the remainder of the book, when the term Windows Phone is used without a version number, we are referring to Windows Phone 7. 5 We’ll use Windows Mobile or Windows Phone 6.5 to... for Windows Mobile and Windows Phone 6.5 aren’t compatible with Windows Phone 7 Windows Mobile development environments and tools are also incompatible In this section we illustrate the major changes which will impact every developer with previous experience in Windows Mobile development, starting with the user interface Comparing Windows Phone to other mobile platforms 13 BUILDING YOUR INTERFACE Windows. .. the Windows Phone differ from Windows Forms on Windows Mobile? Where do you begin when porting your iOS or Android application? In this section we get you started with Windows Phone development by identifying the similarities and differences with other application platforms 1.3.1 Windows Mobile If you’re a third-party Windows Mobile developer, then you should know that Windows Phone 7 is not Windows. .. first version of the Windows Phone 7 operating system was released in October 2010 Microsoft followed the release with an update in the early months of 2011, adding copy/paste support and performance improvements At the Mix 2011 conference, Microsoft unveiled details about the Windows Phone 7. 5 operating system and the corresponding Windows Phone SDK 7. 1 The Windows Phone 7. 1 SDK includes several new... guide to building mobile applications for Windows Phone 7. 5 using Silverlight, C#, XNA, or HTML5 The Windows Phone 7 operating system is Microsoft’s latest entry into the fiercely competitive mobile market Windows Phone 7 is not an upgrade of previous mobile operating systems, Windows Mobile and Windows Phone 6.5 Microsoft has reimagined what a mobile operating system should be and completely changed... 174 Retrieving a picture from the media library 175 ■ ■ 7. 2 Editing and sharing from the Pictures Hub 176 Extending the Picture Hub 176 Extending the Picture Viewer 178 Sharing pictures from your Pictures Hub extension 180 ■ ■ 7. 3 Playing and recording with the Music + Videos Hub 181 Enabling XNA Framework events 183 Building the user interface 183 Recording audio 185 Playing audio 189 ■ ■ 7. 4 Playing... switching, background agents, access to the camera hardware, and a built -in SQL CE database engine Windows Phone 7. 5 also exposes new compass, gyroscope, and motion sensors We find it a bit confusing that the new operating system is versioned with 7. 5 while the corresponding SDK is versioned 7. 1 Throughout this book we’ll refer to both operating system releases as Windows Phone 7 or just Windows Phone. .. 159 Painting with the VideoBrush 162 Snapping a photo 163 Supporting fast application switching 165 ■ 6.4 Image editing 165 Rendering Silverlight elements 166 Saving an image to isolated storage 1 67 Loading an image from isolated storage 168 ■ ■ 6.5 7 Summary 169 Integrating with the Pictures and Music + Videos Hubs 171 7. 1 Working with pictures in the Media Library 172 Exposing Pictures 172 Saving pictures... use their phones to manage their busy lifestyles Windows Phone 7 was designed to let users get tasks done faster and allow them to get back to the important aspects of their life The Windows Phone 7 operating system is Microsoft’s latest entry into the fiercely competitive mobile market Windows Phone 7 is not an upgrade of previous mobile operating systems, such as Windows Mobile and Windows Phone 6.5... FRAMEWORK 369 14 Integrating Silverlight with XNA 371 14.1 Creating a Silverlight with XNA application 373 Sharing the graphics device 14.2 374 Building the game page ■ The game loop 375 376 Understanding models 377 Rendering models 379 Adding shapes 382 Moving around 383 Running a demonstration 3 87 Don’t repeat yourself 389 Collecting shapes 390 It’s the end of the world 393 ■ ■ ■ ■ 14.3 Implementing a scoreboard . MANNING Timothy Binkley-Jones Massimo Perga Michael Sync IN ACTION Windows Phone 7 in Action Windows Phone 7 in Action TIMOTHY BINKLEY-JONES MASSIMO PERGA MICHAEL SYNC MANNING S HELTER . 74 ■ Running behind the lock screen 75 CONTENTS ix 3.5 Working on a schedule 77 Introducing the Scheduled Action Service 78 Scheduling a reminder 81 ■ Editing a notification 83 Deleting a notification. Marketplace 11 1.3 Comparing Windows Phone to other mobile platforms 12 Windows Mobile 12 ■ Apple iOS 14 ■ Android 17 1.4 The Windows Phone Developer Tools 20 Visual Studio for Windows Phone 20 ■ Expression