Tải bản đầy đủ (.pdf) (626 trang)

Professional windows phone 7 application development

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (44.12 MB, 626 trang )

www.it-ebooks.info


www.it-ebooks.info
ffirs.indd ii

10/5/10 4:18:49 PM


PROFESSIONAL
WINDOWS® PHONE 7 APPLICATION
DEVELOPMENT
INTRODUCTION . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxv
CHAPTER 1

Taking the Metro with Windows Phone . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1

CHAPTER 2

Free Transport with Visual Studio 2010 and Expression Blend 4 . . . . . 15

CHAPTER 3

Designing Layouts Using Red Threads . . . . . . . . . . . . . . . . . . . . . . . . . . . 27

CHAPTER 4

Adding Motion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63

CHAPTER 5


Orientation and Overlays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 95

CHAPTER 6

Navigation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 123

CHAPTER 7

Application Tiles and Notification . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 151

CHAPTER 8

Tasks. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 173

CHAPTER 9

Touch Input . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 195

CHAPTER 10

Shake, Rattle, and Vibrate . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 221

CHAPTER 11

Who Said That?. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 241

CHAPTER 12

Where Am I? Finding Your Way . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 279


CHAPTER 13

Connectivity and the Web . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 309

CHAPTER 14

Consuming the Cloud . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 349

CHAPTER 15

Data Visualization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 385

CHAPTER 16

Storing and Synchronizing Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .417

CHAPTER 17

Frameworks. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 459

CHAPTER 18

Security . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 491

CHAPTER 19

Gaming with XNA . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 515

CHAPTER 20


Where to Next? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 541

INDEX . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 571

www.it-ebooks.info
ffirs.indd i

10/5/10 4:18:48 PM


www.it-ebooks.info
ffirs.indd ii

10/5/10 4:18:49 PM


PROFESSIONAL

Windows® Phone 7 Application
Development

www.it-ebooks.info
ffirs.indd iii

10/5/10 4:18:49 PM


www.it-ebooks.info
ffirs.indd iv


10/5/10 4:18:49 PM


PROFESSIONAL

Windows® Phone 7 Application
Development
BUILDING APPLICATIONS AND GAMES USING
VISUAL STUDIO, SILVERLIGHT®, AND XNA®

Nick Randolph
Christopher Fairbairn

www.it-ebooks.info
ffirs.indd v

10/5/10 4:18:50 PM


Professional Windows® Phone 7 Application Development: Building Applications and Games
Using Visual Studio, Silverlight®, and XNA®
Published by
Wiley Publishing, Inc.
10475 Crosspoint Boulevard
Indianapolis, IN 46256

www.wiley.com
Copyright © 2011 by Wiley Publishing, Inc., Indianapolis, Indiana
Published simultaneously in Canada
ISBN: 978-0-470-89166-7

ISBN: 978-1-118-01534-6 (ebk)
ISBN: 978-1-118-01393-9 (ebk)
ISBN: 978-1-118-01394-6 (ebk)
Manufactured in the United States of America
10 9 8 7 6 5 4 3 2 1
No part of this publication may be reproduced, stored in a retrieval system or transmitted in any form or by any means,
electronic, mechanical, photocopying, recording, scanning or otherwise, except as permitted under Sections 107 or 108
of the 1976 United States Copyright Act, without either the prior written permission of the Publisher, or authorization
through payment of the appropriate per-copy fee to the Copyright Clearance Center, 222 Rosewood Drive, Danvers,
MA 01923, (978) 750-8400, fax (978) 646-8600. Requests to the Publisher for permission should be addressed to
the Permissions Department, John Wiley & Sons, Inc., 111 River Street, Hoboken, NJ 07030, (201) 748-6011,
fax (201) 748-6008, or online at />Limit of Liability/Disclaimer of Warranty: The publisher and the author make no representations or warranties with
respect to the accuracy or completeness of the contents of this work and specifically disclaim all warranties, including
without limitation warranties of fitness for a particular purpose. No warranty may be created or extended by sales or
promotional materials. The advice and strategies contained herein may not be suitable for every situation. This work
is sold with the understanding that the publisher is not engaged in rendering legal, accounting, or other professional
services. If professional assistance is required, the services of a competent professional person should be sought. Neither
the publisher nor the author shall be liable for damages arising herefrom. The fact that an organization or Web site is
referred to in this work as a citation and/or a potential source of further information does not mean that the author or the
publisher endorses the information the organization or Web site may provide or recommendations it may make. Further,
readers should be aware that Internet Web sites listed in this work may have changed or disappeared between when this
work was written and when it is read.
For general information on our other products and services please contact our Customer Care Department within the
United States at (877) 762-2974, outside the United States at (317) 572-3993 or fax (317) 572-4002.
Wiley also publishes its books in a variety of electronic formats. Some content that appears in print may not be available
in electronic books.
Library of Congress Control Number: 2010932454
Trademarks: Wiley, the Wiley logo, Wrox, the Wrox logo, Programmer to Programmer, and related trade dress are
trademarks or registered trademarks of John Wiley & Sons, Inc. and/or its affi liates, in the United States and other
countries, and may not be used without written permission. Windows is a registered trademark of Microsoft Corporation

