Microsoft Silverlight
Edition: Programming
Windows Phone 7
Charles Petzold
A01T656673.indd iA01T656673.indd i 11/30/2010 12:38:57 PM11/30/2010 12:38:57 PM
Do wn lo ad f ro m Wo w! e Bo ok < ww w. wo we bo ok .c om >
PUBLISHED BY
Microsoft Press
A Division of Microsoft Corporation
One Microsoft Way
Redmond, Washington 98052-6399
Copyright © 2010 by Microsoft Corporation (All)
All rights reserved. No part of the contents of this book may be reproduced or transmitted in any form or by any
means without the written permission of the publisher.
Library of Congress Control Number: 2010941604
ISBN: 978-0-7356-5667-3
Printed and bound in the United States of America.
Microsoft Press books are available through booksellers and distributors worldwide. For further infor mation about
international editions, contact your local Microsoft Corporation offi ce or contact Microsoft Press International
directly at fax (425) 936-7329. Tell us what you think of this book at
Send comments to
Microsoft and the trademarks listed at />Trademark/EN-US.aspx are trademarks of the Microsoft group of companies. All other marks are the property
of their respective owners.
The example companies, organizations, products, domain names, e-mail addresses, logos, people, places, and
events depicted herein are fi ctitious. No association with any real company, organization, product, domain name,
e-mail address, logo, person, place, or event is intended or should be inferred.
This book expresses the author’s views and opinions. The information contained in this book is provided without
any express, statutory, or implied warranties. Neither the authors, Microsoft Corporation, nor its resellers, or
distributors will be held liable for any damages caused or alleged to be caused either directly or indirectly by
this book.
A03C656673.indd iiiA03C656673.indd iii 11/18/2010 4:11:42 PM11/18/2010 4:11:42 PM
v
Table of Contents
Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .xi
Part I The Basics
1 Hello, Windows Phone 7 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
Targeting Windows Phone 7. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
The Hardware Chassis. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
Sensors and Services. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
File | New | Project . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
A First Silverlight Phone Program . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
The Standard Silverlight Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
Color Themes. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
Points and Pixels . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
The XAP is a ZIP. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
An XNA Program for the Phone. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
2 Getting Oriented. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31
Silverlight and Dynamic Layout . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31
Orientation Events . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38
XNA Orientation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40
Simple Clocks (Very Simple Clocks) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43
3 An Introduction to Touch . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49
Low-Level Touch Handling in XNA . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49
The XNA Gesture Interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53
Low-Level Touch Events in Silverlight . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 54
The Manipulation Events . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 58
Routed Events . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61
Some Odd Behavior? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63
What do you think of this book? We want to hear from you!
7 XAML Power and Limitations . . . . . . . . . . . . . . . . . . . . . . . . . . . . 143
A TextBlock in Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 144
Property Inheritance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 146
Property-Element Syntax. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 148
Colors and Brushes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 149
Content and Content Properties . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 156
The Resources Collection. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 159
Sharing Brushes. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 161
x:Key and x:Name . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 164
An Introduction to Styles. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 164
A04T656673.indd viA04T656673.indd vi 11/18/2010 5:02:08 PM11/18/2010 5:02:08 PM
Table of Contents vii
Style Inheritance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 166
Themes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 168
Gradient Accents. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 168
8 Elements and Properties . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 171
Basic Shapes. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 171
Transforms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 173
Animating at the Speed of Video. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 180
Handling Manipulation Events . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 182
The Border Element . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 184
TextBlock Properties and Inlines . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 187
More on Images . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 190
Playing Movies . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 193
Modes of Opacity . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 193
Non-Tiled Tile Brushes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 196
9 The Intricacies of Layout . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 197
The Single-Cell Grid . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 198
The StackPanel Stack . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 200
Text Concatenation with StackPanel . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 204
A New Type of Toggle . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 322
Panels with Properties . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 327
Attached Properties . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 333
12 Data Bindings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 339
Source and Target . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 339
Target and Mode. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 342
Binding Converters. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 344
Relative Source . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 349
The “this” Source. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 350
Notifi cation Mechanisms. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 354
A Simple Binding Server . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 355
Setting the DataContext . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 361
Simple Decision Making . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 366
Converters with Properties . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 370
Give and Take. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 374
TextBox Binding Updates. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 380
13 Vector Graphics. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 393
The Shapes Library . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 393
Canvas and Grid . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 394
Overlapping and ZIndex . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 396
Polylines and Custom Curves . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 398
Caps, Joins, and Dashes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 403
Polygon and Fill. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 410
The Stretch Property. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 412
Dynamic Polygons . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 413
A04T656673.indd viiiA04T656673.indd viii 11/18/2010 5:02:09 PM11/18/2010 5:02:09 PM
Table of Contents ix
The Path Element . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 416
Geometries and Transforms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 422
Grouping Geometries. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 427
Examining the Visual Tree . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 578
ControlTemplate Basics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 583
The Visual State Manager . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 594
A04T656673.indd ixA04T656673.indd ix 11/18/2010 5:02:09 PM11/18/2010 5:02:09 PM
x Table of Contents
Sharing and Reusing Styles and Templates . . . . . . . . . . . . . . . . . . . . . . . . . . 603
Custom Controls in a Library. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 606
Variations on the Slider . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 612
The Ever-Handy Thumb . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 622
Custom Controls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 626
17 Items Controls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 633
Items Controls and Visual Trees . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 634
Customizing Item Displays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 641
ListBox Selection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 645
Binding to ItemsSource . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 649
Databases and Business Objects. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 655
Fun with DataTemplates. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 672
Sorting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 675
Changing the Panel . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 680
The DataTemplate Bar Chart . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 682
A Card File Metaphor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 689
18 Pivot and Panorama . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 701
Compare and Contrast. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 701
Music by Composer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 712
The XNA Connection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 715
The XNA Music Classes: MediaLibrary. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 717
Displaying the Albums. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 723
The XNA Music Classes: MediaPlayer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 728
Index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 735
What do you think of this book? We want to hear from you!
community aspect of game development. Nor do I discuss any programming tools beyond
Visual Studio—not even Expression Blend.
My publisher Microsoft Press has a couple additional Windows Phone 7 books coming soon:
Windows Phone 7 Silverlight Development Step by Step by Andy Wigley & Peter Foot offers
a more tools-oriented approach. Although Michael Stroh’s Windows Phone 7 Plain & Simple
is a guide to using the phone rather than developing for it, I suspect it will give developers
some insights and ideas.
Moreover, I also hear that my old friend Doug Boling is working hard on a Windows Phone
7 enterprise-programming book that is likely to be considered his masterpiece. Be sure to
check out that one.
A05I656673.indd xiA05I656673.indd xi 12/2/2010 3:30:19 PM12/2/2010 3:30:19 PM
xii Introduction
Organization
This book is divided into three parts. The fi rst part discusses basic concepts of Windows
Phone 7 programming using example programs that target both Silverlight and the XNA
framework. It is likely that many Windows Phone 7 developers will choose either one
platform or the other, but I think it’s important for all developers who have at least a little
knowledge of the alternative to their chosen path.
The second part of this book focuses entirely on Silverlight, and the third part on XNA
2D. For your convenience, the chapters in each part build upon previous knowledge in
a progressive tutorial narrative, and hence are intended to be read sequentially.
My Assumptions About You
I assume that you know the basic principles of .NET programming and you have a working
familiarity with the C# programming language. If not, you might benefi t from reading my
free online book .NET Book Zero: What the C or C++ Programmer Needs to Know about C#
and the .NET Framework, available from my website at www.charlespetzold.com/dotnet.
System Requirements
To use this book properly you’ll need to download and install the Windows Phone
Developer Tools, which includes Visual Studio 2010 Express for Windows Phone, XNA Game
Studio 4.0, and an on-screen Windows Phone Emulator to test your programs in the absence
Some people like to learn new programming environments by re-creating the projects in
Visual Studio and typing in the source code themselves from the pages of the book. Others
prefer to study the code and run the pre-existing programs to see what the code does. If
you fall into the latter category, you can download all the source code in a ZIP fi le via the
Companion Content link at />If you fi nd something in the code that is useful in your own software project, feel free to use
the code without restriction—either straight up or modifi ed in whatever way you want. That’s
what it’s there for.
Last-Minute Items
As I was nearing the completion this book, the fi rst version of the Silverlight for Windows
Phone Toolkit was released with some additional elements and controls, and is available for
downloading at . Historically, these Silverlight toolkits very often
contain previews of elements and controls that are incorporated into later Silverlight releases.
I regret that I could not include a discussion of the toolkit contents in the appropriate
chapters of this book.
With XNA programs, sometimes Visual Studio complains that it can’t build or deploy
the program. If you encounter that problem, in the Solution Platforms drop-down list
on the standard toolbar, select “Windows Phone” rather than “Any CPU”. Or, invoke the
A05I656673.indd xiiiA05I656673.indd xiii 12/2/2010 3:31:13 PM12/2/2010 3:31:13 PM
Do wn lo ad f ro m Wo w! e Bo ok < ww w. wo we bo ok .c om >
xiv Introduction
Confi guration Manager from the Build menu, and in the Active Solution Platform drop-down
select “Windows Phone” rather than “Any CPU”.
The www.charlespetzold.com/phone page on my website will contain information about this
book and perhaps even some information about a future edition. I also hope to blog about
Windows Phone 7 programming as much as possible.
The Essential People
This book owes its existence to Dave Edson—an old friend from the early 1990s era
of Microsoft Systems Journal—who had the brilliant idea that I would be the perfect person
to write a tutorial on Windows Phone 7. Dave arranged for me to attend a technical deep
dive on the phone at Microsoft in December 2009, and I was hooked. Todd Brix gave the
We Want to Hear from You
At Microsoft Press, your satisfaction is our top priority, and your feedback our most valuable
asset. Please tell us what you think of this book at:
/>The survey is short, and we read every one of your comments and ideas. Thanks in advance
for your input.
Stay in Touch
Let’s keep the conversation going! We’re on Twitter: />A05I656673.indd xvA05I656673.indd xv 12/2/2010 3:31:14 PM12/2/2010 3:31:14 PM
A05I656673.indd xviA05I656673.indd xvi 12/2/2010 3:31:14 PM12/2/2010 3:31:14 PM
1
Part I
The Basics
C01656673.indd 1C01656673.indd 1 11/18/2010 4:10:25 PM11/18/2010 4:10:25 PM
3
Chapter 1
Hello, Windows Phone 7
Sometimes it becomes apparent that previous approaches to a problem haven’t quite worked
the way you anticipated. Perhaps you just need to clear away the smoky residue of the past,
take a deep breath, and try again with a new attitude and fresh ideas. In golf, it’s known as
a “mulligan”; in schoolyard sports, it’s called a “do-over”; and in the computer industry, we
say it’s a “reboot.”
A reboot is what Microsoft has initiated with its new approach to the mobile phone market.
With its clean look, striking fonts, and new organizational paradigms, Microsoft Windows
Phone 7 not only represents a break with the Windows Mobile past but also differentiates
itself from other smartphones currently in the market. Windows Phone 7 devices will be
made by several manufacturers and available with a variety of cell providers.
For programmers, Windows Phone 7 is also exciting, for it supports two popular and modern
programming platforms: Silverlight and XNA.
Silverlight—a spinoff of the client-based Windows Presentation Foundation (WPF)—has
already given Web programmers unprecedented power to develop sophisticated user
Internet Applications (RIA), including media and the Web. Silverlight for Windows Phone
is a version of Silverlight 3 excluding some features not appropriate for the phone, but
compensating with some enhancements.
XNA is primarily for writing high-performance games. For 2D games, you defi ne sprites and
backgrounds based around bitmaps; for 3D games you defi ne models in 3D space. The action
of the game, which includes moving graphical objects around the screen and polling for user
input, is synchronized by the built-in XNA game loop.
The differentiation between Silverlight-based applications and XNA-based games is
convenient but not restrictive. You can certainly use Silverlight for writing games and you
can even write traditional applications using XNA, although doing so might sometimes be
challenging.
In particular, Silverlight might be ideal for games that are less graphically oriented, or
use vector graphics rather than bitmap graphics, or are paced by user-time rather than
clock-time. A Tetris-type program might work quite well in Silverlight. You’ll probably fi nd
XNA to be a bit harder to stretch into Silverlight territory, however. Implementing a list box in
XNA might be considered “fun” by some programmers but a torture by many others.
The fi rst several chapters in this book describe Silverlight and XNA together, and then the book
splits into different parts for the two platforms. I suspect that some developers will stick with
either Silverlight or XNA exclusively and won’t even bother learning the other environment.
I hope that’s not a common attitude. The good news is that Silverlight and XNA are so
dissimilar that you can probably bounce back and forth between them without confusion!
Microsoft has been positioning Silverlight as the front end or “face” of the cloud, so cloud
services and Windows Azure form an important part of Windows Phone 7 development. The
Windows Phone is “cloud-ready.” Programs are location-aware and have access to maps and
other data through Bing and Windows Live. One of the available cloud services is Xbox Live,
C01656673.indd 4C01656673.indd 4 11/18/2010 4:10:40 PM11/18/2010 4:10:40 PM
Chapter 1 Hello, Windows Phone 7 5
which allows XNA-based programs to participate in online multiplayer games, and can also
be accessed by Silverlight applications.
Programs you write for the Windows Phone 7 will be sold and deployed through the
Search The operating system uses this button to initiate a search feature.
C01656673.indd 5C01656673.indd 5 11/18/2010 4:10:40 PM11/18/2010 4:10:40 PM
6 Part I The Basics
The initial releases of Windows Phone 7 devices have a display size of 480 × 800 pixels. In the
future, screens of 320 × 480 pixels are also expected. There are no other screen options for
Windows Phone 7, so obviously these two screen sizes play a very important role in phone
development.
In theory, it’s usually considered best to write programs that adapt themselves to any screen
size, but that’s not always possible, particularly with game development. You will probably
fi nd yourself specifi cally targeting these two screen sizes, even to the extent of having
conditional code paths and different XAML fi les for layout that is size-dependent.
I will generally refer to these two sizes as the “large” screen and the “small“ screen. The
greatest common denominator of the horizontal and vertical dimensions of both screens is
160, so you can visualize the two screens as multiples of 160-pixel squares:
I’m showing these screens in portrait mode because that’s usually the way smartphones are
designed. The screen of the original Zune is 240 × 320 pixels; the Zune HD is 272 × 480.
Of course, phones can be rotated to put the screen into landscape mode. Some programs
might require the phone to be held in a certain orientation; others might be more adaptable.
You have complete control over the extent to which you support orientation. By default,
Silverlight applications appear in portrait mode, but you’ll probably want to write your
Silverlight applications so they adjust themselves to orientation changes. New events are
available specifi cally for the purpose of detecting orientation change, and some orientation
shifts are handled automatically. In contrast, game programmers can usually impose
a particular orientation on the user. XNA programs use landscape mode by default, but it’s
easy to override that.
In portrait mode, the small screen is half of an old VGA screen (that is, 640 × 480). In
landscape mode, the large screen has a dimension sometimes called WVGA (“wide VGA”). In
C01656673.indd 6C01656673.indd 6 11/18/2010 4:10:40 PM11/18/2010 4:10:40 PM
Chapter 1 Hello, Windows Phone 7 7
Wi-Fi The phone has Wi-Fi for Internet access to complement 3G data access through
the cell provider. Software on the phone includes a version of Internet Explorer.
■
Camera The phone has at least a 5-megapixel camera with fl ash. Programs can
invoke the camera program for their own input, or register themselves as a Photos
Extra Application and appear on a menu to obtain access to photographed images,
perhaps for some image processing.
C01656673.indd 7C01656673.indd 7 11/18/2010 4:10:41 PM11/18/2010 4:10:41 PM
8 Part I The Basics
■
Accelerometer An accelerometer detects acceleration, which in physics is a change
in velocity. When the camera is still, the accelerometer responds to gravity. Programs
can obtain a three-dimensional vector that indicates how the camera is oriented with
respect to the earth. The accelerometer can also detect sharp movements of the phone.
■
Location If the user so desires, the phone can use multiple strategies for determining
where it is geographically located. The phone supplements a hardware GPS device with
information from the Web or cell phone towers. If the phone is moving, course and
speed might also be available.
■
Vibration The phone can be vibrated through program control.
■
FM Radio An FM Radio is available and accessible through program control.
■
Push Notifi cations Some Web services would normally require the phone to
frequently poll the service to obtain updated information. This can drain battery life.
To help out, a push notifi cation service has been developed that will allow any required
polling to occur outside the phone and for the phone to receive notifi cations only
when data has been updated.
File | New | Project