Description
PUBLISHED BY
Microsoft Press
A Division of Microsoft Corporation
One Microsoft Way
Redmond, Washington 98052-6399
Copyright © 2010 by Microsoft Corporation
All rights reserved. No part of the contents of this book may be reproduced or transmit
p. 2
published by microsoft press a division of microsoft corporation one microsoft way redmond washington 98052-6399 copyright © 2010 by microsoft corporation 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 2010939982 isbn 978-0-7356-4335-2 printed and bound in the united states of america microsoft press books are available through booksellers and distributors worldwide for further information about international editions contact your local microsoft corporation office or contact microsoft press international directly at fax 425 936-7329 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/trademark/emus.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 fictitious 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 developmental and project editor devon musgrave editorial production ashley schneider s4carlisle publishing services technical reviewer per blomqvist technical review services provided by content master a member of cm group ltd cover tom draper design body part no x17-12906 iii
[close]
p. 3
contents at a glance part i the basics 1 hello windows phone 7 2 2 getting oriented 29 3 an introduction to touch 47 4 bitmaps also known as textures 62 5 sensors and services 80 6 issues in application architecture 103 part ii silverlight 7 xaml power and limitations 138 8 elements and properties 165 9 the intricacies of layout 192 10 the app bar and controls 232 11 dependency properties 296 12 data bindings 338 13 vector graphics 393 14 raster graphics 461 15 animations 508 16 the two templates 578 17 items controls 641 18 pivot and panorama 712 iv
[close]
p. 4
part iii xna 19 principles of movement 750 20 textures and sprites 775 21 dynamic textures 799 22 from gestures to transforms 840 23 touch and play 900 24 tilt and play 952 v
[close]
p. 5
table of contents introduction xii part i the basics targeting windows phone 7 2 the hardware chassis 4 sensors and services 6 file new project 7 a first silverlight phone program 8 the standard silverlight files 10 color themes 17 points and pixels 18 the xap is a zip 20 an xna program for the phone 21 1 hello windows phone 7 2 2 getting oriented 29 silverlight and dynamic layout 29 orientation events 36 xna orientation 38 simple clocks very simple clocks 41 3 an introduction to touch 47 low-level touch handling in xna 47 the xna gesture interface 51 low-level touch events in silverlight 52 the manipulation events 56 routed events 59 some odd behavior 61 4 bitmaps also known as textures 62 xna texture drawing 63 the silverlight image element 65 images via the web 66 image and imagesource 70 loading local bitmaps from code 71 vi
[close]
p. 6
capturing from the camera 73 the phone s photo library 76 5 sensors and services 80 accelerometer 80 a simple bubble level 86 geographic location 91 using a map service 95 6 issues in application architecture 103 basic navigation 103 passing data to pages 110 sharing data among pages 112 retaining data across instances 117 the multitasking ideal 120 task switching on the phone 120 page state 122 isolated storage 126 xna tombstoning and settings 130 testing and experimentation 136 part ii silverlight a textblock in code 139 property inheritance 141 property-element syntax 143 colors and brushes 144 content and content properties 151 the resources collection 154 sharing brushes 156 x:key and x:name 159 an introduction to styles 160 style inheritance 161 themes 163 gradient accents 163 7 xaml power and limitations 138 8 elements and properties 165 basic shapes 165 vii
[close]
p. 7
transforms 167 animating at the speed of video 175 handling manipulation events 177 the border element 178 textblock properties and inlines 182 more on images 185 playing movies 188 modes of opacity 188 non-tiled tile brushes 190 9 the intricacies of layout 192 the single-cell grid 193 the stackpanel stack 195 text concatenation with stackpanel 199 nested panels 201 visibility and layout 202 two scrollviewer applications 205 the mechanism of layout 211 inside the panel 213 a single-cell grid clone 214 a custom vertical stackpanel 218 the retro canvas 220 canvas and zindex 226 the canvas and touch 226 the mighty grid 228 10 the app bar and controls 232 applicationbar icons 232 jot and application settings 240 jot and touch 245 jot and the applicationbar 247 elements and controls 252 rangebase and slider 254 the basic button 261 the concept of content 265 theme styles and precedence 270 the button hierarchy 271 toggling a stopwatch 274 buttons and styles 284 viii
[close]
p. 8
textbox and keyboard input 286 11 dependency properties 296 the problem illustrated 296 the dependency property difference 299 deriving from usercontrol 310 a new type of toggle 321 panels with properties 326 attached properties 332 12 data bindings 338 source and target 338 target and mode 341 binding converters 343 relative source 348 the this source 349 notification mechanisms 353 a simple binding server 354 setting the datacontext 360 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 395 overlapping and zindex 396 polylines and custom curves 398 caps joins and dashes 403 polygon and fill 411 the stretch property 413 dynamic polygons 414 the path element 417 geometries and transforms 423 grouping geometries 428 the versatile pathgeometry 429 the arcsegment 431 bézier curves 440 ix
[close]
p. 9
the path markup syntax 450 how this chapter was created 455 14 raster graphics 461 the bitmap class hierarchy 461 writeablebitmap and uielement 463 the pixel bits 470 vector graphics on a bitmap 474 images and tombstoning 480 saving to the picture library 489 becoming a photo extras application 497 15 animations 508 frame-based vs time-based 508 animation targets 512 click and spin 513 some variations 516 xaml-based animations 520 a cautionary tale 523 key frame animations 530 trigger on loaded 534 animating attached properties or not 543 splines and key frames 548 the bouncing ball problem 557 the easing functions 561 animating perspective transforms 567 animations and property precedence 573 16 the two templates 578 contentcontrol and datatemplate 578 examining the visual tree 583 controltemplate basics 589 the visual state manager 601 sharing and reusing styles and templates 610 custom controls in a library 614 variations on the slider 619 the ever-handy thumb 629 custom controls 634 x
[close]
p. 10
17 items controls 641 items controls 641 items controls and visual trees 642 customizing item displays 649 listbox selection 653 binding to itemssource 658 databases and business objects 663 fun with datatemplates 682 sorting 685 changing the panel 690 the datatemplate bar chart 692 a card file metaphor 699 18 pivot and panorama 712 compare and contrast 712 music by composer 725 the xna connection 728 the xna music classes medialibrary 731 displaying the albums 737 the xna music classes mediaplayer 742 part iii xna the naïve approach 750 a brief review of vectors 753 moving sprites with vectors 758 working with parametric equations 760 fiddling with the transfer function 763 scaling the text 764 two text rotation programs 768 19 principles of movement 750 20 textures and sprites 775 the draw variants 775 another hello program 777 driving around the block 782 movement along a polyline 786 the elliptical course 791 a generalized curve solution 794 xi
[close]
p. 11
21 dynamic textures 799 the render target 799 preserving render target contents 808 drawing lines 812 manipulating the pixel bits 819 the geometry of line drawing 823 modifying existing images 835 22 from gestures to transforms 840 gestures and properties 840 scale and rotate 844 matrix transforms 853 the pinch gesture 856 flick and inertia 864 the mandelbrot set 867 pan and zoom 878 game components 885 affine and non-affine transforms 889 23 touch and play 900 more game components 900 the phingerpaint canvas 905 a little tour through spinpaint 916 the spinpaint code 918 the actual drawing 923 phreecell and a deck of cards 928 the playing field 929 play and replay 938 24 tilt and play 952 3d vectors 952 a better bubble visualization 955 the graphical rendition 964 follow the rolling ball 972 navigating a maze 984 about the authors 996 xii
[close]
p. 12
introduction this book is a gift from the windows phone 7 team at microsoft to the programming community and i am proud to have been a part of it within the pages that follow i show you the basics of writing applications for windows phone 7 using the c programming language with the silverlight and xna 2d frameworks yes programming windows phone 7 is truly a free download but for those readers who still love paper as i certainly do this book will also be available for sale divided into two fullyindexed print editions microsoft silverlight programming for windows phone 7 and microsoft xna framework programming for windows phone 7 with the money you ve saved downloading this book please buy other books despite the plethora of information available online books are still the best way to learn about programming within a coherent and cohesive tutorial narrative every book sale brings a tear of joy to an author s eye so please help make them weep overflowing rivers in particular you might want to buy other books to supplement the material in this book for example i barely mention web services in this book and that s a serious deficiency because web services are likely to become increasingly important in windows phone 7 applications my coverage of xna is limited to 2d graphics and while i hope to add several 3d chapters in the next edition of this book i don t really get into the whole xbox live 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 organization this book is divided into three parts the first 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
[close]
p. 13
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 benefit 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 of an actual device get the latest information and downloads at http developer.windowsphone.com you can install these tools on top of visual studio 2010 in effect enhancing visual studio 2010 for phone development that s the configuration i used although you can do quite a bit with the phone emulator at some point you ll want to deploy your programs to an actual windows phone 7 device you can register as a phone developer at http developer.windowsphone.com and then have the ability to unlock your phone so you can deploy your programs from visual studio since late july 2010 i ve had an lg gw910 phone to test the programs in this book for the record the final build i installed was 7.0.7003.0 using the phone emulator windows phone 7 supports multi-touch and working with multi-touch is an important part of developing programs for the phone when using the windows phone emulator mouse clicks and mouse movement on the pc can mimic touch on the emulator but for only one finger you can test out multi-touch for real on the phone emulator if you have a multi-touch monitor running under windows 7.
[close]
p. 14
in the absence of a multi-touch monitor you might want to explore simulating multi-touch with multiple mouse devices the site http multitouchvista.codeplex.com has the download you ll need and includes a link to http michaelsync.net/2010/04/06/step-by-step-tutorial installing-multi-touch-simulator-for-silverlight-phone-7 that provides instructions windows phone 7 devices also have a built-in accelerometer which can be very difficult to simulate in an emulator per blomqvist the technical reviewer for this book found an application at http accelkit.codeplex.com that utilizes the webcam and artoolkit to emulate the accelerometer sensor and feed that data into the windows phone 7 emulator through a tcp/http server and although neither of us have tried it out it sounds quite intriguing code samples to illustrate silverlight and xna programming concepts this book describes about 190 complete programs many of them are small and simple but others are larger and more interesting 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 file the location of this zip file is available from my website at www.charlespetzold.com/phone and from the microsoft press blog at http blogs.msdn.com/b/microsoft_press if you find something in the code that is useful in your own software project feel free to use the code without restriction either straight up or modified in whatever way you want that s what it s there for last-minute items as i was nearing the completion this book the first version of the silverlight for windows phone toolkit was released with some additional elements and controls and is available for downloading at http silverlight.codeplex.com 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
[close]
p. 15
configuration 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 thumbs up on the book and anand iyer coordinated the project with microsoft press at microsoft press ben ryan launched the project and devon musgrave had the unenviable job of trying to make my code and prose resemble an actual book we all go way back you ll see ben and devon s names on the bottom of the copyright page of programming windows fifth edition published in 1998 my technical reviewer was the diligent per blomqvist who apparently tested all the code in both the sample files and as the listings appear in the book and who in the process caught several errors on my part that were truly well shocking dave edson also reviewed some chapters and served as conduit to the windows phone team to deal with my technical problems and questions early on aaron stebner provided essential guidance michael klucher reviewed chapters and kirti deshpande charlie kindel casey mcgee and shawn oster also had important things to tell me thanks to bonnie lehenbauer for reviewing a chapter i am also indebted to shawn hargreaves for his xna expertise and yochay kiriaty and richard bailey for the lowdown on tombstoning my wife deirdre sinnott has been a marvel of patience and tolerance over the past months as she dealt with an author given to sudden mood swings insane yelling at the computer screen and the conviction that the difficulty of writing a book relieves one of the responsibility of performing basic household chores alas i can t blame any of them for bugs or other problems that remain in this book those are all mine.
[close]