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

windows phone 7 programming for android and ios developers

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 (25.51 MB, 361 trang )

www.it-ebooks.info
ffirs.indd iiffirs.indd ii 7/13/11 7:20:43 AM7/13/11 7:20:43 AM
www.it-ebooks.info
WINDOWS
®
PHONE 7 PROGRAMMING FOR
ANDROID™ AND iOS DEVELOPERS
FOREWORD . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxv
INTRODUCTION . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .xxvii
CHAPTER 1 What’s New in Windows Phone 7 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .1
CHAPTER 2 The Development Environment . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
CHAPTER 3 Fundamentals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39
CHAPTER 4 User Interfaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63
CHAPTER 5 Application Data Storage . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 95
CHAPTER 6 Web Services and Push Notifi cations . . . . . . . . . . . . . . . . . . . . . . . . . . . .113
CHAPTER 7 Leveraging Location and Maps . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 155
CHAPTER 8 Graphics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 183
CHAPTER 9 Multimedia . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .205
CHAPTER 10 Utilizing System Hardware . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .235
CHAPTER 11 What You Need to Do about Security . . . . . . . . . . . . . . . . . . . . . . . . . . . 257
APPENDIX A An Introduction to Smartphone Chipset . . . . . . . . . . . . . . . . . . . . . . . . . 277
APPENDIX B An Introduction to Microsoft Expression
Blend for Windows Phone . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 281
APPENDIX C Sample Applications Guide . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 293
INDEX . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 307
ffirs.indd iffirs.indd i 7/13/11 7:20:43 AM7/13/11 7:20:43 AM
www.it-ebooks.info
ffirs.indd iiffirs.indd ii 7/13/11 7:20:43 AM7/13/11 7:20:43 AM
www.it-ebooks.info
Praise for
Windows Phone 7 Programming for Android


and iOS Developers
“Among quite a few Windows Phone 7 programming books on the market, this book is one of
the best for iOS and Android developers who look into creating applications on the new Windows
Phone 7 platform. It is also an excellent desktop reference for mobile computing courses in
schools, as it provides architectural overview of many popular mobile technologies on the three
major mobile platforms, as well as sample code that will help students understand the technologies
and develop programming skills for Windows Phone quickly.
—Dr. Lionel M. Ni
Chair Professor and Dean of HKUST
Fok Ying Tung Graduate School
Hong Kong University of Science and Technology
“Mobile is a rapidly growing space that is yet to reach its full potential and reach worldwide.
The combination of mobility, form factor, price, social media, and a global market is
enabling scenarios that previously were either hard to imagine or to develop. Developers,
both professional and otherwise, are fl ocking to mobile platforms to create applications and
games. An approachable, effective, integrated application platform is a goal that every software
vendor should be aiming for. The Windows Phone 7 application platform enables developers to
create engaging consumer experiences running on a Windows Phone. It is built upon existing
Microsoft tools and technologies such as Visual Studio, Expression Blend, Silverlight, and the
XNA framework. Developers already familiar with those technologies will be able to create new
applications for the Windows Phone quickly and easily.
“This book helps you to understand the Windows Phone application framework. It focuses
on Windows Phone design guidelines, provides guidance architecture, and solidifi es it with
hands-on, real-world development scenarios. It is comprehensive, highly readable, and replete
with useful real-world examples. I highly recommend this book for smartphone developers, IT
professionals, and engineering managers. Whether you are new, or one who has experience on
the iOS and Android platforms, this book serves as the ideal reference for development on the
Windows Phone.”
—Saty Bahadur
Principal Development Manager

Windows Phone Engineering
Microsoft Corporation
ffirs.indd iiiffirs.indd iii 7/13/11 7:20:43 AM7/13/11 7:20:43 AM
www.it-ebooks.info
“With Microsoft back in the game with Windows Phone, and with a growing number of
Windows Phone applications in end-user hands, more and more smartphone developers are
looking for books to learn how development on Windows Phone works.
“This book provides much-needed architecture guidance, theory, and hands-on practice
for real-world cases aimed at developers. It is comprehensive, highly readable, and replete
with useful examples. This book is exceedingly useful for mobile developers, mobile users, IT
engineers, and managers.”
—Quincy Milton
Principal Test Manager
Windows Phone Customer Experience Engineering
Microsoft Corporation
“This timely book will be invaluable to the many individuals and organizations that wish
to extend existing development skills in iOS and/or Android onto the Windows Phone 7
platform. The book’s brilliant approach of focusing on the differences between Windows
Phone 7 and the mobile platforms the reader already knows makes for remarkably quick and
effi cient learning.”
—Ib Green
Head of Capacity
Teleca USA, Inc.
ffirs.indd ivffirs.indd iv 7/13/11 7:20:44 AM7/13/11 7:20:44 AM
www.it-ebooks.info
Windows
®
Phone 7 Programming for
Android™ and iOS Developers
ffirs.indd vffirs.indd v 7/13/11 7:20:44 AM7/13/11 7:20:44 AM

