Windows Phone 7 Development Internals

  ® Windows Phone 7

Development Internals Andrew Whitechapel Published with the authorization of Microsoft Corporation by: O’Reilly Media, Inc. 1005 Gravenstein Highway North Sebastopol, California 95472 Copyright © 2012 by Andrew Whitechapel.

  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.

  ISBN: 978-0-7356-6325-1 1 2 3 4 5 6 7 8 9 LSI 6 5 4 3 2 1 Printed and bound in the United States of America. Microsoft Press books are available through booksellers and distributors worldwide. If you need support related to this book, email Microsoft Press Book Support at mspinput@microsoft.com. Please tell us what you think of this book at http://www.microsoft.com/learning/booksurvey. Microsoft and the trademarks listed at http://www.microsoft.com/about/legal/en/us/IntellectualProperty/

  

Trademarks/EN-US.aspx are trademarks of the Microsoft group of companies. All other marks are property of

their respective owners.

  The example companies, organizations, products, domain names, email addresses, logos, people, places, and events depicted herein are fictitious. No association with any real company, organization, product, domain name, email 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, O’Reilly Media, Inc., 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.

  Acquisitions Editor: Russell Jones Developmental Editor: Russell Jones Production Editor: Melanie Yarbrough Editorial Production: Octal Publishing, Inc. Technical Reviewer: Peter Torr Copyeditor: Bob Russell Indexer: WordCo Indexing Services Cover Design: Twist Creative • Seattle Cover Composition: Karen Montgomery Illustrator: Robert Romano

  Contents at a Glance

  PART I

  

  

  ChAPter 4

  

  PART II ChAPter 16

  ChAPter 17

  ChAPter 18 ChAPter 20

  Contents

  PART I

  

  

  

  

  

  

  

Chapter 2 uI Core

  

  

  

  

  

  v

  

  

  Chapter 3

  

  

  

  

  

  

  vi Contents

Chapter 5 Touch uI

  

  

  

  

  

  

  PART II vii

  

  

  Chapter 7 viii Contents

  

  

  

  

  PART III ix

  x Contents

  

  

Chapter 11 Web and Cloud 349 The WebClient Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .349 WebClient: The DownloadStringAsync Method . . . . . . . . . . . . . . . .349 WebClient: The OpenReadAsync Method . . . . . . . . . . . . . . . . . . . . .351 The HttpWebRequest Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .353 Web Browser Control . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .353 Silverlight and Javascript . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .355 Web Services . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .358 WCF Data Services . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .361 The OData Client and XML Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . .361 JSON-Formatted Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .369 Bing Maps and Geolocation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .372 Using the Map Control . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .372 Geolocation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .374 Bing Maps Web Services . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .375 Deep Zoom (MultiScaleImage) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .378 Windows Azure . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .383 Windows Azure Web Services . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .385 Windows Azure Toolkit for Windows Phone . . . . . . . . . . . . . . . . . . .390

  

  

  Chapter 12 xi

  

Chapter 14 go to Market

  

  

  

  

  

  

  

  xii Contents

  PART Iv xiii

  Chapter 17

  

  

  

  

  xiv Contents

Chapter 19 Framework Enhancements 711 Navigation Enhancements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .711 Frame and Page Navigation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .711 Backstack Management . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .714 UI Enhancements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .717 Enhanced Controls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .718 The ApplicationBar and SystemTray Classes,

  xv

  

  

  

  

