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: [email protected]
©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
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
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
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
■
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
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
CONTENTS
x
CRUD operations 137
■
Searching for data 141
Compiling queries 142
■
Upgrading 143
Adding a read-only database to your project 146
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 recorded audio in the Music + Videos Hub 190
Fulfilling Music + Videos Hub requirements 191
Launching from the Music + Videos Hub 193
7.5 Playing recorded audio with a background agent 194
CONTENTS
xi
7.6 Listening to FM radio 196
7.7 Summary 197
8
Using sensors 199
8.1 Understanding the sensor APIs 200
Data in three dimensions 201
9.1 Detecting network connectivity 228
Reading device settings 229
Using the NetworkInterface class 231
9.2 Pushing notifications to a phone 232
Three types of notifications 233
■
Push notification
workflow 234
■
Creating a Push Notification client 235
Opening a notification channel 236
■
Looking for
navigation parameters 237
■
In-app notifications 238
Copying the channel URI 239
9.3 Simulating a push notification service 239
Issuing HTTP web requests 240
■
Sending toast
notifications 243
■
Using notifications to update a tile 244
9.4 Tiles without all the pushiness 247
CONTENTS
xii
9.5 Communicating with sockets 249
9.6 Implementing a chat application with TCP sockets 250
Building the Chit-chat client 250
Remembering where you are 272
■
Adding a background 273
Customize the title 274
10.3 Pivoting around an application 275
Building the sample 276
■
Remembering the
current selection 278
■
Generating sample data 279
Dynamically loading pages 281
10.4 Summary 283
11
Building Windows Phone UI with Silverlight controls 284
11.1 Handling page orientation 285
Supported orientations 286
■
Animating orientation
transitions 287
■
Changing orientation 289
11.2 Building user interfaces 290
TextBlock 290
■
Border 292
■
Shapes 293
ProgressBar 293
■
Controlling volume 319
12.2 Manipulating the media stream with
MediaStreamSource 320
Opening a media source 321
■
Seeking media 322
Sampling media 323
12.3 Creating custom video 324
Initializing the stream source 325
■
Opening the video
stream source 326
■
Generating media samples 327
12.4 Creating custom audio 329
Defining a custom audio stream source 330
Opening the audio stream source 331
Generating audio samples 332
12.5 Streaming media clients 334
Using Smooth Streaming 335
■
Streaming limitations 338
12.6 Summary 340
13
Using Bing Maps and the browser 341
13.1 Introducing Bing Maps 342
Preparing the application 343
■
Launching the Bing
Maps application 343
4 S
ILVERLIGHT
AND
THE
XNA F
RAMEWORK
369
14
Integrating Silverlight with XNA 371
14.1 Creating a Silverlight with XNA application 373
Sharing the graphics device 374
■
The game loop 375
14.2 Building the game page 376
Understanding models 377
■
Rendering models 379
Adding shapes 382
■
Moving around 383
Running a demonstration 387
■
Don’t repeat yourself 389
Collecting shapes 390
■
It’s the end of the world 393
14.3 Implementing a scoreboard with Silverlight 394
Adding a scoreboard 395
WPF
and Silverlight.
Against all this diversity, our shared passion for Silverlight and mobile applications
brought us together to produce this book.
In 2011, nearly half a billion smart phones were sold worldwide. The world is
quickly moving to a fully connected society, and smart phones like the Windows
Phone are positioned to play a major role in how we access data, connect with our
family and friends, and generally interact with the world around us. Smartphones are
almost always with us, know where they are located, and are connected to the internet.
Our job as application developers is to create applications that can interact with
our environment, sift through the data, and present a simplified view of the world to
users overwhelmed with the complexities of the fast-paced, high-tech, digital world.
We hope our book gives you the knowledge you need to determine location, process
sensor input, capture audio and video, and scrutinize data to build killer Windows Phone
applications that integrate nicely with the operating system and native applications.
xvi
acknowledgments
We would like to thank our family, friends, and coworkers for their support and advice,
for being there when we needed someone to listen to half-formed ideas, and for
understanding when we said “I’d love to, but I have to work on the book.” The chap-
ters covering the
XNA
Framework would have been impossible to write without advice
and debugging from Trystan Binkley-Jones.
Of course none of this would have been possible without Microsoft and the sup-
port they provide to the development community. In particular, we would like to
thank Cliff Simpkins for providing hub screenshots and a developer phone complete
with a pre-release version of Windows Phone 7.5.
We would like to thank the following reviewers, who read the manuscript at various
stages during development and provided invaluable feedback: ‘Anil’ Radhakrishna,
ware, sensors, and software. In this book we show you how to build user interfaces that
adhere to the new Metro design, and how to use the new
API
s to access the sensors
and integrate with the built-in application.
Who should read this book
This book is written for C# and .
NET
developers who are familiar with
XAML
, Silverlight
or
WPF
development. This book does not teach you the subtleties of C# or Silverlight/
XAML
development. That being said, the book avoids many of the more powerful fea-
tures of Silverlight and the Model-View-ViewModel pattern used by many Silverlight
developers. Instead we kept the focus on the features and
API
s that are unique to the
phone and endeavored to make the content accessible to those readers who are not
very familiar with Microsoft technologies.
ABOUT THIS BOOK
xviii
Roadmap
This book has four parts, fifteen chapters, and three appendices. We divided the book
into sections that introduce Windows Phone 7, cover the core concepts of the phone,
and discuss enhancements to Silverlight. The final section of the book shows you how
Silverlight applications can use the powerful graphics
API
In chapter 6 you’ll build an application that captures images from the phone cam-
era and allows a user to make simple modifications to the photographs.
In chapter 7 you’ll integrate an application with the built-in Pictures and Music +
Video Hubs.
In chapter 8 you’ll learn how to obtain data from the phone’s hardware including
the accelerometer, compass, gyroscope, and motion sensor.
In chapter 9 we cover networking topics such as using
TCP
sockets and Push Notifi-
cations. Push Notifications provide the ability for an external application or web ser-
vice to send messages and updates to a particular Windows Phone device.
Part 3 presents new Silverlight features and controls used to build applications that
match the look and feel of Windows Phone.
In chapter 10 you’ll take a deep dive into the Application Bar, Panorama, and Pivot
controls that are unique to the Windows Phone.
In chapter 11 you’ll learn how to build applications that automatically adjust them-
selves to match the Metro design, and how to control the software keyboard. You will
also be introduced to the Silverlight Toolkit for Windows Phone, a Codeplex project
from Microsoft.
ABOUT THIS BOOK
xix
In chapter 12 you’ll work with the MediaElement to play audio and video and will
learn how to create a Windows Phone 7 Smooth Streaming client application.
In chapter 13 you’ll build a location-aware application using location services and
the Bing Maps
API
. You’ll also build an
HTML5
-based application.
Part 4 of this book demonstrates how Silverlight and the
font
like
this
, which sets it off from
the surrounding text. In many listings, the code is annotated to point out the key con-
cepts, and numbered bullets are used in the text to provide additional information
about the code. We’ve tried to format the code so that it fits within the available page
space in the book by adding line breaks and using indentation carefully. Sometimes,
however, very long lines include line continuation markers.
The source code presented in the book can be downloaded from the publisher’s
web site at www.manning.com/WindowsPhone7inAction.
The source code is organized into folders for each chapter, with sub-folders for
each project. The source code contains the completed sample projects for each chap-
ter. Many of the sample projects link to image files shipped as part of the
SDK
. We
chose not to redistribute the image files and instead used Visual Studio’s linked file
features when adding the images to the projects.
Software or hardware requirements
The Windows Phone Developer Tools, which Microsoft provides as a free download,
are required to compile and execute the sample projects presented in this book. The
Windows Phone Developer Tools install an express edition of Visual Studio 2010 con-
figured with the phone development tools. If you already have a retail edition of
Visual Studio 2010 installed on your computer, the phone development tools will be
installed as a plug-in to the IDE. Windows Phone projects can be written in both C#
and Visual Basic.
We’ll use the express edition throughout the book for the screen shots and sample
code. Code and user interface design features will work the same in the retail editions
of free disk space on the system drive.
■
3
GB
RAM
.
■
Windows Phone Emulator requires a DirectX 10 or above capable graphics card
with a
WDDM
1.1 driver.
Author Online
Purchase of Windows Phone 7 in Action 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 authors and from other users. To
access the forum and subscribe to it, point your web browser to www.manning.com/
WindowsPhone7inAction. This page provides information on how to get on the
forum once you’re registered, what kind of help is available, and the rules of conduct
on the forum.
Manning’s commitment to our readers is to provide a venue where a meaningful
dialog between individual readers and between readers and the author can take place.
It’s not a commitment to any specific amount of participation on the part of the
author, whose contribution to the
AO
remains voluntary (and unpaid). We suggest
you try asking the authors some challenging questions lest their interest stray!
The Author Online forum and the archives of previous discussions will be accessi-
ble from the publisher’s website as long as the book is in print.
xxi
OS
development and introduce you to Visual
Studio and the
SDK
tools you’ll use when building applications.
In chapter 2 you’ll build your first Windows Phone 7 project, which is a tradi-
tional Hello World application. We use the Hello World application to introduce
you to touch events, application tiles, the application bar, and the Windows Phone
navigation model.