IN ACTION Timothy Binkley-Jones Massimo Perga Michael Sync MANNING www.it-ebooks.info Windows Phone in Action www.it-ebooks.info www.it-ebooks.info Windows Phone in Action TIMOTHY BINKLEY-JONES MASSIMO PERGA MICHAEL SYNC 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 the use of elemental chlorine Manning Publications Co 20 Baldwin Road PO Box 261 Shelter Island, NY 11964 Development editor: Copyeditor: Technical proofreader: Proofreader: Typesetter: Cover designer: Jeff Bleiel Benjamin Berg Richard Reukema Melody Dolab Dennis Dalinnik Marija Tudor ISBN: 9781617290091 Printed in the United States of America 10 – MAL – 18 17 16 15 14 13 12 www.it-ebooks.info brief contents PART PART PART INTRODUCING WINDOWS PHONE 1 ■ A new phone, a new operating system ■ Creating your first Windows Phone application 29 CORE WINDOWS PHONE 55 ■ Fast application switching and scheduled actions ■ Launching tasks and choosers ■ Storing data 121 ■ Working with the camera 149 ■ Integrating with the Pictures and Music + Videos Hubs 171 ■ Using sensors ■ Network communication with push notifications and sockets 227 57 93 199 SILVERLIGHT FOR WINDOWS PHONE 257 10 ■ ApplicationBar, Panorama, and Pivot controls 259 11 ■ Building Windows Phone UI with Silverlight controls 284 v www.it-ebooks.info vi PART BRIEF CONTENTS 12 ■ Manipulating and creating media with MediaElement 310 13 ■ Using Bing Maps and the browser 341 SILVERLIGHT AND THE XNA FRAMEWORK 369 14 ■ Integrating Silverlight with XNA 15 ■ XNA input handling 399 www.it-ebooks.info 371 contents preface xv acknowledgments xvi about this book xvii about the cover illustration xxi PART INTRODUCING WINDOWS PHONE .1 A new phone, a new operating system 1.1 1.2 Rebooting the Windows Phone platform Windows Phone foundations Hardware specs A new user interface User experience Platform APIs and frameworks AppHub and the Windows Phone Marketplace 11 ■ ■ 1.3 Comparing Windows Phone to other mobile platforms 12 Windows Mobile 1.4 10 12 ■ Apple iOS 14 ■ The Windows Phone Developer Tools Android 17 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 ■ ■ ■ ■ vii www.it-ebooks.info CONTENTS viii WPConnect 24 Isolated Storage Explorer tool Marketplace Test Kit 25 ■ 1.5 25 Summary 28 Creating your first Windows Phone application 29 2.1 Generating the project 30 Debugging phone projects 33 2.2 ■ Implementing Hello World Application startup 34 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 Adding a toolbar button 43 ■ 2.4 42 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 2.7 Try before you buy Summary 53 52 PART CORE WINDOWS PHONE 55 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 3.3 62 ■ 65 Switching applications 66 Going dormant 66 Tombstoning 69 3.4 First-time initialization Out of sight 74 Obscuration 74 ■ ■ Returning to action 68 Running behind the lock screen 75 www.it-ebooks.info CONTENTS 3.5 ix Working on a schedule 77 Introducing the Scheduled Action Service 78 Scheduling a reminder 81 Editing a notification Deleting a notification 84 ■ 3.6 83 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 Launching tasks and choosers 4.1 4.2 93 Tasks API 94 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 4.5 Reviewing appointments 121 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 5.3 Storing data with application settings 127 Serializing data to isolated storage files 129 Serializing high scores with the XmlSerializer Deleting files and folders 131 5.4 115 Summary 119 Storing data 5.1 ■ Working with a database 130 132 Attributing your domain model 133 Defining the data context 135 Creating the database 136 ■ ■ www.it-ebooks.info .. .Windows Phone in Action www.it-ebooks.info www.it-ebooks.info Windows Phone in Action TIMOTHY BINKLEY-JONES MASSIMO PERGA MICHAEL SYNC MANNING SHELTER ISLAND www.it-ebooks.info For online information... 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 features, such as fast application switching, background... Running behind the lock screen 75 www.it-ebooks.info CONTENTS 3.5 ix Working on a schedule 77 Introducing the Scheduled Action Service 78 Scheduling a reminder 81 Editing a notification Deleting