www.it-ebooks.info
ffirs.indd viffirs.indd vi 7/13/11 7:20:44 AM7/13/11 7:20:44 AM
www.it-ebooks.info
Windows
®
Phone 7 Programming for
Android™ and iOS Developers
Zhinan Zhou
Robert Zhu
Pei Zheng
Baijian Yang
John Wiley & Sons, Inc.
ffirs.indd viiffirs.indd vii 7/13/11 7:20:44 AM7/13/11 7:20:44 AM
www.it-ebooks.info
Windows® Phone 7 Programming for Android™ and iOS Developers
Published by
John Wiley & Sons, Inc.
10475 Crosspoint Boulevard
Indianapolis, IN 46256
www.wiley.com
Copyright © 2011 by John Wiley & Sons, Inc., Indianapolis, Indiana
Published simultaneously in Canada
ISBN: 978-1-118-02197-2
ISBN: 978-1-118-15420-5 (ebk)
ISBN: 978-1-118-15422-9 (ebk)
ISBN: 978-1-118-15421-2 (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 specifi cally disclaim all warranties, including
without limitation warranties of fi tness 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: 2011931747
Trademarks: Wiley, the Wiley logo, Wrox, the Wrox logo, Wrox 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. Android is a trademark of Google, Inc. All other trademarks are the property of their respective owners.
John Wiley & Sons, Inc., is not associated with any product or vendor mentioned in this book.
ffirs.indd viiiffirs.indd viii 7/13/11 7:20:45 AM7/13/11 7:20:45 AM
www.it-ebooks.info
To my son, Vincent, who is the fi rst reader of
this book and loves smartphones much more than me.
To my wife, Xu, for her support and tolerance.

—Zhinan Zhou
To my wife, Jane, and my daughters, Jacqueline and
Angie, for their great understanding
and endless support.
—Robert Zhu
To my wife, Ning Liu, for her encouragement
and support.
—Pei Zheng
In memory of my parents.
—Baijian Yang
ffirs.indd ixffirs.indd ix 7/13/11 7:20:46 AM7/13/11 7:20:46 AM
www.it-ebooks.info
ffirs.indd xffirs.indd x 7/13/11 7:20:46 AM7/13/11 7:20:46 AM
www.it-ebooks.info
ABOUT THE AUTHORS
ZHINAN ZHOU is a senior software engineer with 10 years of professional experience
in the R&D of mobile applications and wireless communications. He is currently
with Samsung Telecommunications America, responsible for creating cutting-
edge technologies for the mobile-device space and for supporting other Samsung
development groups bringing new functionality to market. Zhou has a rich hands-on
experience in mobile-platform development on Android, Windows Phone, and J2ME.
Zhou is also an author of numerous cited publications on IEEE and ACM journals and conferences.
He received a Ph.D. in computer science from Michigan State University in 2006.
ROBERT ZHU is a principal development lead with Microsoft, developing Windows
Phone software products, providing hands-on design in computer engineering such
as kernel, device driver, and board support packages, and driving the technical
partnership with mobile carriers and OEM partners. Zhu also gave training
classes to OEMs on driver development, and Windows Mobile OS development.
Before working for Microsoft, he was with Digital Equipment Corporation (DEC),
U.S.A., as senior software engineer on the 64-bit DEC Alpha platform for workstation server

optimization and performance tuning for Windows, and was also a software lead with Motorola
Wireless Division, Canada. He obtained a master of computer science degree at the University of
Washington; a master of computing and electrical engineering degree from Simon Fraser University,
Canada; and a bachelor of engineering degree from Tsinghua University. He was in a Ph. D.
program with the SFU School of Engineering Science, Canada.
PEI ZHENG is a senior software architect with 10 years of experience in the mobile
wireless industry. He is currently with Sony Ericsson, responsible for overall device
platform software architecture and key software differentiations on Android and
Windows Phone. Before that, he was with Microsoft and Lucent Technologies.
Zheng is the author of two books in the mobile computing area, Smart Phone and
Next Generation Mobile Computing from Morgan Kaufmann and Professional
Smartphone Programming from Wiley/Wrox, as well as numerous cited publications in IEEE
journals and conferences. Zheng received a Ph.D. in computer science from Michigan State
University in 2003.
BAIJIAN YANG is currently an associate professor in the Department of Technology,
Ball State University. He has extensive industry and academic experience in mobile
computing, distributed computing, and information security. His current industry
certifi cations include MCSE, CISSP, and Six Sigma Black Belt. Yang is also a
contributing author of Professional Smartphone Programming from Wiley/Wrox and
numerous refereed publications. Yang received his Ph.D. in computer science from
Michigan State University in 2002.
ffirs.indd xiffirs.indd xi 7/13/11 7:20:46 AM7/13/11 7:20:46 AM
www.it-ebooks.info
ffirs.indd xiiffirs.indd xii 7/13/11 7:20:48 AM7/13/11 7:20:48 AM
www.it-ebooks.info
ABOUT THE TECHNICAL EDITOR
JOHN MUELLER is a freelance author and technical editor. He has writing in his blood, having
produced 87 books and over 300 articles to date. His technical editing skills have helped more than
60 authors refi ne the content of their manuscripts. Mueller has provided technical editing services
to both Data Based Advisor and Coast Compute magazines. He’s also contributed articles to such

magazines as Software Quality Connection, DevSource, InformIT, SQL Server Professional, Visual
C++ Developer, Hard Core Visual Basic, asp.netPro, Software Test and Performance, and Visual
Basic Developer.
ffirs.indd xiiiffirs.indd xiii 7/13/11 7:20:48 AM7/13/11 7:20:48 AM
www.it-ebooks.info
ffirs.indd xivffirs.indd xiv 7/13/11 7:20:48 AM7/13/11 7:20:48 AM
www.it-ebooks.info
CREDITS
ACQUISITIONS EDITOR
Paul Reese
PROJECT EDITOR
William Bridges
TECHNICAL EDITOR
John Mueller
PRODUCTION EDITOR
Daniel Scribner
EDITORIAL MANAGER
Mary Beth Wakefi eld
FREELANCER EDITORIAL MANAGER
Rosemarie Graham
ASSOCIATE DIRECTOR OF MARKETING
David Mayhew
BUSINESS MANAGER
Amy Knies
PRODUCTION MANAGER
Tim Tate
VICE PRESIDENT AND EXECUTIVE GROUP
PUBLISHER
Richard Swadley
VICE PRESIDENT AND EXECUTIVE PUBLISHER

Neil Edde
ASSOCIATE PUBLISHER
Jim Minatel
PROJECT COORDINATOR, COVER
Katie Crocker
PROOFREADER
James Saturnio, Word One
INDEXER
Robert Swanson
COVER DESIGNER
LeAndra Young
COVER IMAGE
© iStock / Andrew Rich
ffirs.indd xvffirs.indd xv 7/13/11 7:20:48 AM7/13/11 7:20:48 AM
www.it-ebooks.info
ffirs.indd xviffirs.indd xvi 7/13/11 7:20:48 AM7/13/11 7:20:48 AM
www.it-ebooks.info
ACKNOWLEDGMENTS
THE AUTHORS OF THE BOOK WOULD LIKE TO THANK Eric Hautala, Saty Bahadur, and Quincy
Milton from the Windows Phone leadership team for their support of this book project. Our
special thanks go to Dr. Lionel M. Ni, chair professor of the Hong Kong University of Science and
Technology, for his inspiration and long-term support of our work in the mobile computing space.
We also thank Ib Green, head of capacity for Teleca USA, for supporting this book project and
reviewing the content.
We also thank Paul Reese from Wrox/Wiley for guiding us through the book publication process.
Thanks also go to John Mueller and Bill Bridges, who have provided tons of valuable comments and
suggestions, including insightful technical feedback as well as writing guidance. Our appreciation
also extends to other members of the Wrox/Wiley team for their hard-working and consistent
contributions to the book project.
We also want to thank our coworkers and friends in the mobile space, who have reviewed the

book and provided valuable feedback. Ying Chin has given a lot of inspiring technical input,
offered mobile programming expertise, and reviewed multiple chapters of the book, and many
people reviewed some chapters of the book or provided insightful comment on mobile software
development to the authors. These include Ciaran Rochford, Kevin Lee, Madelyn Bryant McIntire,
Paul Wu, Mark Leiter, Meishan Li, Anil Hashia, Brad Belanger, Alex Su, Hailin Jiang, Ming Wang,
John Zhu, Moataz Mohamed, John Basacchi, Neil McDonald, Aimin Pan, Richard Zhang, Yutao
Xi, Haifeng Zheng, Joy Fan, Junqun Dai, Shigenori Tanaka, Yana Wang, Lingzhi Cao, Bolian Yin,
Landong Wei, Yi Yang, Yong Chen, Houston Wong, Kevin Wu, Andrew Lin, Richen Wei, Peng
Huang, Chen Wang, Emeel Noohi, Scott Leatham, Brian Stern, Youngji Kim, Jason Whitehorn,
and Kevin Son.
We would like to thank our family members for their encouragement and support of the book
project. In particular, Baijian Yang would like to thank his wife, Chen, and daughter, Ashley, for
their love, support, and tolerance. He would also like to thank his co-authors for their inspiration
and encouragement.
ffirs.indd xviiffirs.indd xvii 7/13/11 7:20:49 AM7/13/11 7:20:49 AM
www.it-ebooks.info
ffirs.indd xviiiffirs.indd xviii 7/13/11 7:20:49 AM7/13/11 7:20:49 AM
www.it-ebooks.info
CONTENTS
FOREWORD xxv
INTRODUCTION xxvii
CHAPTER 1: WHAT’S NEW IN WINDOWS PHONE 7 1
An Overview of Windows Phone 7 2
A Brief History 2
The Big Ideas 3
System Architecture 4
Application Framework 6
Windows Phone Marketplace 7
Limitations and Road Map 7
Side-By-Side Comparisons with Android and iPhone 8

Operating System 8
Application Framework 10
Application Store Process 12
Summary 13
CHAPTER 2: THE DEVELOPMENT ENVIRONMENT 15
Overview 15
Using Windows Phone 7 Developer Tools 17
Creating WP7 Applications with Visual Studio 17
Testing WP7 Applications in the Windows Phone Emulator 21
Testing WP7 Applications on the Actual Windows Phone Device 26
Publishing Windows Phone 7 Applications to the Marketplace 28
Preparation 28
Submission 30
Post-Submission Actions 35
Comparisons with Android and iPhone 35
Summary 36
CHAPTER 3: FUNDAMENTALS 39
Basic Application Project Structure 40
Application Project Structure for Windows Phone 7 40
Comparing Application Project Structure for Android and iOS 42
ftoc.indd xixftoc.indd xix 7/13/11 7:24:11 AM7/13/11 7:24:11 AM
www.it-ebooks.info
xx
CONTENTS
Application Execution Model and Life Cycles 43
Application Execution Model and Life Cycles in Windows Phone 7 44
Comparing Application Model and Life Cycles in Android and iOS 47
Preserve/Restore Application and Page Transient States for
Windows Phone 7 50
Using Launchers and Choosers 59

Work-Around Solutions in Windows Phone 7 60
Multitasking 60
Background Service 61
Data Sharing 61
System Event Hooks 61
Summary 62
CHAPTER 4: USER INTERFACES 63
UI Design Principles 64
UI Design Resources 64
Platform Characteristics 64
Building the WP7 UI 66
Defi ning WP7 UI with XAML 66
Defi ning WP7 UI Programmatically 67
Pages and Navigation Among Pages 68
Using Controls 74
Data Binding and MVVM 81
Handling UI Events 90
Other UI Considerations 91
UI Customization 92
Summary 94
CHAPTER 5: APPLICATION DATA STORAGE 95
Application Storage on Mobile Devices 95
Local and Cloud 96
Local Files and Databases 97
Using Isolated Storage 98
Where Is Isolated Storage? 98
Saving Data to the Cloud 105
Building a Cloud Data Service 105
Using Cloud Data Services 108
Data Storage Design Considerations 110

Summary 112
ftoc.indd xxftoc.indd xx 7/13/11 7:24:12 AM7/13/11 7:24:12 AM
www.it-ebooks.info
xxi
CONTENTS
CHAPTER 6: WEB SERVICES AND PUSH NOTIFICATIONS 113
Using Web Services 113
A Primer of Web Services 114
Consuming Web Services on WP7 117
WP7 Push Notifi cations 131
Why Push Notifi cations? 131
Push Notifi cation Architecture 131
Push Notifi cations on WP7 136
Mobile Advertising 146
Mobile Advertising Basics 147
Mobile Advertising Providers 148
Adding Ads to WP7 Apps 150
Summary 152
CHAPTER 7: LEVERAGING LOCATION AND MAPS 155
Location Frameworks Roundup 156
Getting Current Location 159
Geographical Data 159
Civilian Data 166
Using Maps 173
Using the Bing Map Control 173
Combining the Location Service and Bing Map 176
Summary 180
CHAPTER 8: GRAPHICS 183
Graphics Fundamentals 184
Basic 2D and 3D Graphics Glossary 184

Drawing Graphics on Smartphones 187
Drawing Graphics with XNA 189
When Do You Use XNA? 189
Game Loop 190
Textures and Sprites 194
Animation 198
3D Graphics 200
Summary 204
CHAPTER 9: MULTIMEDIA 205
Multimedia Overview 205
Multimedia Architectural Overview 206
WP7 Multimedia 206
ftoc.indd xxiftoc.indd xxi 7/13/11 7:24:13 AM7/13/11 7:24:13 AM
www.it-ebooks.info
xxii
CONTENTS
iOS Multimedia 208
Android Multimedia 209
Supported Media Codecs 212
Playing Audio on WP7 215
Playing Sounds Using MediaElement 215
Playing Sounds Using SoundE ect 222
Sound, Picture, and Graphics Integration 223
Playing Video on WP7 229
Playing Video Using MediaPlayerLauncher 229
Playing Video Using MediaElement 230
Reusable Media Player Controls 232
Summary 232
CHAPTER 10: UTILIZING SYSTEM HARDWARE 235
An Overview of Smartphone Hardware 235

ARM Processor Architecture 236
Smartphone Hardware Components 237
Accessing the Microphone 238
Accessing the Microphone on iOS 239
Accessing the Microphone on Android 239
Accessing the Microphone on WP7 240
Accessing the Camera 242
Accessing the Camera on iOS 242
Accessing the Camera on Android 243
Accessing the Camera on WP7 244
Using Sensors 245
Accessing the Sensors on iOS 246
Accessing the Sensors on Android 246
Accessing Sensors on WP7 248
Utilizing FM Radio and Bluetooth 254
Utilizing the FM Radio On WP7 254
Utilizing Bluetooth 255
Summary 255
CHAPTER 11: WHAT YOU NEED TO DO ABOUT SECURITY 257
Understanding Mobile Application Security Models 258
Windows Phone 7 Security Overview 258
iOS Security Overview 260
Android Security Overview 261
Security Model Comparisons 262
Using Windows Phone 7 Security APIs 262
ftoc.indd xxiiftoc.indd xxii 7/13/11 7:24:13 AM7/13/11 7:24:13 AM
www.it-ebooks.info
xxiii
CONTENTS
.NET Security Namespace 263

Protecting Data Confi dentiality with Encryption 263
Ensuring Data Integrity with Hashing 270
Security Best Practices 274
Summary 275
APPENDIX A: AN INTRODUCTION TO SMARTPHONE CHIPSET 277
APPENDIX B: AN INTRODUCTION TO MICROSOFT EXPRESSION
BLEND FOR WINDOWS PHONE 281
Microsoft Expression Blend IDE 282
Creating Animation with Expression Blend 284
Customizing UI with Expression Blend 288
Summary 291
APPENDIX C: SAMPLE APPLICATIONS GUIDE 293
Chapter 2 295
HelloWP7 295
Chapter 3 295
WP7Lifecycles 295
Chapter 4 295
WP7Controls 295
WP7Navigation 296
Chapter 5 296
AppDataSample 296
SampleCloudService 297
Chapter 6 297
SocialDemo 297
Push Notifi cation 297
AdControlDemo 298
Chapter 7 298
CurrentLocationTxt 298
SimpleMap 299
Street2Geo 299

TrackMe 299
Chapter 8 300
Cube3D 300
Rolling2D 301
ToyAnimation 301
ftoc.indd xxiiiftoc.indd xxiii 7/13/11 7:24:13 AM7/13/11 7:24:13 AM
www.it-ebooks.info

×