Preface ...... xi Acknowledgments ...... xiv Introduction ...... xv

Part I: Silverlight Basics 1

Chapter 1: Getting to Know Silverlight ...... 3 What Is Silverlight? ...... 3 Why Use Silverlight? ...... 4 Comparing Silverlight 1.0 and 2 ...... 5 Silverlight Framework Architecture ...... 7 The Silverlight plug-in...... 7 The Silverlight core presentation framework ...... 8 The .NET Silverlight framework ...... 9 Silverlight application architecture ...... 10 Summary ...... 11 Chapter 2: Getting Started with Silverlight ...... 13 Implementing the Silverlight Development Environment ...... 13 Installing Visual Studio 2008 ...... 14 Installing the Silverlight plug-in ...... 14 Installing the Silverlight SDK ...... 15 Implementing Silverlight applications on the platform ...... 17 Understanding Silverlight Development Tools ...... 18 Using Silverlight in Visual Studio 2008 ...... 18 Designing Silverlight applications and elements in Expression Blend ...... 19 EncodingCOPYRIGHTED Silverlight media elements in MATERIALExpression Encoder ...... 20 Outputting vector images as XAML using Expression Design ...... 20 Encoding Multiscale Images Using Deep Zoom Composer ...... 22 Building Your First Silverlight Application ...... 23 Summary ...... 28

v

002_375006-ftoc.indd2_375006-ftoc.indd v 88/28/08/28/08 110:39:570:39:57 PPMM Contents

Part II: Silverlight Design Fundamentals 29

Chapter 3: Using XAML to Build Declarative Silverlight Applications . . . . 31 Understanding the Silverlight/XAML Relationship ...... 31 Understanding XML Namespaces ...... 32 Creating Silverlight Controls ...... 33 Creating a Button ...... 33 Creating a Calendar ...... 34 Creating a CheckBox ...... 35 Creating a DatePicker ...... 36 Creating a HyperlinkButton ...... 37 Creating an Image ...... 37 Creating a ListBox ...... 38 Creating a MediaElement ...... 39 Creating a RadioButton ...... 40 Creating a ScrollBar ...... 41 Creating a ScrollViewer ...... 42 Creating a Slider ...... 43 Creating a TextBlock ...... 44 Creating a TextBox ...... 45 Creating a ToggleButton ...... 46 Designing the Layout of Silverlight Applications ...... 47 Using a Canvas control ...... 47 Using a Grid control ...... 49 Using a TabControl control ...... 54 Using a StackPanel control ...... 57 Using a Border control ...... 58 Using XAML Transforms ...... 59 ScaleTransform ...... 59 RotateTransform...... 60 SkewTransform ...... 61 TranslateTransform ...... 62 Opacity ...... 63 Drawing ...... 65 Creating a Rectangle ...... 65 Creating an Ellipse ...... 65 Creating a Path ...... 66 Using XAML Brushes ...... 66 Adding a SolidColorBrush ...... 67 Adding a LinearGradientBrush ...... 67 Adding a RadialGradientBrush ...... 68 Adding an ImageBrush ...... 70 Adding a VideoBrush ...... 70

vi

002_375006-ftoc.indd2_375006-ftoc.indd vivi 88/28/08/28/08 110:39:570:39:57 PPMM Contents

