Charles Petzold
Windows
®
Programming
SIXTH EDITION
Writing Windows 8 Apps
With C# and XAML
Release Preview eBook
PUBLISHED BY
Microsoft Press
A Division of Microsoft Corporation
One Microsoft Way
Redmond, Washington 98052-6399
Copyright © 2012 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.
ISBN: 978-0-7356-7176-8
This document supports a preliminary release of a software product that may be changed substantially prior to
final commercial release. This document is provided for informational purposes only and Microsoft makes no
warranties, either express or implied, in this document. Information in this document, including URL and other
Internet website references, is subject to change without notice. The entire risk of the use or the results from
the use of this document remains with the user.
Unless otherwise noted, the companies, organizations, products, domain names, e-mail addresses, logos,
people, places, and events depicted in examples 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.
Complying with all applicable copyright laws is the responsibility of the user. Without limiting the rights under
copyright, no part of this document may be reproduced, stored in or introduced into a retrieval system, or
transmitted in any form or by any means (electronic, mechanical, photocopying, recording, or otherwise), or
for any purpose, without the express written permission of Microsoft Corporation.
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.
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, Developmental, and Project Editor: Devon Musgrave
Technical Reviewer: Marc Young
Cover: Twist Creative • Seattle
Do wn l oa d f ro m W ow! e B oo k < ww w .w owe bo o k. com >
Introduction ......................................................................................................... 9
The Versions of Windows 8 ....................................................................................................................................................... 9
The Focus of This Book ............................................................................................................................................................. 10
The Approach ............................................................................................................................................................................... 12
My Setup ........................................................................................................................................................................................ 13
The Programming Windows Heritage .................................................................................................................................. 13
Behind the Scenes ....................................................................................................................................................................... 16
Errata & Book Support .............................................................................................................................................................. 17
We Want to Hear from You ..................................................................................................................................................... 17
Stay in Touch ................................................................................................................................................................................ 17
Chapter 1: Markup and Code .......................................................................... 18
The First Project ........................................................................................................................................................................... 18
Graphical Greetings .................................................................................................................................................................... 24
Variations in Text ......................................................................................................................................................................... 28
Media As Well .............................................................................................................................................................................. 36
The Code Alternatives ............................................................................................................................................................... 37
Images in Code ............................................................................................................................................................................ 41
Not Even a Page .......................................................................................................................................................................... 43
Chapter 2: XAML Syntax .................................................................................. 45
The Gradient Brush in Code .................................................................................................................................................... 45
Property Element Syntax .......................................................................................................................................................... 48
Content Properties...................................................................................................................................................................... 51
The TextBlock Content Property............................................................................................................................................. 55
Sharing Brushes (and Other Resources) ............................................................................................................................... 57
Resources Are Shared ................................................................................................................................................................ 61
A Bit of Vector Graphics ............................................................................................................................................................ 62
Stretching with Viewbox ........................................................................................................................................................... 71
Styles ............................................................................................................................................................................................... 74
A Taste of Data Binding ............................................................................................................................................................ 80
3
Chapter 3: Basic Event Handling .................................................................... 83
The Tapped Event ........................................................................................................................................................................ 83
Routed Event Handling ............................................................................................................................................................. 86
Overriding the Handled Setting ............................................................................................................................................. 92
Input, Alignment, and Backgrounds ..................................................................................................................................... 93
Size and Orientation Changes ................................................................................................................................................ 97
Bindings to Run? .......................................................................................................................................................................101
Timers and Animation .............................................................................................................................................................103
Chapter 4: The Border Element ..................................................................... 111
The Border Element ..................................................................................................................................................................111
Rectangle and Ellipse ...............................................................................................................................................................115
The StackPanel ...........................................................................................................................................................................117
Horizontal Stacks .......................................................................................................................................................................121
WhatSize with Bindings (and a Converter) .......................................................................................................................124
The ScrollViewer Solution .......................................................................................................................................................128
Layout Weirdness or Normalcy? ..........................................................................................................................................134
Making an E-Book ....................................................................................................................................................................135
Fancier StackPanel Items ........................................................................................................................................................138
Deriving from UserControl .....................................................................................................................................................141
Creating Windows Runtime Libraries .................................................................................................................................143
The Wrap Alternative ...............................................................................................................................................................146
The Canvas and Attached Properties .................................................................................................................................148
The Z-Index .................................................................................................................................................................................153
Canvas Weirdness .....................................................................................................................................................................153
Chapter 5: Control Interaction ...................................................................... 155
The Control Difference ............................................................................................................................................................155
The Slider for Ranges ...............................................................................................................................................................157
The Grid ........................................................................................................................................................................................161
Orientation and Aspect Ratios ..............................................................................................................................................168
Slider and the Formatted String Converter ......................................................................................................................171
4
Tooltips and Conversions .......................................................................................................................................................171
Sketching with Sliders ..............................................................................................................................................................174
The Varieties of Button Experience .....................................................................................................................................176
Defining Dependency Properties .........................................................................................................................................184
RadioButton Tags ......................................................................................................................................................................194
Keyboard Input and TextBox .................................................................................................................................................200
Touch and Thumb .....................................................................................................................................................................204
Chapter 6: WinRT and MVVM ....................................................................... 211
MVVM (Brief and Simplified).................................................................................................................................................211
Data Binding Notifications .....................................................................................................................................................212
A View Model for ColorScroll ................................................................................................................................................214
Syntactic Shortcuts ...................................................................................................................................................................219
The DataContext Property .....................................................................................................................................................222
Bindings and TextBox ...............................................................................................................................................................224
Buttons and MVVM ..................................................................................................................................................................230
The DelegateCommand Class ................................................................................................................................................231
Chapter 7: Building an Application .............................................................. 238
Commands, Options, and Settings ......................................................................................................................................238
The Segoe UI Symbol Font.....................................................................................................................................................240
The Application Bar ..................................................................................................................................................................246
Popups and Dialogs .................................................................................................................................................................248
Windows Runtime File I/O .....................................................................................................................................................251
Await and Async ........................................................................................................................................................................258
Calling Your Own Async Methods .......................................................................................................................................260
Controls for XamlCruncher ....................................................................................................................................................263
Application Settings and Isolated Storage........................................................................................................................278
The XamlCruncher Page .........................................................................................................................................................282
Parsing the XAML......................................................................................................................................................................286
XAML Files In and Out .............................................................................................................................................................288
The Settings Dialog ..................................................................................................................................................................292
5