in the United States and/or other countries. All other trademarks are the property of their respective owners. Wiley
Publishing, Inc., is not associated with any product or vendor mentioned in this book.

www.it-ebooks.info
ffirs.indd vi

10/5/10 4:18:50 PM


To the love of my life, Cynthia.
— Nick Randolph

To my princess forever after, Michele.
— Christopher Fairbairn

www.it-ebooks.info
ffirs.indd vii

10/5/10 4:18:50 PM


www.it-ebooks.info
ffirs.indd viii

10/5/10 4:18:51 PM


ABOUT THE AUTHORS

NICK RANDOLPH currently runs Built to Roam, which builds rich mobile applications for Windows


Phone. Previously, Nick was co -founder and Development Manager at nsquared Solutions, where
he led a team of developers to build inspirational software using next-wave technologies. Prior to
nsquared, Nick was the lead developer at Intilecta Corporation, where he was integrally involved in
designing and building their application framework.
After graduating with a combined Engineering (IT)/Commerce degree, Nick went on to be
nominated as a Microsoft MVP in recognition of his work with the .NET developer community,
and his interest and knowledge on developing for the Windows Mobile platform. He is still
an active contributor in the device application development space via his blog at http://
nicksnettravels.builttoroam.com.
Nick has been invited to present at a variety of events including TechEd Australia and NZ, MEDC,
and Code camp. He has authored three other books covering Visual Studio, the latest being
Professional Visual Studio 2010, and has helped judge the world-wide fi nals for the Imagine Cup for
five years.
CHRISTOPHER FAIRBAIRN currently works as a lead developer for ARANZ Medical Limited devel-

oping mobile applications (and hardware) for wound care applications within the medical space.
Prior to this, Christopher was Technical Architect for Blackbay working on the technical framework
behind their flagship mobile freight logistics product, Delivery Connect. He has developed for a
wide range of mobile platforms including iPhone, Windows Mobile, and Palm OS.
Christopher is currently a Microsoft MVP in recognition of his work within the Windows Mobile
developer community. He has also presented at a variety of events throughout New Zealand, mostly
organized through the thriving New Zealand MS Communities organization (formally NZ .NET User
Groups Society). Whenever possible, Christopher maintains a blog at www.christec.co.nz/blog/.

www.it-ebooks.info
ffirs.indd ix

10/5/10 4:18:51 PM



www.it-ebooks.info
ffirs.indd x

10/5/10 4:18:51 PM


ABOUT THE TECHNICAL EDITOR

SCOTT SPRADLIN has been programming professionally for 30 years and developing solutions

in .NET since the early beta releases. He’s always been fascinated by the ability to take code
portable in handheld devices. As a Microsoft MVP, he is an advocate for the developer
community at large and is on the board of INETA North America. You can fi nd Scott on
Twitter at www.twitter.com/scotts or his blog />
www.it-ebooks.info
ffirs.indd xi

10/5/10 4:18:51 PM


www.it-ebooks.info
ffirs.indd xii

10/5/10 4:18:51 PM


CREDITS

ACQUISITIONS EDITOR


VICE PRESIDENT AND
EXECUTIVE GROUP PUBLISHER

Paul Reese

Richard Swadley
SENIOR PROJECT EDITOR

Adaobi Obi Tulton

VICE PRESIDENT AND
EXECUTIVE PUBLISHER

TECHNICAL EDITOR

Barry Pruett

Scott Spradlin
ASSOCIATE PUBLISHER
SENIOR PRODUCTION EDITOR

Jim Minatel

Debra Banninger
PROJECT COORDINATOR, COVER

Lynsey Stanford

COPY EDITOR


Cate Caffrey
PROOFREADER

Nancy Carrasco

EDITORIAL DIRECTOR

Robyn B. Siesky
INDEXER

Robert Swanson

EDITORIAL MANAGER

Mary Beth Wakefield
COVER DESIGNER
FREELANCER EDITORIAL MANAGER

Michael E. Trent

Rosemarie Graham
COVER IMAGE
ASSOCIATE DIRECTOR OF MARKETING

© Thorsten Rust