Implementing Animations and Storyboards ...... 72 Defining Storyboard controls...... 73 Starting animations upon page load ...... 74 Creating From/To animations ...... 74 Creating Key Frame animations ...... 77 Creating animations as Resources ...... 80 Attaching Event Handlers to XAML Elements ...... 81 Summary ...... 81 Chapter 4: Using Expression Blend to Design Silverlight Applications . . . 83 Getting Started with Expression Blend ...... 84 Creating a Silverlight application project in Expression Blend ...... 84 Using the Design, XAML, and Split views ...... 85 Understanding the Expression Blend Toolbox ...... 86 Adding Silverlight controls ...... 90 Arranging Controls in the Design View ...... 91 Using snaplines and gridlines ...... 91 Aligning controls ...... 93 Nesting controls using layout panels ...... 94 Ordering controls ...... 97 Modifying the Properties of Silverlight Controls ...... 97 Naming the control ...... 99 Attaching event handlers to controls ...... 99 Applying brushes to controls ...... 100 Setting appearance properties of controls ...... 105 Setting the layout of controls in the Silverlight application ...... 105 Applying transforms to controls ...... 107 Modifying other control properties ...... 111 Creating Storyboards and Animations in Expression Blend ...... 115 Creating a Storyboard...... 115 Animating Silverlight controls in a Storyboard timeline ...... 116 Playing animations in Expression Blend ...... 117 Recording and modifying key frames ...... 119 Importing Vector Images Using Expression Design and Expression Blend ...... 120 Exporting XAML from Expression Design ...... 120 Adding exported XAML to a Silverlight application ...... 122 Summary ...... 123 Chapter 5: Encoding Audio, Video, and Images for Silverlight Applications ...... 125 Configuring Video in a Job ...... 126 Importing audio and video into Expression Encoder ...... 126 Using the timeline controls to cut audio or video ...... 127

vii

002_375006-ftoc.indd2_375006-ftoc.indd viivii 88/28/08/28/08 110:39:570:39:57 PPMM Contents

Configuring Audio and Video Encode Settings ...... 128 Configuring an encoded media file profile ...... 128 Configuring a video profile for display in Silverlight ...... 131 Setting audio and video preprocessing options ...... 132 Encoding Metadata in the Media File ...... 133 Adding ...... 134 Adding timeline markers to video ...... 135 Adding script commands to a video file ...... 137 Configuring Silverlight Output ...... 140 Generating a thumbnail ...... 140 Specifying a template ...... 141 Modifying the template in Expression Blend ...... 141 Specifying output options ...... 142 Implementing Encoded Media in Silverlight Applications ...... 144 Understanding encoded output ...... 144 Using the encoded output in a Silverlight application ...... 145 Encoding Multiscale Images Using Deep Zoom Composer ...... 149 Importing Images into Deep Zoom Composer ...... 150 Arranging Images in Deep Zoom Composer ...... 151 Exporting Multiscale Images from Deep Zoom Composer ...... 151 Understanding the Exported Multiscale Image Project ...... 154 Summary ...... 154

Part III: Programming Silverlight Applications 157

Chapter 6: Using Visual Studio to Program Silverlight Applications . . . . 159 Developing a .NET Silverlight Application in Visual Studio...... 159 Creating a Silverlight project ...... 160 Adding Silverlight controls ...... 162 Naming Silverlight controls ...... 165 Adding event handlers to Silverlight controls ...... 166 Testing Silverlight applications in Visual Studio ...... 167 Using IntelliSense ...... 168 Using the Solution Explorer ...... 169 Switching to Expression Blend ...... 169 Adding resources...... 170 Build settings...... 173 Summary ...... 175 Chapter 7: Creating .NET Silverlight Applications ...... 177 Understanding the Anatomy of a .NET Silverlight Application ...... 177 Application package ...... 178 Implementing assemblies ...... 185 Using resource files ...... 193

viii

002_375006-ftoc.indd2_375006-ftoc.indd viiiviii 88/28/08/28/08 110:39:570:39:57 PPMM Contents

