Start developing applications
for the Windows Phone quickly and easily
Beginning
Windows Phone 7
Development
Henry Lee
|
Eugene Chuvyrov
Start developing applications
for the Windows Phone quickly and easily
Beginning
Windows Phone 7
Development
Henry Lee
|
Eugene Chuvyrov
Start developing applications
for the Windows Phone quickly and easily
Beginning
Windows Phone 7
Development
Henry Lee
|
Eugene Chuvyrov
Start developing applications
for the Windows Phone quickly and easily
Beginning
Windows Phone 7
Development
Henry Lee
|
Henry Lee
|
Eugene Chuvyrov
Download from Wow! eBook <www.wowebook.com>
i
Beginning Windows
Phone 7 Development ■ ■ ■
Henry Lee
and
Eugene Chuvyrov
ii
Beginning Windows Phone 7 Development
Copyright © 2010 by Henry Lee & Eugene Chuvyrov
All rights reserved. No part of this work may be reproduced or transmitted in any form or by any means,
electronic or mechanical, including photocopying, recording, or by any information storage or retrieval system,
without the prior written permission of the copyright owner and the publisher.
ISBN-13 (pbk): 978-1-4302-3216-2
person or entity with respect to any loss or damage caused or alleged to be caused directly or indirectly by the
information contained in this work.
The source code for this book is available to readers at www.apress.com.
iii
To Erica, my beautiful wife, for her love and support.
Thank you for always being there for me.
—Henry Lee
To Marianna—my friend, my wife, my muse—and to
my parents for their gift of unconditional love.
—Eugene Chuvyrov
iv
Contents at a Glance
■Contents v
■About the Authors xvi
■About the Technical Reviewer xvii
■Acknowledgments xviii
■Introduction xix
Part 1: The Essentials of Windows Phone 7 Application Development 1
■Chapter 1: Introducing Windows Phone 7 and the Windows Phone Platform 3
■Chapter 2: Building Windows Phone 7 Applications 15
■Chapter 3: Using Cloud Services As Data Stores 35
■Chapter 4: Catching and Debugging Errors 97
■Chapter 5: Packaging, Publishing, and Managing Applications 121
Part 2: Using Windows Phone 7 Technologies 139
■Chapter 6: Working with the Accelerometer 141
■Chapter 7: Application Bar 161
■Chapter 8: WebBrowser Control 179
Cloud Services 11
Metro Design 12
Application Development Life Cycle 12
Summary 14
■Chapter 2: Building Windows Phone 7 Applications 15
Preparing Your Development Machine 15
Building Your First Windows Phone 7 Application 15
Creating a Windows Phone Project 16
■ CONTENTS
vi
Using Your First Windows Phone Silverlight Controls 18
Writing Your First Windows Phone Code 22
Running Your First Silverlight Windows Phone Application 23
Customizing Your First Windows Phone Application 25
Styling Your Application 28
Summary 34
■Chapter 3: Using Cloud Services As Data Stores 35
Introducing the MVVM Pattern 36
Introducing Microsoft Azure and SQL Azure 37
Creating a Cloud Database 37
Creating an SQL Azure Database 38
Creating a Database in SQL Azure 47
Creating a Cloud Service to Access the Cloud Database 50
Creating a Windows Azure Project 51
Generating an Object Model to Access the Cloud Database 52
Implementing a WCF Service to Access the SQL Azure Database 57
Building a Phone Client to Access a Cloud Service 64
Creating a Windows Phone Project 64
Building the User Interface 65
Submitting Your First Windows Phone Application to
the Windows Phone Marketplace 128
Package the application 128
Submit the application 129
Updating your application 136
Finding your application in the Marketplace 137
Summary 138
Part 2: Using Windows Phone 7 Technologies 139
■Chapter 6: Working with the Accelerometer 141
Understanding Orientation and Movement 141
Calculating Distance 144
Calculating Pitch and Roll 144
Introducing SDK Support for Accelerometers 146
■ CONTENTS
viii
Retrieving Accelerometer Data 146
Creating the CaptureAccelerometerData Project 147
Building the User Interface 148
Coding the Application 150
Testing the Finished Application 152
Using Accelerometer Data to Move a Ball 153
Creating the MoveBall Project 154
Building the User Interface 155
Coding the Application 157
Testing the Finished Application 159
Summary 159
■Chapter 7: Application Bar 161
Introducing the Application Bar 162
Adding an Application Bar to a Windows Phone 7 Application 163
Adding Images for Use with Application Bar Buttons 164
Applying a Theme 196
Changing the Theme 198
Detecting the Currently Selected Theme 200
Panorama and Pivot Controls 201
Using the Panorama Control 201
Using the Pivot Control 205
Understanding Frame and Page Navigation 205
Creating a User Interface for NavigationTest Project 206
Adding Navigation Code 206
Adding Code to Pass Parameters Between Pages 207
Adding Transition Effects 207
Creating a User Interface 207
Downloading TiltEffect.cs and Applying Dependency Properties 209
Summary 210
■ CONTENTS
x
■Chapter 10: Integrating Applications with the Windows Phone OS 211
Introducing Windows Phone 7 Launchers and Choosers. 211
Launchers . 212
Choosers . 213
Working with Launchers and Choosers . 213
Creating the User Interface. 213
Coding Application Logic. 214
Working with the Windows Phone 7 Application Life Cycle. 215
Observing Application Life Cycle Events. 216
Managing Application State. 220
Best Practices for Managing the Application Life Cycle on the Windows Phone 7 OS 225
Windows Phone 7 Hubs . 225
Summary. 227
■Chapter 11: Creating Trial Applications 229
Summary 283
■Chapter 14: Using Location Services 285
Understanding Windows Phone Location Services Architecture 285
Introducing the Windows Phone Location Service and Mapping APIs 286
Simulating the Location Service 286
Creating the GeoCoordinateWatcherDemo Project 287
Coding the User Interface 288
Coding the Application 290
Testing the Finished Application 292
Using GeoCoordinateWatcher and the Bing Maps Control to Track Your Movements 292
Registering with the Bing Maps Service Portal and Installing the Bing Maps SDK 293
Creating the BingMapDemo Project 295
Coding the User Interface 297
Coding the Application 299
Testing the Finished Application 302
Plotting an Address on a Bing Maps Map and Working with the Bing Maps Service . 303
Creating the AddressPlottingDemo Application 304
■ CONTENTS
xii
Adding a Service Reference to the Bing Maps GeoCodeService 304
Coding the User Interface 306
Coding the Application 308
Testing the Finished Application 310
Summary 310
■Chapter 15: Media 311
Introducing MediaElement 311
Working with Video 312
Creating the MediaPlayerDemo Project 313
Building the User Interface 314
Creating a Client Application 364
Creating an Application to Send Notifications 364
Implementing Raw Notifications 367
Creating a Client Application 368
Creating an Application to Send Notifications 371
Testing Delivery of Raw Notifications 372
Implementing Cloud Service to Track Push Notifications 372
Creating a WCF Service to Track Notification Recipients 373
Modifying the Client to Call the WCF Service 378
Verifying Automated Push Notification Subscriber Tracking 380
Using Push Notifications in the Real World 381
Summary 381
■Chapter 18: Reactive Extensions for .NET 383
Introducing Reactive Programming 384
Implementing the Observer Pattern with Rx.NET 386
Creating a Windows Phone Project 386
Adding Code to Create and Read Observable Collections 386
Using Rx.NET Event Handling to Search for Flickr Photographs 388
Creating a Windows Phone Project 389
Adding a User Interface 390
Adding Logic to Search Flickr for Images 390
■ CONTENTS
xiv
Enhancing a Flickr Search with Throttling 391
Adding an Animation that Plays as Flickr Images Load 393
Using Rx.NET with Web Services to Asynchronously Retrieve Weather Data 398
Creating a Windows Phone Project 398
Creating a User Interface 399
Adding Logic to Get Weather Information 401
Application Must Not Implement Any Security-Critical Code 438
Summary 439
■Index 441
■ CONTENTS
xvi
About the Authors
■ Henry Lee is the founder of NewAgeSolution.net and is passionate about
technology. He works with various Fortune 500 companies, delivering mobile
applications and rich Internet applications. He recently formed a startup company
called ToeTapz.com, focusing his energy on delivering mobile applications in
Windows Phone 7, Android, and iPhone. In his spare time, he dedicates his effort to
help his .NET community by delivering sessions at technology events. He enjoys
talking with other technologists about current trends in technology and sharing
business insights with fellow colleagues. Often you will find Henry at a local cigar bar,
enjoying a cigar and a drink, trying to come up with the next big mobile application.
■ Eugene Chuvyrov is an independent .NET consultant in beautiful Jacksonville,
Florida. He was lucky enough to start working with Microsoft technologies when he
graduated from college in 1998, and has been consistently delivering a positive return
on investment to the clients that engage him. His most recent venture is an online
event marketing startup, packedhouseevents.com, which extends event creation,
marketing, and electronic payments to anybody with Internet access. Eugene also
facilitates the meetings of the Jacksonville Software Architecture Group, where he
enjoys networking and learning from smart people.
As soon as Eugene heard the news that a new mobile platform (Windows Phone
7) was being released by Microsoft, he was immediately intrigued. It was hard to resist
the temptation of wide-open possibilities to create smartphone applications using his favorite IDE and
all the latest cloud and functional programming–based technologies. This passion, combined with a
unique opportunity to share our knowledge. Thank you, Ewan, for believing in us. Stefan Turalski has
given us consistent in-depth technical guidance and commentary—thank you for ensuring that we use
industry best practices at all times. John Osborn made our book understandable not only to the two of
us, but to the rest of the world as well. Thank you, John; we are much better writers now because of you!
Mary Ann Fugate performed very timely copyedit checks and ensured that all the references and
grammar made sense—thank you, Mary Ann.
However, this book would have never come to fruition if it weren’t for our coordinating editor,
Jennifer Blackwell. Jennifer encouraged us when we needed encouragement, provided guidance when
we were lost, and made us see the light at the end of the tunnel at all times. For that, we are extremely
grateful!
xix
Introduction
We decided that we should write a book about something we enjoy doing to share with our fellow
developers to fulfill their dreams. This is how our journey started. When the startup company
NewAgeSolution.net was founded, the company emphasized delivering compelling rich Internet
applications using Silverlight. The company enjoyed great success, with eight employees, delivering
amazing Silverlight applications that you can read about in a Microsoft case study
(www.microsoft.com/casestudies/Case_Study_Detail.aspx?CaseStudyID=4000006798).
Then Microsoft invited many of the Silverlight partners to join the Windows Phone Partner
program, and after attending the Windows Phone partner conference in Redmond at the Microsoft
campus, we truly believed that the age of the smartphone had begun. It was a natural progression for us
to tackle Windows Phone, since Silverlight is one of the primary development platforms, and we were
able to utilize all the knowledge that we had gained over the years.
Windows Phone is our passion and our daily life, The Windows Phone market is big, and we want to
share our dream with you by showing you how to successfully develop a Windows Phone application so
you can also deploy your dream to the Marketplace.
Who This Book Is For
This book assumes that you have basic C# and .NET knowledge. This book will provide you with basic
fundamentals and skills that you will need to be successful in developing a Windows Phone application.
section concludes with step-by-step instruction on how to gain certification from Microsoft and offer an
application to the public through the Windows Phone Marketplace.
In Part 2, you will learn how to use specific features of Windows Phone devices in your applications,
including the accelerometer, location service, application bar, reactive extensions, application hub
integration, application life cycle events, isolated storage, Silverlight, XAML, skinning controls, web
browser controls, media elements, photos, push notifications, internalization, and security. While each
of its chapters is a tutorial, you can also use Part 2 as a reference. Each chapter will focus on a single
phone feature and provide step-by-step instructions on how to incorporate it into your application.
Where to Find Sources for the Examples
The source code of all of the examples is available at www.apress.com/book/view/1430232161.
Send Us Your Comments
We value your input. We’d like to know what you like about the book and what you don’t like about it.
You can send us comments via e-mail to When providing feedback, please make
sure you include the title of the book in your note to us.
We’ve tried to make this book as error-free as possible. However, mistakes happen. If you find any
type of error in this book, whether it is a typo or an erroneous command, please let us know about it.
Please e-mail the problem to Your information will be validated and posted on the
errata page to be used in subsequent editions of the book. The corrigenda can be viewed on the book’s
web page at .
Contacting the Authors
You can contact us directly at the following e-mail addresses:
Henry Lee:
Eugene Chuvyrov:
Download from Wow! eBook <www.wowebook.com>
P A R T 1
■ ■ ■
The Essentials of
untapped as the market will open to the public on November of 2010, and you will have a chance to
publish an application that is first to the market. You might consider downloading Zune software from
www.zune.net/en-US/products/software/download/downloadsoftware.htm to view the current
Marketplace. Once you have downloaded the Zune software and fired it up, click marketplace ➤ APPS
links, and you will be able to see all the Windows Phone applications currently published, as shown in
Figure 1–1. You will learn more about the Marketplace in Chapter 5.
Figure 1–1. Windows Phone Marketplace