David Mayhew
PRODUCTION MANAGER


Tim Tate

www.it-ebooks.info
ffirs.indd xiii

10/5/10 4:18:51 PM


www.it-ebooks.info
ffirs.indd xiv

10/5/10 4:18:52 PM


ACKNOWLEDGMENTS

I ’ VE ALWAYS BEEN A MOBILE ENTHUSIAST, so when the opportunity came about to write one of

the fi rst books to be published on Windows Phone, I jumped at it, not quite envisaging how much
of a challenge it would be. In the months since the early versions of the developer tools were made
available, a great deal changed, often causing whole sections of the book to be completely rewritten. I can’t say how grateful I am to my beautiful Cynthia. Without your unquestioning support and
understanding, I would have never been able to complete this book.
The team at Wrox has been amazing to work with. Their attention to detail and ability to address
even the most challenging aspects of this book have been a lifesaver — especially Paul Reese, Adaobi
Obi Tulton, and the editors who worked on producing this book.
I also extend a big thank you to the Windows Phone team at Microsoft — especially Peter Torr and
Anand Iyer, whose willingness to answer my questions saved hours of experimentation, and Dave
Glover (DPE Australia), who provided an invaluable sounding board for issues and ideas I came
across while writing.
Lastly, I thank my co -author Christopher for sharing his knowledge of multiple mobile platforms

to make sure we addressed issues that were relevant to developers wanting to build applications for
Windows Phone.

—Nick Randolph

I WOULD LIKE TO THANK MY CO-AUTHOR Nick for giving me the opportunity to collaborate on writing this book with him. His passion and in-depth knowledge of multiple modern technologies made
coming up to speed with Windows Phone 7 a breeze. Nick was also very accommodating when it
came to adapting to challenges that came up along the way, such as a magnitude 7.1 earthquake that
occurred within my hometown of Christchurch, New Zealand near the end of the process.

Closer to home, without the patience, reassurance and support of my partner, Michele, I would
never have made it to the end.
Lastly I would like to thank the team at Wrox who has been extremely easy to work with. Their
clear explanation of expectations and quality of feedback has been truly awe-inspiring. Thank you
to Paul Reese, Adaobi Obi Tulton, and the various editors who contributed to making this book
what it is today.

—Christopher Fairbairn

www.it-ebooks.info
ffirs.indd xv

10/5/10 4:18:52 PM


www.it-ebooks.info
ffirs.indd xvi

10/5/10 4:18:52 PM



CONTENTS

INTRODUCTION

xxv

CHAPTER 1: TAKING THE METRO WITH WINDOWS PHONE

Minimum Specifications

1

2

Chassis Design
Screen Resolution

3
5

Metro Design Language

5

Principles
User Experience

7
8


Start and Lock Screens
Hubs
Developer Landscape
Summary

9
10
12
13

CHAPTER 2: FREE TRANSPORT WITH VISUAL STUDIO 2010
AND EXPRESSION BLEND 4

Visual Studio 2010 Express for Windows Phone
Expression Blend 4
Windows Phone Emulator
Summary
CHAPTER 3: DESIGNING LAYOUTS USING RED THREADS

Red Threads
Controls

15

16
21
24
26
27


27
29

Standard Controls

34

Layout
Resources and Styles
Themes
Summary

50
54
59
61

CHAPTER 4: ADDING MOTION

63

Visual State Management
Behaviors
Animation

63
71
79


www.it-ebooks.info
TOC.indd xvii

10/5/10 11:01:33 AM


CONTENTS

Template Transitions
State Transitions

80
83

Panoramic and Pivot Controls
Summary
CHAPTER 5: ORIENTATION AND OVERLAYS

Device Orientation

88
94
95

95

Orientation Detection
Orientation Changes
Orientation Strategies


96
99
99

Soft Input Panel (SIP)
Application Bar

109
113

Icon Buttons
Menu Items
Opacity
StateChanged Event

114
118
119
120

System Tray
Summary

120
121

CHAPTER 6: NAVIGATION

123


Page Layout and Architecture
Navigation

123
126

Fragments and QueryString
UriMapping
Go Back
GoBack and CanGoBack
Animation
Wizards

131
132
133
135
136
139

Background Processing

140

Eligible for Termination
Scenarios
Saving State
Obscured

141

142
146
148

Summary

149

CHAPTER 7: APPLICATION TILES AND NOTIFICATION

Application Tile
Push Notifications

151

151
155

Priority
Tile Notifications

161
163

xviii

www.it-ebooks.info
TOC.indd xviii

10/5/10 11:01:34 AM



CONTENTS

Toast Notifications
Raw Notifications
Examples
Errors