Chapter 20 Tooling Enhancements 745 Emulator Improvements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .745 Debugger Experience . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 747 Marketplace Test Kit . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .749 The Profiler . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .754 UserVoice Forums . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .764 Portable Library Tools . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .765 Async Framework . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .769 Summary. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .772 Index 773

  Foreword

  o, you’re curious about Windows Phone development? Welcome aboard! Whether you’re an existing Microsoft Silverlight developer wanting to branch out into the mobile space, an existing

  S

  mobile developer looking to extend your reach across a second or third ecosystem, a rising star who’s ready to create the Next Big Thing and take the world by storm, or maybe just a curious phone user who wants to know what all the “app” fuss is about, Windows Phone is the platform for you.

  Getting started with Windows Phone development is free and easy; everything you need to write apps is just a couple of clicks away. You can have your first app up and running in a matter of minutes, even if you know next to nothing about Windows Phone development or don’t even own a device. As your apps become more ambitious and you encounter more complex development issues, a vibrant developer community on the web is ready and willing to help you out along the way. Mastery of this platform, with its rich feature set, unique application model, integrated end-to-end experiences, and burgeoning international marketplace, takes time and effort—and an expertly written guide. Luckily for you, this book is just such a guide.

  The Windows Phone platform stands on the shoulders of giants—giants such as Silverlight, XNA, Microsoft Visual Studio, and Microsoft Expression Blend—and as we built the platform, we embraced the power and familiarity that these existing technologies afforded us; our goal was to introduce new concepts only when strictly necessary to enable new scenarios, and to re-use existing concepts every- where else. We spent less time re-solving old problems (such as navigation and microphone capture) and more time tackling new ones so that we could ship a vast array of new phone-specific APIs for developers to wrap their heads around—cameras, gyroscopes, multi-tasking, phone integration, user data access, and live tile updates, just to name a few—and Andrew covers all of them (and more!) in this book.

  As you’ve probably heard (and seen), Windows Phone ushered in a new design language for Microsoft, code-named “Metro.” Adhering closely to this design is critical when building user experi- ences that will delight and engage your customers. As you would expect, the Windows Phone devel- oper tools give you a big helping hand in this department, providing user interface elements and ap- plication templates that “just work” by default. Nevertheless, as the owner of your application’s overall experience you are ultimately responsible for ensuring it performs optimally, adhering not just to the graphic design rules, such as “content over chrome,” but also the fast and fluid interaction model that your customers will come to expect. Throughout this book you’ll find practical examples and guid- ance that show how to embody the Metro design language in your applications, along with examples of common pitfalls and how to avoid them—particularly with respect to application performance and responsiveness, which are key factors in user satisfaction (and hence, app ratings and profitability).

  xvii On a more personal note, I was thrilled when Andrew asked me to tech-review this book (although writing this foreword was more than a little daunting!). As an infrastructure guy at heart, I love build- ing platforms and enabling developers to be successful on top of them, but there are only so many people you can reach via blogs or conference speaking sessions. I’ve been asked to author books before, but I've never had the time or inclination to do so. By piggybacking on Andrew’s hard work with this book, I feel like I’ve made a difference—if only a small one—and that makes me grateful for the opportunity. I also learned a lot while reviewing this book, and I know that you will, too.

  Peter Torr

  Program Manager in the Windows Phone Application Platform team xviii Foreword

  Introduction

  he smart phone is increasingly important in people’s daily lives. It is used for a wide variety of tasks, both work-related and non-work related. People use smart phones to keep up to date

  T

  with friends and family, for relaxation, and for entertainment, as well as for viewing documents and spreadsheets, surfing the Internet, and enriching their lives. There is therefore considerable scope for building smart phone applications. Windows Phone is not just another smart phone; rather, it is positioned as an opportunity for developers to build applications that can make a real difference to people’s lives. The platform has been designed from the ground up to support an all-encompassing, integrated, and attractive user experience.

  Windows Phone 7 Development Internals covers the breadth of application development for the

  Windows Phone platform, both the major 7 and 7.1/7.5 versions and the minor 7.1.1 version, and shows how you can build such compelling and useful applications. You can build applications for Windows Phone 7.x by using either the Microsoft Silverlight runtime or the XNA runtime. This book focuses on Silverlight applications. The primary development and design tools are Microsoft Visual Studio and Microsoft Expression Blend. Here again, this book focuses on Visual Studio.

  Each chapter covers a handful of related features. For each feature, the book provides one or more sample applications and walks you through the significant code (C# and XAML). This will help you to understand the techniques used and also the design and implementation choices that you have in each case. Potential pitfalls are called out, as are scenarios in which you can typically make per- formance or user experience improvements. An underlying theme is to conform not only to the user interface design guidelines, but also to the notion of a balanced, healthy phone ecosystem.

  Who should Read This Book

  This book is intended to help existing developers understand the core concepts, the significant pro- grammable feature areas, and the major techniques in Windows Phone development. The book is tai- lored for existing Silverlight developers that want to jump into the exciting world of mobile application developer with the Windows Phone platform. Developers experienced with other mobile platforms will find this book invaluable in learning the ins and outs of Microsoft’s operating system, but will likely need additional resources to pick up C# and XAML languages.

  The Windows Phone 7 release only supports C#, and although support for Visual Basic was intro- duced with the 7.1 SDK, this book focuses purely on C# and XAML. The basic architecture of the platform is covered in Chapter 1, “Vision and Architecture,” and most chapters go deeply into the internal behavior of the system. This is knowledge that helps to round out your understanding of the platform, and inform your design decisions, even though, in some cases, the internal details have no immediate impact on the exposed API.

  xix

  Assumptions

  The book assumes that you have a reasonable level of experience of developing in managed code, specifically in C#. Basic language constructs are not discussed, nor is basic use of Visual Studio, the project system or the debugger, although more advanced techniques, and phone-specific features are, of course, explained in detail. You should also have some knowledge of XAML development, preferably in Silverlight, although Windows Presentation Foundation experience would also be useful background.

  Although many component-level diagrams are presented as high-level abstractions, there are also many sections that describe the behavior of the feature in question through the use of UML sequence diagrams. It helps to have an understanding of sequence diagrams, but it is not essential, as they are fairly self-explanatory.

  Who should not Read This Book

  This book is not intended for use by application designers—if designers are defined as developers who use Expression Blend—although designers might find it useful to understand some of the issues facing developers in the Windows Phone application space. The book is also not suitable for XNA developers because it does not cover game development at all.

  organization of This Book

  Windows Phone 7 was first released in October 2010. The first major update, code-named “Mango,” was released in September 2011. The Mango release includes a wide range of enhancements and additional features. Note that the user-focused version number for the Mango release (that is, the product version) is version 7.5; however, the developer-focused number is 7.1 (for both the OS version and the SDK version). The reason for this slightly confusing numbering situation is that the Mango release includes improvements across the board—in the operating system, the developer tooling, in the emulator, in the application platform, in Silverlight itself, and also in the server-side experience of marketplace, and in ingestion. All of this is Windows Phone, or Windows Phone 7.5. A developer is normally focused more on the pure technical aspects: the operating system, tooling, and application platform subset of the overall release, and that is technically the 7.1 release (both SDK and OS).

  This book covers all 7.x versions: the original Windows Phone 7 release, the later Windows Phone 7.1 release, and the minor 7.1.1 release. Applications built for version 7 also work without change on 7.1 devices. Note that, while there are still about a million version 7 phones in use, it is safe to assume that most of these will be upgraded to 7.1 at some point. However, to keep things simple, the first 14 chapters focus on the basic infrastructure, programming model, and the core features that are common to both versions. Where there are material differences, these are called out, with references to the later chapter where the 7.1 behavior is explained in detail. Chapter 15, “Multi-Tasking and Fast App Switching,” onward focuses on the features and platform enhancements that are specific to version 7.1.

  xx Introduction

  The 7.1.1 version is a narrowly scoped release intended to support phones with low memory capabilities (256 MB) for specific target markets. Most developers—and most applications—will not be affected by this. For the small number that might be affected, the 7.1.1 release provides additional support for performance tuning and an additional marketplace submission option, as discussed in

  Chapter 16, “Enhanced Phone Services.” It’s also worth reading Chapter 14, “Go To Market” ahead of time. This chapter focuses on the end-game of bringing your application to market, including tuning the design for performance and robustness, and marketplace certification. Even before you have a thorough understanding of the architecture and fundamentals, it is instructive to see what you’ll be aiming for.

  Conventions and Features in This Book

  This book presents information by using conventions designed to make the information readable and easy to follow.

  ■

  In some cases, especially in the early chapters, application code is listed in its entirety. More often, only the significant code is listed. Wherever code has been omitted for the sake of brev- ity, this is called out in the listing. In all cases, you can refer to the sample code that accompa- nies this book for complete listings.

  ■

  In the XAML listings, attributes that are not relevant to the topic under discussion, and that have already been explained in previous sections, are omitted. This applies, for example, to Grid.Row,

  Grid.Column, Margin, FontSize, and similarly trivial attributes. In this way, you can focus on the

  elements and attributes that do actually contribute to the feature at hand, without irrelevant distractions.

  ■

  Code identifiers (the names for classes, methods, properties, events, enum values, and so on) are all italicized in the text.

  ■

  In the few cases where two or more listings are given with the explicit aim of comparing alter- native techniques (or “before” and “after” scenarios), the differences appear in bold.

  ■

  Boxed elements with labels such as “Note” provide additional information or alternative meth- ods for completing a step successfully.

  ■ Text that you type (apart from code blocks) appears in bold. ■

  A plus sign (+) between two key names means that you must press those keys at the same time. For example, “Press Alt+Tab” means that you hold down the Alt key while you press the Tab key.

  ■

  A vertical bar between two or more menu items (for example, File | Close), means that you should select the first menu or menu item, then the next, and so on.

  Introduction xxi

  system Requirements

  You can build and run the accompanying sample code, or you can create your own solutions from scratch, following the instructions in the text. In either case, you will need the following hardware and software to create the sample applications in this book:

  ■

  Either Windows Vista (x86 and x64) with Service Pack 2, all editions except the Starter Edition, or Windows 7 (x86 and x64), all editions except the Starter Edition. If you install the Windows Phone SDK 7.1.1 Update, this also works with the Windows 8 Consumer Preview, although this is not a supported configuration.

  ■

  The Windows Phone SDK version 7.0 or 7.1. These are both free downloads that include Visual Studio 2010 Express Edition and all other standard tools, as listed in Chapter 1. If you install the SDK version 7.1, you can then also upgrade this with the SDK version 7.1.1. This is an update to 7.1, not a stand-alone install.

  ■

  Some of the server-side sample projects require Visual Studio Professional, but all of the Win- dows Phone samples work with Visual Studio Express.

  ■

  Installing the SDK requires 4 GB of free disk space on the system drive. If you use the profiler (described in Chapter 20, “Tooling Enhancements”) for an extended period, you will need considerably more disk space.

  ■ 4 GB RAM (8 GB recommended). ■

  Windows Phone Emulator requires a DirectX 10 or above capable graphics card with a WDDM 1.1 driver.

  ■ 2.6 GHz or faster processor (4GHz or 2.6GHz dual-core, recommended). ■

  Internet connection to download additional software or chapter examples, and for testing web-related applications. Depending on your Windows configuration, you might require Local Administrator rights to install or configure Visual Studio 2010, and to install or configure features such as Internet Information

  Services, if not already installed.

  For the latest requirements, visit the Windows Phone SDK download page a

  . xxii Introduction

  Code samples

  All of the chapters in this book include multiple sample solutions with which you can interactively try out new material learned in the main text. All sample projects can be downloaded from the following page:

   Follow the instructions to download the WP7xDevInternals.zip file.

  Installing the Code Samples

  Follow these steps to install the code samples on your computer so that you can refer to them while learning about the techniques that they demonstrate.

  1. Unzip the WP7xDevInternals.zip file that you downloaded from the book’s website to any suit-

  able folder on your local hard disk. The sample code expands out to nearly 200 MB, and you will need even more space for the binaries if you choose to build any of the samples.

  2. If prompted, review the displayed end-user license agreement. If you accept the terms, select the accept option, and then click Next.

  Note If the license agreement doesn’t appear, you can access it from the same webpage from which you downloaded the WP7xDevInternals.zip file.

  Using the Code Samples

  When you unzip the sample code, this creates a number of subfolders, one for each chapter. Within each chapter’s subfolder there are further subfolders. In most cases, there is one subfolder per applica- tion (or per version of an application), but in some cases, multiple applications are grouped together; for example, where there is a server-side application as well as a client-side application in the solution. In keeping with the book's structure, the samples for the first 14 chapters were built as version 7 proj- ects, and the remaining samples were built as version 7.1 projects. However, you can use the version 7.1 (or 7.1.1) SDK for all the sample projects, for all chapters.

  All of the samples are complete, fully functioning applications. Note, however, that in some cases, you might need to update assembly references, depending on where you install the SDK as well as where you install supplementary libraries and frameworks that don’t ship with the main SDK (for instance the Silverlight toolkit, Live SDK, Azure toolkit, and so on).

  For samples that demonstrate the use of some supplementary framework, you will need to down- load and install that framework so that you can reference its assemblies. Also note that, in some cases, this requires a user ID, such as for Bing maps, FaceBook, or Google Analytics, as described in the relevant sections. In all cases, you can sign up for the ID without charge as of the time of this writing.

  Introduction xxiii

  Acknowledgments

  The Windows Phone development space is truly inspiring, and the Windows Phone teams at Microsoft are chock-full of smart, helpful people. The list of folks who helped me prepare this book is very long. I’d particularly like to thank Peter Torr for doing all the heavy lifting in the technical review. It’s mainly thanks to Peter that this book isn’t riddled with schoolboy errors. In addition, I’d like to thank all the other people who answered my dumb questions, and corrected my various misinterpretations of the internal workings of the platform, especially Tim Kurtzman, Wei Zhang, Jason Fuller, Vij Vasu, Abolade Gbadegesin, Andrew Clinick, Darin Miller, Mark Paley, Jeff Wilcox, Thomas Fennel, Matt Klupchak, Alper Selcuk, Gary Lin, Conrad Chang, Justin Horst, Sai Prasad Patro, Yasser Shaaban, Mike Battista, Jorge Raastroem, and Joao Guberman Raza. I’d also like to thank the folks at O’Reilly Media and Mi- crosoft Press who helped to turn my scattered thoughts into polished prose, especially Russell Jones, Devon Musgrave, Melanie Yarbrough, and Bob Russell (at Octal Publishing, Inc.). Finally, none of this would have been possible without the patience and support of Narins Bergstrom.

  Errata & Book support

  We’ve made every effort to ensure the accuracy of this book and its companion content. Any er- rors that have been reported since this book was published are listed on our Microsoft Press site at oreilly.com:

   If you find an error that is not already listed, you can report it to us through the same page.

  If you need additional support, email Microsoft Press Book Support at Please note that product support for Microsoft software is not offered through the addresses above.

  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

  xxiv Introduction

  Pa r t I Building Blocks

ChAPTER 1 Vision and Architecture . . . . . . . . . . . . . . . . . . . . . . . . 3 ChAPTER 2 UI Core . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .29 ChAPTER 3 Controls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .61 ChAPTER 4 Data Binding and Layer Decoupling. . . . . . . . . . . . . 91 ChAPTER 5 Touch UI. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .135 This part describes the overall vision and architecture of Windows Phone applications, and the basic building blocks that every application requires in order to support minimum functionality. This includes the core user interface features, the various stan- dard controls, touch manipulation, and data-binding.

  1

  C h A P T E R 1 Vision and Architecture

  here are two dimensions to this chapter: an introduction to the vision of the Windows Phone plat- form and the revolutionary Metro design philosophy, and an introduction to the basic architec-

  T

  ture and building blocks of a Windows Phone application. The vision is extremely important; it should inform the way you think about applications, from first concept through design to implementation and final publication. The application platform provides support and guidance, such that it’s quite difficult to build a truly bad application, but it is definitely possible to build an application that is in conflict with the system and fails to provide a good user experience (UX). An understanding of the vision should steer you toward good design choices and help you to identify potential pitfalls, thereby accelerating your development cycle.

  Windows Phone vision

  “Windows Phone is a software platform that can improve a person’s life.” This is a quote from the

  

Microsoft User Experience Design Guidelines (kept up-to-date online a. It is a philosophy on which the teams that are responsible for

  building the Windows Phone operating system, application platform, and SDK are highly focused. It’s not just marketing-speak, and it’s not just an inspirational motto—it really does speak to the core vision of the platform.

  Traditional desktop operating systems such as Windows 7 and Mac OS are used for all sorts of rea- sons and purposes, but predominantly for work-related activities. So applications built for the desktop (including browser clients for web applications) are typically business-focused (although buttoned- down PCs are also increasingly being used for social networking applications like Facebook). Applica- tions for Microsoft Xbox and other game consoles are predominantly geared toward entertainment. The idea behind the Windows Phone UX model is that this is a device that users turn to for every- thing—for work and play, for keeping in touch with friends and family, for relaxation, games, music and videos, for research and fact-finding—in short, for enhancing their day-to-day lives. As develop- ers, you have the weighty responsibility of living up to this vision. You are encouraged (and extensively supported through the application platform) to build applications that do improve the user’s life.

  A good Windows Phone application is engaging, compelling, and attractive to the user. A great Windows Phone application can make a meaningful contribution to the user’s life. The user interac- tion is well thought out, predictable, and familiar. It gives him enjoyment every time he uses it, or it fulfills some pragmatic need in a simple, unobtrusive manner. The application integrates seamlessly

  3 with standard features on the phone, such as email, contacts, photos, and so on. It’s a ”team player,” cooperating with the system. It’s conservative in its use of CPU, memory, and sensor resources. It doesn’t consume battery power in an unbounded way, and it is thoughtful about its use of the user’s data plan. The application should have a way to keep itself up-to-date so that its data is always fresh. It can also be location-aware; this way, it can ensure that it is always relevant to the user’s context, and functions in the smart way that user’s expect from a “smart” phone.

  A good application should also look and feel like it’s a part of the overall phone ecosystem, using the Metro design guidelines in an appropriate way, so that it doesn’t jar the senses or look out of place. The same applies to navigation and the way the application behaves when the user switches away from it, and perhaps comes back to it later. It should be configurable, so that the user can per- sonalize it, just as he can personalize other parts of the phone experience, such as the Start page, the theme, and accent colors. There should be a way for the user to provide feedback to the developer, to report bugs, or to request new features. The user should be able to consider the phone as an exten- sion of his personality, configured the way he likes it, and tailoring the set of installed applications to suit his own personal needs and preferences.

  Metro Metro is the code name for the design language used in establishing the UX for Windows Phone.

  Metro is modern and clean, light, open, and fast. Metro is alive and in motion—hence, the extensive use of page transition animations and the considerable thought that has gone into perfecting the standard animations to provide a visually pleasing experience. Consider just one feature: when the user swipes to scroll through a list, the scrolling speed closely matches the force of the swipe gesture. It responds immediately when the user holds a finger on the screen to stop the scrolling. If the user drags a list down (or up) to its limits, there’s a visible compression effect, which provides clear feed- back that she’s reached the end of the list. When she releases her finger, the compression bounces back in a pleasing and intrinsically recognizable way that makes the phone seem almost organically alive. The standard animations have all been designed to provide just the right level of feedback— again, in a pleasing way, without becoming intrusive or distracting.

  You are encouraged to use the Metro design style for your application. Metro covers all aspects of the user interface (UI), including the use of space, control styles, and typography. Content is seen as an important part of any application; this is in deliberate and emphatic contrast to chrome (that is, artifacts of the UI that allow the user to manipulate the UI, such as buttons and grab-handles that are presented in addition to the actual content). Other phone platforms focus a lot on chrome—shiny buttons, gradient fills, 3D images, arbitrary custom animations, and so on. All these chrome effects might seem entertaining for a brief period, but they soon become a distraction, and they also lead to a cluttered, inconsistent, confusing UI. By contrast, Windows Phone and Metro considers content to be king, whereas chrome should always be unobtrusive: it should serve its purpose, and then get out of the user’s way. Think about the implementation of the system tray; most of the time, this is invisible. If the user wants to see it, she taps the top of the screen, the tray drops down with a slight bounce, stays around long enough for the user to see the information she needs, and then disappears, leaving

4 PART I Building Blocks

  the screen free for the application. It also has no shiny, colorful, arbitrarily animated distractions. The same applies for the application bar.

  The aim is to achieve a UI that looks and feels clean, light, and open. Figure 1-1 shows an example of a panorama-based application in the spirit of Metro: clean lines, no gradient fills, content-driven.

  FIguRE 1-1 Metro applications should be clean and open.

  This really represents a major shift in the way you design applications. What you might call the “90s aesthetic” was all about iconography, whereby the chrome took center stage, and the model was hyper-realistic. Metro brings a content-focused aesthetic, through which content is represented in its cleanest form, without embellishment or adornment, and without distractions. The premise is that the user can get to the information directly, without having to navigate some developer’s view of what is suitable UI decoration. Consider, for example, the scrollbar. In Metro, this is mostly invisible; the user scrolls the content, not some shiny scrollbar chrome thumb or widget. This is especially important on a mobile device, given the smaller screen real estate and the use of a touch-based input model.

  Metro celebrates typography: after all, this is a large part of what the user will see in all applica- tions, so the typography must be true to the Metro principles. To embody this principle, the font selected as the standard system font on Windows Phone is Segoe WP (in regular, bold, semi-bold, semi-light and black variants), as shown in Figure 1-2.

ChAPTER 1 Vision and Architecture

   5

  FIguRE 1-2 Segoe WP is the standard font on Windows Phone.

  The FontSize values for the standard theme fonts are listed in Table 1-1.

  TABlE 1-1 Standard Theme Font Sizes Resource name size in Points size in Pixels PhoneFontSizeSmall

  14 18.667

  PhoneFontSizeNormal

  15

  20 PhoneFontSizeMedium 17 22.667

  PhoneFontSizeMediumLarge

  19 25.334

  PhoneFontSizeLarge

  24

  32 PhoneFontSizeExtraLarge 32 42.667

  PhoneFontSizeExtraExtraLarge

  54

  72 PhoneFontSizeHuge 140 186.667 In addition to the design guidelines, two further UI/UX guidance resources are available from MSDN a

  ■

The Windows Phone Design system—Codename Metro A visual explanation of the

  inspiration behind the Windows Phone design system, including the Red Threads principles (Personal, Relevant, Connected).

  ■ Design Templates for Windows Phone 7 A collection of 28 layered Photoshop templates that help designers to maintain a consistent look and feel across applications.

  The focus is on building applications that can take part in a holistic UX. This extends beyond the visuals to include all aspects of the application. Navigation, for example, is deliberately enforced to be very simple, very linear, and predictable. The user can quickly become familiar with the standard navi- gation model, and all applications follow this model. Thus, the user can quickly become familiar with any new application. There are constraints on what your application can do to avoid interfering with this predictability. The user must be able to trust the hardware, so all the hardware buttons always behave the same way, and you cannot override this in a damaging way. The Start button always goes to the Start page. The Back button always goes back in navigation (both within an application’s pages, and between applications in the backstack). When the user backs up to the beginning of the back- stack, he always ends up at the Start page.

6 PART I Building Blocks

  Adhering to Metro design principles is not a burden: it actually frees you to concentrate on making your application the best it can be, because you don’t have to spend time on inventing a new design paradigm. Instead, you can focus on making your application truly useful, compelling, and delightful, such that it offers a benefit to the user’s daily life. Note that Metro is also being applied within Micro- soft for Windows 8 and for Xbox. It will be the dominant design aesthetic for the next several years.

  Developer Guidelines

  If you want to build an ecosystem in which the inhabitants co-exist in harmony to their mutual ben- efit, then there must be rules and guidelines. Rules in Windows Phone development are enforced by the operating system, the application platform, and the platform API. There are certain programming techniques that might be appropriate in a desktop development environment, but you simply cannot do them in Windows Phone. You cannot currently build a Windows Phone application using native code. You also cannot use COM or RPC, or indeed, any kind of inter-process communication. Every application is strictly “sandboxed” and has no access to any other application. You cannot consume CPU cycles (and battery power) when you’re not running in the foreground. However careless you might be in handling exceptions, you cannot bring down the entire system. Clearly, these rules have been put in place to optimize the stability of the phone and the overall UX.

  Then there are guidelines. These are recommendations for how your application should look and behave, but they are not explicitly enforced by the phone itself. Rather, they are enforced by the mar- ketplace: if you build a bad application, users will quickly uninstall it and give it a bad review. You’re not forced to use Metro in your design, but if you don’t, you might end up with an application that clashes with the ecosystem. You’re not constrained in how much disk space you use up with applica- tion data, but if you use too much, the user will soon realize your application is making it difficult to run or install other applications. You can use more than 90 MB of memory at runtime, but sooner or later the system will run up against a memory cap, and then your application will crash. The Windows Phone marketplace publishes a comprehensive set of certification requirements, and the developer has a lot of support (especially in Windows Phone SDK 7.1) in preparing applications for publication.

  However, the marketplace performs only a small set of automated and manual tests; consequently, it is entirely possible to publish a bad application which then fails at runtime. Users typically have very low tolerance for things that crash, so this is very much a self-policing system.

  In the market today, there is a spectrum of application models from iOS at one end (for which there’s really only one logical device, and applications are severely constrained as to what they can and cannot do), to Android at the other (where there’s a very wide and heterogeneous range of devices, and applications have very wide latitude to do all kinds of things, which might or might not function well, or at all, on every device). Windows Phone sits somewhere in the middle, offering the best of both worlds: there is a small set of supported devices, with a tightly controlled hardware requirements specification to which they must minimally adhere, and an application platform that offers a wide range of features, yet enforces some reasonable constraints in the interests of maintain- ing a consistent UX as well as overall device health.

ChAPTER 1 Vision and Architecture

   7

  Windows Phone Architecture

  Apart from a few applications provided by Microsoft, the device manufacturer, or the service pro- vider, Windows Phone 7 applications are developed in managed code, using either the Windows Phone version of the Microsoft Silverlight runtime or the Windows Phone version of the XNA runtime (or some combination). These are slightly modified, phone-specific versions of the standard Silver- light/XNA libraries. The phone application platform includes a set of standard controls and wrap- per classes to the phone services. A high-level view of the architecture is shown in Figure 1-3. The underlying operating system is Windows CE, with the Microsoft .NET Compact Framework and the Silverlight or XNA runtime layered on top.

  host Process Silverlight/XNA Application Managed runtime Phone Controls & Services

NetCF Silverlight/XNA runtime

Native Application runtime

  Managed execution Platform Interop runtime host Manager Proxy Native Application Platform Install/Update Window Manager execution Manager Manager DB Operating System Kernel Security Networking Storage Direct3D FIguRE 1-3 The Windows Phone application architecture has managed and native layers.

  The application model exposed to marketplace developers is very robust. An important principle is that the core services of the phone must continue to function at all times, regardless of what custom applications might be installed or running.

8 PART I Building Blocks

   9 From a UX perspective, this means that the user can rely on the following behavior: ■ The user is always able to get back to the Start menu, lock the phone, and turn the phone off.

  Feature silverlight xnA Primary target app N-tier business applications, tools. Games.

  The game always uses the full screen.

  Multichannel audio, including recording. Screen Standard Silverlight visual tree model, with a hierarchy of controls.

  Audio Focused on simple media playback (music and video); can use XNA sounds and recording.

  Immediate mode graphics (application code causes direct rendering of graphics), with full 3D support.

  Gaming loop, display-focused. Graphics Retained mode graphics (application code up- dates an in-memory model of the graphics, which the OS renders later on), with 2D and limited 3D (PlaneProjection). Cannot use XNA graphics in 7.

  Models, meshes, sprites, textures. Execution model Event-driven (via UI controls or external events), and application-focused.

  Typical artifacts Built-in support for controls, data binding, web services, and text.

  Visual Studio–code-driven. Also similar to Xbox console and Windows games development.

  XAML-driven, using Microsoft Expression and Microsoft Visual Studio, very similar to Windows Presentation Foundation (WPF) and desktop Silverlight development.

  Developer experience

  TABlE 1-2 Comparison Between Silverlight and XNA Applications

  She is always able to make and receive calls, navigate to hubs, and so on. The phone protects itself mainly by its application isolation/security model.

  You can build applications for Windows Phone by using either Silverlight or XNA or a constrained combination of both, as described in Table 1.2.

  Comparison of Silverlight and XNA

  The application platform manages application install/uninstall in a very controlled way, so it always knows what applications are installed and displays the list to the user. It’s also not pos- sible to have ”hidden” or ”uninstallable” applications on the phone—even applications from the carrier or mobile operator are visible to the user and can always be uninstalled. While you can deploy applications to a developer-unlocked phone directly, you cannot deploy to a retail phone without publishing to the marketplace.

  ■