Programming Windows®, Sixth Edition
Total Page:16
File Type:pdf, Size:1020Kb
Programming Windows ®, Sixth Edition Charles Petzold PUBLISHED BY Microsoft Press A Division of Microsoft Corporation One Microsoft Way Redmond, Washington 98052-6399 Copyright © 2013 by Charles Petzold 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: 2012955549 ISBN: 978-0-7356-7176-8 Printed and bound in the United States of America. Second Printing Microsoft Press books are available through booksellers and distributors worldwide. If you need support related to this book, email Microsoft Press Book Support at [email protected]. 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, 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: Devon Musgrave Developmental Editor: Devon Musgrave Project Editor: Devon Musgrave Editorial Production: Waypoint Press Technical Reviewer: Marc Young Indexer: Christina Yeager Cover: Twist Creative • Seattle and Joel Panchot [M] [2013-02-08] Contents at a glance Introduction xvii PART I ELEMENTALS CHAPTER 1 Markup and Code 3 CHAPTER 2 XAML Syntax 31 CHAPTER 3 Basic Event Handling 69 CHAPTER 4 Presentation with Panels 97 CHAPTER 5 Control Interaction 139 CHAPTER 6 WinRT and MVVM 193 CHAPTER 7 Asynchronicity 221 CHAPTER 8 App Bars and Popups 261 CHAPTER 9 Animation 329 CHAPTER 10 Transforms 377 CHAPTER 11 The Three Templates 449 CHAPTER 12 Pages and Navigation 539 PART II SPECIALTIES CHAPTER 13 Touch, Etc. 615 CHAPTER 14 Bitmaps 683 CHAPTER 15 Going Native 779 CHAPTER 16 Rich Text 845 CHAPTER 17 Share and Print 893 CHAPTER 18 Sensors and GPS 953 CHAPTER 19 Pen (Also Known as Stylus) 1013 Index 1057 This page intentionally left blank Table of Contents Introduction xvii PART I ELEMENTALS Chapter 1 Markup and Code 3 The First Project . 3 Graphical Greetings . 9 Variations in Text . .13 Media As Well . .22 The Code Alternatives . .23 Images in Code . .27 Not Even a Page . .29 Chapter 2 XAML Syntax 31 The Gradient Brush in Code . .31 Property Element Syntax . .34 Content Properties . .37 The TextBlock Content Property . .41 Sharing Brushes (and Other Resources) . .43 Resources Are Shared . .47 Exploring Vector Graphics . .48 What do you think of this book? We want to hear from you! Microsoft is interested in hearing your feedback so we can continually improve our books and learning resources for you. To participate in a brief online survey, please visit: microsoft.com/learning/booksurvey vii Stretching with Viewbox. .58 Styles . .60 A Taste of Data Binding . .66 Chapter 3 Basic Event Handling 69 The Tapped Event . .69 Routed Event Handling . .72 Overriding the Handled Setting . .78 Input, Alignment, and Backgrounds . .80 Size and Orientation Changes . .83 Bindings to Run? . .87 Timers and Animation . .89 Chapter 4 Presentation with Panels 97 The Border Element . .97 Rectangle and Ellipse . .101 The StackPanel . .103 Horizontal Stacks . .106 WhatSize with Bindings (and a Converter) . .108 The ScrollViewer Solution . .112 Layout Weirdness or Normalcy?. .118 Making an E-Book . .119 Fancier StackPanel Items . .122 Deriving from UserControl . .124 Creating Windows Runtime Libraries . .127 The Wrap Alternative . .130 The Canvas and Attached Properties . .132 The Z-Index . .136 Canvas Weirdness . .137 viii Contents Chapter 5 Control Interaction 139 The Control Difference . .139 The Slider for Ranges . .141 The Grid . .146 Orientation and Aspect Ratios . .152 Slider and the Formatted String Converter . .154 Tooltips and Conversions . .154 Sketching with Sliders. .157 The Varieties of Button Experience . .159 Defi ning Dependency Properties . .167 RadioButton Tags . .177 Keyboard Input and TextBox . .184 Touch and Thumb . .187 Chapter 6 WinRT and MVVM 193 MVVM (Brief and Simplifi ed) . .193 Data Binding Notifi cations . .194 A View Model for ColorScroll . .196 Syntactic Shortcuts . .201 The DataContext Property . .204 Bindings and TextBox . .206 Buttons and MVVM . .212 The DelegateCommand Class . .213 Chapter 7 Asynchronicity 221 Threads and the User Interface . .221 Working with MessageDialog . .222 Callbacks as Lambda Functions . .228 The Amazing await Operator . .229 Cancelling an Asynchronous Operation . .231 Contents ix Approaches to File I/O . .233 Application Local Storage . .234 File Pickers . .234 Bulk Access . .235 File Pickers and File I/O . .235 Handling Exceptions . ..