164
165
166
169

Summary

171

CHAPTER 8: TASKS

173

Windows Phone Tasks

173

Where Did My Application Go?
Camera and Photos
Phone and SMS
E-Mail

Launchers

Extras
Summary

175
176
179
184
188

192
194

CHAPTER 9: TOUCH INPUT

195

User Experience

195

Guidelines

196

Touch Events

200


Single Touch
Double-Tap
Multi-Touch

201
203
214

Summary

220

CHAPTER 10: SHAKE, RATTLE, AND VIBRATE

Accelerometer

221

221

Working with the Emulator
Reactive Extensions for .NET

Vibration
Summary

226
238

239

240

CHAPTER 11: WHO SAID THAT?

Media Playback

241

241

MediaElement
SoundEffects with XNA
Microsoft Translator

241
257
267

Audio Recording

270

Playback
Saving

272
272

xix


www.it-ebooks.info
TOC.indd xix

10/5/10 11:01:35 AM


CONTENTS

Music and Video Hub
FM Tuner
Summary

274
276
278

CHAPTER 12: WHERE AM I? FINDING YOUR WAY

Geo-Location

279

279

GeoCoordinateWatcher
IGeoPositionWatcher

280
284


Bing Maps

295

Map Design
Map Credentials
Points of Interest and Lines
Events
Bing Maps Web Services

Summary

296
297
298
302
304

307

CHAPTER 13: CONNECTIVITY AND THE WEB

Connected Status

309

309

Network Availability
Service Reachability

Emulator Testing
Connectivity

310
310
312
315

WebBrowser Control
MultiScaleImage
Authentication

320
326
328

Windows Live ID

329

Summary

347

CHAPTER 14: CONSUMING THE CLOUD

HTTP Request

349


349

WebClient
HttpWebRequest
Credentials
Compression

350
355
359
360

WCF/ASMX Services

365

Service Configuration
Add Service Reference
Service Implementation and Execution
Custom Headers
Credentials

365
367
368
371
371

xx


www.it-ebooks.info
TOC.indd xx

10/5/10 11:01:35 AM


CONTENTS

WCF Data Services

372

OData with WCF Data Services
JSON

Summary

372
379

383

CHAPTER 15: DATA VISUALIZATION

Data Binding

385

385


DataContext
BindingMode
Value Converters

386
388
392

Designing with Data

394

Sample Data
Design-Time Data
MVVM Light Toolkit
Element and Resource Binding

Summary

394
402
411
413

415

CHAPTER 16: STORING AND SYNCHRONIZING DATA

Isolated Storage


417

417

ApplicationSettings
IsolatedStorageFileStream

418
421

Data-Caching

422

Object Cache
Persistent Storage
Synchronization

422
430
442

Summary

458

CHAPTER 17: FRAMEWORKS

459


Managed Extensibility Framework
Import and Export
ImportMany

459
460
463

Application Composition
Microsoft Silverlight Analytics Framework
Testing
Unit Testing
Emulator Automation

467
467
471
471
486

Summary

490

xxi

www.it-ebooks.info
TOC.indd xxi

10/5/10 11:01:35 AM



CONTENTS

CHAPTER 18: SECURITY

491

On the Device

491

Device Security
Device Management
Data Encryption

492
492
493

Over the Wire

496

Transport
Authentication

496
497


Summary

514

CHAPTER 19: GAMING WITH XNA

Getting Started

515

515

Game Loop
Game Life Cycle

521
522

Rendering

524

Content
Sprites
Movement
Text and Fonts

524
525
526

527

Input

530

Accelerometer
Touch
Keyboard

530
531
532

3D Rendering

532

3D Model
Color and Lighting
Primitives
Textures

533
535
535
538

Summary


539

CHAPTER 20: WHERE TO NEXT?

Device Debugging

541

541

Registering for Development
Debugging Applications
Deploying Applications

Third-Party Components

541
543
544

544

Silverlight Toolkit
Database

545
546

xxii


www.it-ebooks.info
TOC.indd xxii

10/5/10 11:01:35 AM


CONTENTS

Application Migration

547

User Interface
Services and Connectivity
Data
Device Capabilities
Background Processing

547
548
548
548
549

User Interface Performance

549

Performance Counters
Redraw Regions

Caching

549
551
551

External Systems

552

Proxy Service (Exchange)
Shared Key Signatures (Windows Azure)

Publishing

552
554

563

Application and Start Icons
Splash Screen
Capabilities
Trial Mode and Marketplace

564
565
565
567


Summary

569

INDEX

571

xxiii

www.it-ebooks.info
TOC.indd xxiii

10/5/10 11:01:36 AM


×