IN ACTION Revised Edition of Silverlight in Action Pete Brown MANNING www.it-ebooks.info Silverlight in Action PETE BROWN MANNING SHELTER ISLAND www.it-ebooks.info For online information and ordering of this and other Manning books, please visit www.manning.com The publisher offers discounts on this book when ordered in quantity For more information, please contact Special Sales Department Manning Publications Co 20 Baldwin Road PO Box 261 Shelter Island, NY 11964 Email: orders@manning.com ©2012 by Manning Publications Co All rights reserved No part of this publication may be reproduced, stored in a retrieval system, or transmitted, in any form or by means electronic, mechanical, photocopying, or otherwise, without prior written permission of the publisher Many of the designations used by manufacturers and sellers to distinguish their products are claimed as trademarks Where those designations appear in the book, and Manning Publications was aware of a trademark claim, the designations have been printed in initial caps or all caps Recognizing the importance of preserving what has been written, it is Manning’s policy to have the books we publish printed on acid-free paper, and we exert our best efforts to that end Recognizing also our responsibility to conserve the resources of our planet, Manning books are printed on paper that is at least 15 percent recycled and processed without elemental chlorine Manning Publications Co 20 Baldwin Road PO Box 261 Shelter Island, NY 11964 Development editor: Technical proofreader: Copyeditor: Proofreader: Typesetter: Cover designer: ISBN: 9781617290312 Printed in the United States of America 10 – MAL – 18 17 16 15 14 13 12 www.it-ebooks.info Jeff Bleiel Thomas MacKearney Liz Welch Elizabeth Martin Marija Tudor Marija Tudor brief contents PART PART CORE SILVERLIGHT 1 ■ Introducing Silverlight ■ XAML and the property system ■ The application model and the plug-in ■ Working with HTML and browsers ■ Out-of-browser applications ■ The security model and elevated trust 114 26 47 73 95 CREATING THE USER INTERFACE 125 ■ Rendering, layout, and transforming ■ Panels 160 ■ Human input 180 10 ■ Text fundamentals 11 ■ Editing plain and rich text 225 12 ■ Control basics and UserControls 13 ■ Animation and behaviors 199 iii www.it-ebooks.info 272 247 127 iv PART PART PART PART BRIEF CONTENTS 14 ■ Resources, styles, and control templates 307 15 ■ Extensions, converters, custom controls, and panels 337 WORKING WITH DATA AND SERVICES 365 16 ■ Binding 367 17 ■ Data controls: DataGrid and DataForm 18 ■ Input validation 433 19 ■ Networking basics 20 ■ Working with SOAP services 21 ■ RESTful services with the ASP.NET Web API 22 ■ Working with XML, JSON, RSS, and Atom 23 ■ Duplex, sockets, and local connections 410 460 491 520 549 575 2D AND 3D GRAPHICS 601 24 ■ Graphics and effects 603 25 ■ Working with images 630 26 ■ Introduction to 3D 27 ■ 3D lighting, texturing, and animation 649 679 MAKING THE MOST OF THE PLATFORM 709 28 ■ Pop-ups, windows, and full-screen applications 29 ■ Navigation 734 30 ■ Working with files and directories 31 ■ Printing 798 32 ■ COM, Native Extensions, and p-invoke 711 761 832 BEST PRACTICES 867 33 ■ Structuring and testing with the MVVM pattern 869 34 ■ Debugging your application 35 ■ The install experience and preloaders www.it-ebooks.info 913 929 contents preface xxi acknowledgments xxiv about this book xxvi about the cover illustration PART 1 xxxi CORE SILVERLIGHT Introducing Silverlight 1.1 A Silverlight primer Silverlight and the web Silverlight and WPF Types of Silverlight applications ■ 1.2 A brief history of Silverlight Features for business and client applications Media and graphics enhancements 10 User interaction 11 Text 11 ■ ■ 1.3 ■ Getting started with Silverlight development 12 Setting up your development environment 13 1.4 ■ Helpful sites Building your first Silverlight web application 14 Project setup 15 User interface 16 Calling Twitter search 18 Parsing the results and binding the ListBox 19 Making the ListBox contents more meaningful 23 ■ ■ ■ 1.5 Summary 25 v www.it-ebooks.info 13 vi CONTENTS XAML and the property system 26 2.1 XAML basics 27 Objects 28 Namespaces 29 Properties 32 Dependency properties 33 Attached properties 35 Events 36 Commands 38 ■ ■ ■ ■ 2.2 Object trees and namescope Object trees 39 2.3 2.4 2.5 39 Namescope 42 ■ XAML type converters 43 Loading XAML at runtime 44 Summary 46 The application model and the plug-in 47 3.1 The Silverlight application model 48 Application startup process 49 XAP 50 The application manifest file 51 The Silverlight application object 52 Application dependencies 55 Assembly caching 56 ■ ■ ■ ■ 3.2 Creating the Silverlight plug-in 58 Using the object tag 59 Using the Silverlight.js utility file Creating an instance of the Silverlight plug-in 61 60 ■ 3.3 Integrating the Silverlight plug-in 62 Relating the Silverlight application to the HTML DOM 62 Clarifying the initial experience 64 Handling plug-in events 68 Sending initialization parameters 70 ■ ■ 3.4 Summary 71 Working with HTML and browsers 73 4.1 4.2 Silverlight and the HTML DOM 74 Working with the web page from managed code 75 Navigating web page contents 76 Working with element properties 77 Handling CSS information 78 Accessing the query string 78 ■ ■ 4.3 Working with the hosting browser window 79 Prompting the user 79 Navigating the browser window 81 Discovering the browser properties 81 ■ 4.4 Bridging the scripting and managed code worlds Calling managed code from JavaScript 82 from managed code 85 www.it-ebooks.info ■ 82 Using JavaScript vii CONTENTS 4.5 Hosting HTML in Silverlight Hosting the WebBrowser control WebBrowserBrush 92 4.6 Summary 86 87 ■ Using the 94 Out-of-browser applications 95 5.1 Implementation specifics Process and hosting 5.2 5.3 97 ■ 97 Capabilities and restrictions 98 The end-user experience 98 Creating out-of-browser applications 100 The out-of-browser settings file 100 Controlling the experience 102 Customizing icons 105 Updating ■ ■ 5.4 5.5 ■ Alerting the user with notification toast Controlling the host window 107 105 106 Basic window properties 108 Changing window chrome 109 Minimizing, maximizing, restoring, and closing 110 Moving a window 111 Resizing 111 ■ ■ ■ 5.6 Summary ■ 112 The security model and elevated trust 114 6.1 6.2 6.3 Code classifications and the transparency model 115 User initiation and consent 117 Elevated trust 119 Creating elevated trust applications 120 Enabling in-browser elevated trust applications 122 Detecting elevated trust mode 124 ■ ■ 6.4 PART Summary 124 CREATING THE USER INTERFACE 125 Rendering, layout, and transforming 127 7.1 The UIElement and FrameworkElement Properties 7.2 128 ■ 128 Methods 134 The rendering process 135 Clock tick 137 Per-frame rendering callback Rasterization 138 ■ www.it-ebooks.info 137 viii CONTENTS 7.3 The layout system 144 Multipass layout—measuring and arranging 144 The LayoutInformation class 146 Performance considerations 147 ■ 7.4 Render transforms 148 RotateTransform 149 ScaleTransform 150 SkewTransform 150 TranslateTransform 151 TransformGroup 151 CompositeTransform 152 MatrixTransform 153 ■ ■ ■ 7.5 3D projection transforms PlaneProjection 155 7.6 Summary ■ 155 Matrix3dProjection 157 159 Panels 160 8.1 Canvas 161 Setting the offsets 162 8.2 8.3 The StackPanel 165 The WrapPanel 166 Vertical wrapping 8.4 Setting the stack order 163 ■ 167 ■ Horizontal wrapping 168 The Grid 169 Arranging Grid content 170 Positioning Grid content 172 Spanning cells 172 Sizing it up 173 Working with the grid programmatically 176 Customizing cell boundaries 177 ■ ■ ■ ■ 8.5 Summary 179 Human input 180 9.1 Capturing the keyboard 181 Understanding focus 181 Handling keyboard events Dealing with modifier keys 184 ■ 9.2 Mouse input 185 Mouse movement events 186 Using the mouse wheel 191 9.3 9.4 Mouse button events 188 ■ Using multitouch 193 Collecting ink drawings 194 Creating the InkPresenter 195 Styling the ink 197 9.5 182 Summary ■ 198 www.it-ebooks.info Collecting ink 195 ix CONTENTS 10 Text fundamentals 199 10.1 The text system 200 Subpixel text rendering 200 Text hinting 201 formatting 202 Text rendering 203 ■ ■ Text ■ 10.2 Displaying text 204 Font properties 204 Flow control properties 209 Spacing 212 ■ 208 ■ Text ■ 10.3 OpenType font support 215 Ligatures 216 Stylistic sets 217 Font capitals 219 Fractions and numbers 220 Variants, superscript, and subscript 221 ■ ■ ■ 10.4 10.5 11 Embedding fonts 223 Summary 224 Editing plain and rich text 11.1 225 Handling basic text input 226 Enabling multiline text support 227 selection 228 11.2 11.3 11.4 11.5 12 ■ 247 Control 248 ■ Tab navigation and control state 249 ContentControl 251 The ContentPresenter 252 12.3 242 Summary 245 Appearance 248 Templating 250 12.2 Working with selected Free-form text layout Control basics and UserControls 12.1 ■ Multicolumn and free-form linked text 241 Multicolumn text 241 11.7 Mastering text Understanding input method editors 228 Copying text with the Clipboard API 231 Collecting sensitive data 232 Entering and displaying rich text 233 Formatting and inline elements 233 text 237 11.6 ■ Button controls 253 The Button 254 The HyperlinkButton 255 The RadioButton 255 The CheckBox 257 ■ ■ www.it-ebooks.info ... Atom 54 9 22.1 Parsing plain old XML LINQ to XML 22.2 55 0 ■ 55 0 XmlSerializer 55 4 Working with JSON 55 8 JsonObject and JsonArray 22.3 55 9 ■ Working with RSS and Atom Reading syndication feeds 56 6... Understanding your binding source 373 Binding to a property 374 Binding to an object 376 Binding to a UI element 378 Binding to an indexed element 381 Binding to a keyed (string indexed) element 382 Binding... caching 56 ■ ■ ■ ■ 3.2 Creating the Silverlight plug -in 58 Using the object tag 59 Using the Silverlight. js utility file Creating an instance of the Silverlight plug -in 61 60 ■ 3.3 Integrating