MANNING Pete Brown Revised Edition of Silverlight 4 in Action IN ACTION Silverlight 5 in Action PETE BROWN MANNING S HELTER I SLAND 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. Development editor: Jeff Bleiel Manning Publications Co. Technical proofreader: Thomas MacKearney 20 Baldwin Road Copyeditor: Liz Welch PO Box 261 Proofreader: Elizabeth Martin Shelter Island, NY 11964 Typesetter: Marija Tudor Cover designer: Marija Tudor ISBN: 9781617290312 Printed in the United States of America 1 2 3 4 5 6 7 8 9 10 – MAL – 18 17 16 15 14 13 12 iii brief contents P ART 1 C ORE S ILVERLIGHT 1 1 ■ Introducing Silverlight 3 2 ■ XAML and the property system 26 3 ■ The application model and the plug-in 47 4 ■ Working with HTML and browsers 73 5 ■ Out-of-browser applications 95 6 ■ The security model and elevated trust 114 P ART 2 C REATING THE USER INTERFACE 125 7 ■ Rendering, layout, and transforming 127 8 ■ Panels 160 9 ■ Human input 180 10 ■ Text fundamentals 199 11 ■ Editing plain and rich text 225 12 ■ Control basics and UserControls 247 13 ■ Animation and behaviors 272 BRIEF CONTENTS iv 14 ■ Resources, styles, and control templates 307 15 ■ Extensions, converters, custom controls, and panels 337 PART 3 WORKING WITH DATA AND SERVICES 365 16 ■ Binding 367 17 ■ Data controls: DataGrid and DataForm 410 18 ■ Input validation 433 19 ■ Networking basics 460 20 ■ Working with SOAP services 491 21 ■ RESTful services with the ASP.NET Web API 520 22 ■ Working with XML, JSON, RSS, and Atom 549 23 ■ Duplex, sockets, and local connections 575 PART 4 2D AND 3D GRAPHICS 601 24 ■ Graphics and effects 603 25 ■ Working with images 630 26 ■ Introduction to 3D 649 27 ■ 3D lighting, texturing, and animation 679 PART 5 MAKING THE MOST OF THE PLATFORM 709 28 ■ Pop-ups, windows, and full-screen applications 711 29 ■ Navigation 734 30 ■ Working with files and directories 761 31 ■ Printing 798 32 ■ COM, Native Extensions, and p-invoke 832 PART 6 BEST PRACTICES 867 33 ■ Structuring and testing with the MVVM pattern 869 34 ■ Debugging your application 913 35 ■ The install experience and preloaders 929 v contents preface xxi acknowledgments xxiv about this book xxvi about the cover illustration xxxi PART 1 CORE SILVERLIGHT 1 1 Introducing Silverlight 3 1.1 A Silverlight primer 4 Silverlight and the web 5 ■ Silverlight and WPF 6 Types of Silverlight applications 6 1.2 A brief history of Silverlight 8 Features for business and client applications 8 ■ Media and graphics enhancements 10 ■ User interaction 11 ■ Text 11 1.3 Getting started with Silverlight development 12 Setting up your development environment 13 ■ Helpful sites 13 1.4 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 CONTENTS vi 2 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 39 Object trees 39 ■ Namescope 42 2.3 XAML type converters 43 2.4 Loading XAML at runtime 44 2.5 Summary 46 3 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 60 Creating an instance of the Silverlight plug-in 61 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 4 Working with HTML and browsers 73 4.1 Silverlight and the HTML DOM 74 4.2 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 82 Calling managed code from JavaScript 82 ■ Using JavaScript from managed code 85 CONTENTS vii 4.5 Hosting HTML in Silverlight 86 Hosting the WebBrowser control 87 ■ Using the WebBrowserBrush 92 4.6 Summary 94 5 Out-of-browser applications 95 5.1 Implementation specifics 97 Process and hosting 97 ■ Capabilities and restrictions 98 5.2 The end-user experience 98 5.3 Creating out-of-browser applications 100 The out-of-browser settings file 100 ■ Controlling the experience 102 ■ Customizing icons 105 ■ Updating 105 5.4 Alerting the user with notification toast 106 5.5 Controlling the host window 107 Basic window properties 108 ■ Changing window chrome 109 ■ Minimizing, maximizing, restoring, and closing 110 ■ Moving a window 111 ■ Resizing 111 5.6 Summary 112 6 The security model and elevated trust 114 6.1 Code classifications and the transparency model 115 6.2 User initiation and consent 117 6.3 Elevated trust 119 Creating elevated trust applications 120 ■ Enabling in-browser elevated trust applications 122 ■ Detecting elevated trust mode 124 6.4 Summary 124 PART 2 CREATING THE USER INTERFACE 125 7 Rendering, layout, and transforming 127 7.1 The UIElement and FrameworkElement 128 Properties 128 ■ Methods 134 7.2 The rendering process 135 Clock tick 137 ■ Per-frame rendering callback 137 Rasterization 138 CONTENTS viii 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 155 PlaneProjection 155 ■ Matrix3dProjection 157 7.6 Summary 159 8 Panels 160 8.1 Canvas 161 Setting the offsets 162 ■ Setting the stack order 163 8.2 The StackPanel 165 8.3 The WrapPanel 166 Vertical wrapping 167 ■ Horizontal wrapping 168 8.4 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 9 Human input 180 9.1 Capturing the keyboard 181 Understanding focus 181 ■ Handling keyboard events 182 Dealing with modifier keys 184 9.2 Mouse input 185 Mouse movement events 186 ■ Mouse button events 188 Using the mouse wheel 191 9.3 Using multitouch 193 9.4 Collecting ink drawings 194 Creating the InkPresenter 195 ■ Collecting ink 195 Styling the ink 197 9.5 Summary 198 CONTENTS ix 10 Text fundamentals 199 10.1 The text system 200 Subpixel text rendering 200 ■ Text hinting 201 ■ Text formatting 202 ■ Text rendering 203 10.2 Displaying text 204 Font properties 204 ■ Flow control 208 ■ Text properties 209 ■ Spacing 212 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 Embedding fonts 223 10.5 Summary 224 11 Editing plain and rich text 225 11.1 Handling basic text input 226 Enabling multiline text support 227 ■ Mastering text selection 228 11.2 Understanding input method editors 228 11.3 Copying text with the Clipboard API 231 11.4 Collecting sensitive data 232 11.5 Entering and displaying rich text 233 Formatting and inline elements 233 ■ Working with selected text 237 11.6 Multicolumn and free-form linked text 241 Multicolumn text 241 ■ Free-form text layout 242 11.7 Summary 245 12 Control basics and UserControls 247 12.1 Control 248 Appearance 248 ■ Tab navigation and control state 249 Templating 250 12.2 ContentControl 251 The ContentPresenter 252 12.3 Button controls 253 The Button 254 ■ The HyperlinkButton 255 The RadioButton 255 ■ The CheckBox 257 [...]... SERVICES 3 65 Binding 16.1 367 Binding basics 368 Mastering the binding syntax mode 371 16.2 369 ■ Choosing a binding 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 to an entire collection 383 Deciding when to update binding 3 85 ■ ■ ■ ■ xii... 4 95 Sending data using the proxy 499 ■ ■ xiv CONTENTS 20.2 Using WCF services and complex data types 50 0 Creating the Silverlight- enabled WCF service 50 1 Sharing type definitions 50 5 Adding the service reference 50 8 Using the service 50 9 ■ ■ 20.3 20.4 Using the configuration file 51 1 Error handling with WCF 51 3 Using an out parameter 51 3 Exposing exception information for debugging 51 5 Error handling... resources using ■ ■ Summary 54 8 Working with XML, JSON, RSS, and 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 items 56 9 22.4 23 ■ DataContractJsonSerializer 56 6 ■ Working with feed Summary 57 3 Duplex, sockets, and local connections 23.1 WCF polling duplex... polling duplex services 57 5 57 6 Creating the project and callback contract 57 7 Creating the service 57 9 Creating the service logic 58 2 Managing client subscriptions 58 4 Using the duplex service 58 5 ■ ■ ■ ■ 56 2 52 2 xv CONTENTS 23.2 Connecting to sockets 58 9 Serving the policy file 59 0 Opening the connection 59 1 Handling the response 59 1 ■ 23.3 Multicast sockets 59 3 Any-Source Multicast/Internet Standard Multicast... writing files: the isolated storage way 7 95 30 .5 31 Reading and Summary 797 Printing 31.1 ■ 798 How Silverlight printing works 799 The PrintDocument class 801 The PrintPage Event 804 Converting to PostScript 807 Rasterization 807 Forcing bitmap printing 808 Forcing vector printing 809 ■ ■ ■ 31.2 Printing onscreen information 810 Printing the content as is 810 Reparenting the elements to fit 812 Scaling... faults 51 6 ■ ■ 20 .5 21 Summary 51 8 RESTful services with the ASP.NET Web API 52 0 21.1 Creating a RESTful service using the ASP.NET Web API Solution setup 52 3 Creating the services 52 5 Testing the service using the browser 53 0 Adding the Silverlight project 53 1 ■ ■ ■ 21.2 Consuming REST services REST service GET operations POSTing to the service 54 4 DELETE 54 6 21.3 22 53 9 54 0 Updating resources by Removing... 639 Zooming in and out 639 Managing the viewport 641 Deploying multiscale images 643 ■ ■ 624 xvi CONTENTS 25. 4 Dealing with dead space 643 Filling the space 644 Uniform sizing 6 45 area 646 UniformToFill 647 ■ ■ Fill the ■ 25. 5 26 Summary 647 Introduction to 3D 649 26.1 26.2 3D—a natural way of interacting with information The Silverlight/ XNA 3D API 652 Rendering pipeline 26.3 26.4 26 .5 652 ■ 650 Project... and IDataErrorInfo 4 45 ■ ■ ■ 18.4 Asynchronous validation with INotifyDataErrorInfo The INotifyDataErrorInfo interface 446 interface 447 Binding support 448 ■ ■ ■ 446 Implementing the Building the WCF xiii CONTENTS web service 448 Adding the client service code 449 Property modifications 450 ■ 18 .5 Annotating for validation 451 Validation attributes 452 Annotating your entity 453 Calling external validation... binding errors in the output window 921 Debugging with custom value converters 922 Using XAML breakpoints 922 ■ ■ 34.3 Troubleshooting network operations 923 Installing Fiddler 924 Monitoring and logging traffic Inspecting individual requests 926 ■ 34.4 Summary 928 924 xx CONTENTS 35 The install experience and preloaders 35. 1 Handling the Silverlight not installed’ scenarios Creating your own install experience... converter 15. 3 339 342 ■ Creating a custom panel Using the converter 343 3 45 Project setup 346 The OrbitPanel class 346 Properties 347 Custom layout 350 Enhancements ■ ■ 15. 4 ■ Creating a custom control 354 355 Choosing the base type 355 Properties 356 The control template contract 357 The default template 359 Visual states 360 Visual states in template 361 ■ ■ ■ ■ 15. 5 PART 3 16 Summary 363 WORKING WITH . 54 4 ■ Removing resources using DELETE 54 6 21.3 Summary 54 8 22 Working with XML, JSON, RSS, and Atom 54 9 22.1 Parsing plain old XML 55 0 LINQ to XML 55 0 ■ XmlSerializer. wheel 191 9.3 Using multitouch 193 9.4 Collecting ink drawings 194 Creating the InkPresenter 1 95 ■ Collecting ink 1 95 Styling the ink 197 9 .5 Summary 198