Implementing the Application Class ...... 197 Application class methods ...... 197 Displaying the application UI ...... 202 Using initialization parameters ...... 205 Deploying .NET Silverlight Applications in Web Pages ...... 206 Embedding a Silverlight plug-in in an HTML Web page...... 207 Embedding a Silverlight application using the asp:Silverlight tag ...... 211 Summary ...... 212 Chapter 8: Programming .NET Silverlight Applications ...... 213 Manipulating XAML Elements Programmatically ...... 213 Accessing namescoped XAML elements ...... 214 Modifying existing Silverlight XAML elements ...... 214 Dynamically adding/removing XAML elements ...... 216 Implementing Event Handling ...... 222 Application events ...... 225 Silverlight control events ...... 226 Mouse events ...... 229 Keyboard events ...... 234 Media events ...... 237 Other control events...... 242 Controlling Animations Programmatically...... 247 Starting and stopping the animation ...... 247 Handling the animation completed event ...... 251 Dynamically creating animations in code ...... 251 Implementing Multiscale Images in Silverlight Applications ...... 257 Summary ...... 268 Chapter 9: Programming Silverlight Applications with JavaScript ...... 271 Understanding the Silverlight–JavaScript Relationship ...... 271 Managed JavaScript versus unmanaged JavaScript ...... 272 Using the Silverlight.js library ...... 272 Creating and Embedding the Silverlight Application ...... 273 Configuring parameters of the createObject and createObjectEx functions ...... 274 Creating the Silverlight object in a separate JavaScript file ...... 280 Accessing XAML Using Unmanaged JavaScript ...... 280 Accessing the Silverlight object...... 281 Accessing the Silverlight application settings ...... 282 Accessing XAML content ...... 286 Implementing Event Handling in Unmanaged JavaScript ...... 292 Dynamically Loading Content in Unmanaged JavaScript ...... 295 Understanding the Downloader object ...... 296 Dynamically loading XAML ...... 300 Dynamically loading text and media ...... 303 Summary ...... 306

ix

002_375006-ftoc.indd2_375006-ftoc.indd ixix 88/28/08/28/08 110:39:570:39:57 PPMM Contents

Chapter 10: Programming Silverlight Applications with ASP.NET Services and AJAX ...... 307 Creating an ASP.NET Web Service to Use Silverlight ...... 307 Creating an ASP.NET Web application ...... 308 Registering the Silverlight assembly ...... 308 Adding a ScriptManager ...... 309 Embedding an asp:Silverlight tag in an ASP.NET Web page ...... 310 Creating a Web service ...... 313 Adding Web methods to the ASP.NET Web service ...... 314 Creating a Silverlight Application for ASP.NET Web Services ...... 317 Linking a Silverlight application to an ASP.NET Web application ...... 317 Adding a service reference to the Silverlight application ...... 319 Accessing an ASP.NET Web service from Silverlight ...... 320 Using asp:MediaPlayer AJAX Element to Directly Embed Video ...... 325 Registering the Silverlight assembly ...... 326 Adding a ScriptManager ...... 326 Embedding an asp:MediaPlayer tag in an ASP.NET Web page...... 326 Adding MediaChapters controls ...... 332 Encoding video for AJAX scripting ...... 333 Adding an AJAX-enabled WCF service ...... 335 Implementing marker events in AJAX ...... 337 Summary ...... 341 Chapter 11: Programming Silverlight Applications Using the ...... 343 Configuring a Silverlight DLR Application ...... 343 Embedding a Silverlight DLR application in a Web page ...... 344 Building the AppManifest.xml file ...... 345 Using Chiron ...... 346 Using Chiron as a Web server to test applications ...... 346 Creating an XAP file ...... 348 DLR Console on the Web ...... 348 Creating an IronPython Silverlight Application ...... 349 Creating XAML for the IronPython Silverlight application ...... 350 Creating a for the IronPython Silverlight application ...... 350 Providing access to CLR libraries and namespaces in Python...... 351 Accessing XAML from IronPython ...... 352 Implementing event handling in IronPython ...... 352 Adding dynamic XAML using IronPython ...... 353 Example of an IronPython Silverlight application ...... 354 Creating an IronRuby Silverlight Application ...... 357 Creating XAML for the IronRuby Silverlight application ...... 357 Creating a manifest for the IronRuby Silverlight application ...... 358 Providing access to CLR libraries and namespaces in IronRuby ...... 359

x

002_375006-ftoc.indd2_375006-ftoc.indd x 88/28/08/28/08 110:39:570:39:57 PPMM Contents

