Microsoft press ebook_programming_windows_phone_7_pdf

4,238
-1

Published on

windows 7 programming

0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
4,238
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
75
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Microsoft press ebook_programming_windows_phone_7_pdf

  1. 1. PUBLISHED BYMicrosoft PressA Division of Microsoft CorporationOne Microsoft WayRedmond, Washington 98052-6399Copyright © 2010 by Microsoft CorporationAll rights reserved. No part of the contents of this book may be reproduced or transmitted in any form or by any meanswithout the written permission of the publisher.Library of Congress Control Number: 2010939982ISBN: 978-0-7356-4335-2Printed and bound in the United States of America.Microsoft Press books are available through booksellers and distributors worldwide. For further information aboutinternational editions, contact your local Microsoft Corporation office or contact Microsoft Press International directly atfax (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/EM-US.aspx are trademarks of the Microsoft group of companies. All other marks are the property of their respective owners.The example companies, organizations, products, domain names, e-mail addresses, logos, people, places, and eventsdepicted 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 anyexpress, statutory, or implied warranties. Neither the authors, Microsoft Corporation, nor its resellers, or distributors willbe held liable for any damages caused or alleged to be caused either directly or indirectly by this book.Developmental and Project Editor: Devon MusgraveEditorial Production: Ashley Schneider, S4Carlisle Publishing ServicesTechnical Reviewer: Per Blomqvist; Technical Review Services provided by Content Master, a member of CM Group,Ltd.Cover: Tom Draper DesignBody Part No. X17-12906 iii
  2. 2. 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 ........................................... 103Part 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
  3. 3. 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
  4. 4. Table of Contents Introduction ................................................................................. xii Part I The Basics 1 Hello, Windows Phone 7............................................................... 2 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 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
  5. 5. 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 ..................................................................... 136Part II Silverlight 7 XAML Power and Limitations................................................... 138 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 8 Elements and Properties........................................................... 165 Basic Shapes ................................................................................................. 165 vii
  6. 6. 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........................................................................................... 22810 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
  7. 7. TextBox and Keyboard Input ..................................................................... 28611 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 .................................................................................... 33212 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 ........................................................................... 38013 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
  8. 8. The Path Markup Syntax ............................................................................ 450 How This Chapter Was Created ................................................................. 45514 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....................................................... 49715 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....................................................... 57316 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
  9. 9. 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 ....................................................... 742Part III XNA 19 Principles of Movement............................................................ 750 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 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
  10. 10. 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......................................................................... 83522 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............................................................ 88923 Touch and Play .......................................................................... 900 More Game Components ............................................................................ 900 The PhingerPaint Canvas ...................................................