Accessing XAML from IronRuby ...... 360 Implementing event handling in IronRuby ...... 360 Adding dynamic XAML using IronRuby ...... 361 Example of an IronRuby Silverlight application ...... 361 Creating a Managed JavaScript Silverlight Application ...... 365 Creating XAML for the managed JavaScript Silverlight application ...... 365 Creating a manifest for the managed JavaScript Silverlight application ...... 367 Providing access to CLR libraries and namespaces in managed JavaScript ...... 368 Accessing XAML from managed JavaScript ...... 368 Implementing event handling in managed JavaScript ...... 368 Adding dynamic XAML using managed JavaScript ...... 369 Example of a managed JavaScript Silverlight application ...... 370 Summary ...... 373 Chapter 12: Programming Custom Silverlight Controls and Libraries . . .375 Creating Custom Silverlight Controls in Applications ...... 375 Creating a custom User Control in a Visual Studio project ...... 376 Creating the custom control UI ...... 377 Coding the functionality ...... 378 Implementing the custom control in the project ...... 378 Example of implementing a custom Silverlight User Control ...... 379 Creating Silverlight Class Library Controls ...... 384 Creating the Silverlight library ...... 384 Defining the UI for the custom control ...... 386 Implementing the custom control functionality ...... 388 Example of a custom control Silverlight class library ...... 390 Adding a Silverlight Class Library Control to an Application ...... 396 Summary ...... 401

Part IV: Understanding Silverlight Frameworks 403

Chapter 13: Using the Silverlight Presentation Framework ...... 405 Understanding the Silverlight Presentation Framework ...... 405 Interacting with the HTML DOM ...... 406 Accessing the HTML DOM from managed code ...... 407 Accessing managed code from HTML JavaScript ...... 412 Accessing the Local File System from Silverlight Applications ...... 417 Using the ’s native open for read-only ...... 417 Accessing the isolated local storage...... 420 Implementing Threading in Silverlight Applications ...... 425 Implementing a timer thread ...... 425 Adding background threads to Silverlight applications ...... 428 Summary ...... 437

xi

002_375006-ftoc.indd2_375006-ftoc.indd xixi 88/28/08/28/08 110:39:570:39:57 PPMM Contents

Chapter 14: Using the Silverlight Communication Framework ...... 439 Enabling Network Security Access for Silverlight Applications ...... 440 Using a WebClient to Download Resources on Demand ...... 444 Using a WebClient to download a text file ...... 445 Using a WebClient to download and access a ZIP package ...... 448 Accessing RSS Services ...... 453 Implementing Sockets in Silverlight Applications ...... 459 Opening a socket to a remote host ...... 460 Sending data on a socket ...... 461 Receiving data from a socket ...... 462 Closing a socket ...... 463 Example of a Silverlight socket application ...... 463 Using the HttpWebRequest and HttpWebResponse Objects ...... 473 Using the HttpWebRequest to send requests ...... 473 Using the HttpWebResponse class to handle responses ...... 475 Example of using HttpWebRequest and HttpWebResponse to send POST data ...... 476 Summary ...... 482 Chapter 15: Using the Silverlight Data Framework ...... 483 Using XmlReader and XmlWriter ...... 483 Implementing XmlReader to parse XML data ...... 484 Implementing XmlWriter to write XML data ...... 486 Example: Reading and writing XML data in memory ...... 487 Serializing a Data Object into XML ...... 492 Serializing objects into XML ...... 493 Deserializing objects from serialized XML ...... 494 Example: Serializing and deserializing XML data in isolated storage ...... 494 Implementing Data Binding in Silverlight Applications ...... 500 Implementing a DataGrid Control ...... 506 Using LINQ in Silverlight to Query SQL Databases ...... 510 Creating a Web server Silverlight application project that accesses SQL using LINQ ...... 511 Setting up the Silverlight project to use the LINQ service ...... 516 Summary ...... 522

Part V: Appendixes 523

Appendix A: Silverlight Resources ...... 525 Appendix B: Silverlight Control Reference ...... 527

Index ...... 545

xii

002_375006-ftoc.indd2_375006-ftoc.indd xiixii 88/28/08/28/08 110:39:570:39:57 PPMM