MacDonald
Silverlight 3 in C#
Companion
eBook Available
trim = 7" x 9.125" spine = 1.90625" 832 page count
The eXPeRT’s VOIce
®
In sIlVeRlIghT
Pro
Silverlight 3
in C#
Matthew MacDonald
Create cross-platform .NET applications for the browser
Full Color Inside
Full Color Inside
Pro
this print for content only—size & color not accurate
BOOks fOR PROfessIOnals By PROfessIOnals
®
Pro Silverlight 3 in C#
Dear Reader,
Silverlight is a revolutionary browser plug-in that allows developers to create
rich client applications that run inside the browser. Like Adobe Flash, Silverlight
supports event handling, two-dimensional drawing, video playback, network-
ing, and animation. Unlike Flash, Silverlight is tailored to .NET developers.
Most impressively, Silverlight applications execute pure C# code.
The most exciting part of Silverlight is its cross-platform muscle. Unlike
ordinary .NET applications, Silverlight applications run seamlessly in non-
Microsoft browsers (like Firefox) and on non-Microsoft platforms (like Mac OS
X). Essentially, Silverlight is a scaled-down, browser-hosted version of .NET—
and that’s made it the most hotly anticipated technology that Microsoft’s
Beginning
Silverlight 3
Accelerated
Silverlight 3
Silverlight
Recipes
www.apress.com
SOURCE CODE ONLINE
)3".
Author of
Pro WPF in C# 2008
Pro ASP.NET 3.5 in C# 2008
(with Mario Szpuszta)
Beginning ASP.NET 3.5 in
C# 2008
Pro Silverlight 2 in C# 2008
Your Brain: The Missing
Manual
Companion eBook
See last page for details
on $10 eBook version
www.it-ebooks.info
www.it-ebooks.info
occurrence of a trademarked name, we use the names only in an editorial fashion and to the
benefit of the trademark owner, with no intention of infringement of the trademark.
Lead Editor: Ewan Buckingham
Technical Reviewer: Damien Foggon
Editorial Board: Clay Andres, Steve Anglin, Mark Beckner, Ewan Buckingham, Tony
Campbell, Gary Cornell, Jonathan Gennick, Michelle Lowman, Matthew Moodie, Jeffrey
Pepper, Frank Pohlmann, Ben Renow-Clarke, Dominic Shakeshaft, Matt Wade, Tom
Welsh
Coordinating Editor: Anne Collett
Copy Editor: Heather Lang and Tiffany Taylor
Associate Production Director: Kari Brooks-Copony
Production Editor: Brigid Duffy
Compositor: Nancy Wright
Indexer: John Collin
Artist: April Milne
Cover Designer: Anna Ishchenko
Distributed to the book trade worldwide by Springer-Verlag New York, Inc., 233 Spring Street,
6th Floor, New York, NY 10013. Phone 1-800-SPRINGER, fax 201-348-4505, e-mail orders-
, or visit .
For information on translations, please e-mail , or visit .
Apress and friends of ED books may be purchased in bulk for academic, corporate, or
promotional use. eBook versions and licenses are also available for most titles. For more
information, reference our Special Bulk Sales—eBook Licensing web page at
The information in this book is distributed on an “as is” basis, without warranty. Although every
precaution has been taken in the preparation of this work, neither the author(s) nor Apress shall
have any liability to any person or entity with respect to any loss or damage caused or alleged to
be caused directly or indirectly by the information contained in this work.
The source code for this book is available to readers at . You will need to
answer questions pertaining to this book in order to successfully download the code.
■Chapter 13: Templates and Custom Controls 449
■Chapter 14: Browser Integration 491
■Chapter 15: ASP.NET Web Services 517
■Chapter 16: Data Binding 541
■Chapter 17: Data Controls 585
■Chapter 18: Isolated Storage 635
■Chapter 19: Multithreading 657
■Chapter 20: Networking 679
■Index 727
www.it-ebooks.info
www.it-ebooks.info
vii
Contents
■About the Author xxii
■About the Technical Reviewer xxiii
■Acknowledgments xxiv
■Introduction xxv
Understanding Silverlight 9
Silverlight System Requirements 12
Silverlight vs. Flash 12
Silverlight and WPF 14
The Evolution of Silverlight 15
About This Book 17
What You Need to Use This Book 17
The Silverlight Toolkit 18
Code Samples 19
■Chapter 2: XAML 33
XAML Basics 34
XAML Namespaces 34
Core Silverlight Namespaces 35
Design Namespaces 36
Custom Namespaces 37
The Code-Behind Class 38
Naming Elements 39
Properties and Events in XAML 39
Simple Properties and Type Converters 41
Complex Properties 42
Attached Properties 44
Nesting Elements 45
Events 48
The Full Eight Ball Example 49
XAML Resources 50
The Resources Collection 50
The Hierarchy of Resources 51
Accessing Resources in Code 53
Organizing Resources with Resource Dictionaries 54
Element-to-Element Binding 56
One-Way Binding 56
Two-Way Binding 57
The Last Word 59
■Chapter 3: Layout 61
The Layout Containers 61
The Panel Background 63
Borders 65
Simple Layout with the StackPanel 66
■Chapter 4: Dependency Properties and Routed Events 107
Dependency Properties 107
Defining and Registering a Dependency Property 108
Dynamic Value Resolution 110
Attached Properties 111
The WrapBreakPanel Example 112
Routed Events 115
The Core Element Events 115
Event Bubbling 118
Handled (Suppressed) Events 119
An Event Bubbling Example 120
Mouse Movements 123
The Mouse Wheel 123
Capturing the Mouse 125
A Mouse Event Example 126
Mouse Cursors 129
Key Presses 130
Key Modifiers 132
www.it-ebooks.info
■ CONTENTS
x
Focus 133
The Last Word 134
■Chapter 5: Elements 135
The Silverlight Elements 135
Static Text 140
Font Properties 141
Standard Fonts 142
Font Embedding 143
The Slider 176
The ProgressBar 176
Date Controls 177
www.it-ebooks.info
■ CONTENTS
xi
The Last Word 181
■Chapter 6: The Application Model 183
The Application Class 183
Accessing the Current Application 184
Application Properties 184
Application Events 185
Application Startup 186
Initialization Parameters 187
Application Shutdown 190
Unhandled Exceptions 190
Custom Splash Screens 192
Out-of-Browser Applications 197
Enabling Out-of-Browser Support 199
Installing an Out-of-Browser Application 201
Customizing Icons 203
Tracking Application State 204
Removing and Updating an Application 208
Binary Resources 209
Placing Resources in the Application Assembly 210
Using Subfolders 212
Programmatically Retrieving a Resource 212
Placing Resources in the Application Package 213
Placing Resources on the Web 214
Hyperlinks 247
Pages 247
Navigation Properties 248
State Storage 249
Navigation Methods 249
Navigation Templates 250
The Last Word 251
■Chapter 8: Shapes and Geometries 253
Basic Shapes 253
The Shape Classes 254
Rectangle and Ellipse 255
Sizing and Placing Shapes 257
Sizing Shapes Proportionately with a Viewbox 260
Line 262
Polyline 263
Polygon 264
Line Caps and Line Joins 267
Dashes 269
Paths and Geometries 271
Line, Rectangle, and Ellipse Geometries 272
Combining Shapes with GeometryGroup 273
Curves and Lines with PathGeometry 274
Straight Lines 276
Arcs 276
Bézier Curves 278
The Geometry Mini-Language 280
Clipping with Geometry 282
Exporting Clip Art 284
The Last Word 323
■Chapter 10: Animation 325
Understanding Silverlight Animation 325
The Rules of Animation 327
Creating Simple Animations 328
The Animation Class 328
The Storyboard Class 328
Starting an Animation with an Event Trigger 329
Starting an Animation with Code 330
Configuring Animation Properties 331
From 331
To 333
By 333
Duration 333
Animation Lifetime 334
RepeatBehavior 335
Simultaneous Animations 336
Controlling Playback 337
www.it-ebooks.info
■ CONTENTS
xiv
Animation Easing 339
Using an Easing Function 340
Easing In and Easing Out 341
Easing Function Classes 342
Animation Types Revisited 346
Animating Transforms 346
Playing Multiple Sounds 390
Changing Volume, Balance, and Position 391
Playing Video 395
Client-Side Playlists 396
Server-Side Playlists 396
www.it-ebooks.info
■ CONTENTS
xv
Progressive Downloading and Streaming 397
Adaptive Streaming 399
Advanced Video Playback 400
Video Encoding 400
Encoding in Expression Encoder 401
Markers 403
Adding Markers with Expression Encoder 403
Using Markers in a Silverlight Application 405
VideoBrush 408
Video Effects 409
Deep Zoom 414
Creating a Deep Zoom Image Set 416
Using a Deep Zoom Image Set in Silverlight 419
The Last Word 423
■Chapter 12: Styles and Behaviors 425
Styles 425
Defining a Style 426
Applying a Style 427
Dynamic Styles 428
Style Inheritance 429
Transitioning to a Steady State 466
Custom Transition 466
Understanding Parts with the Slider Control 468
Creating Templates for Custom Controls 472
Planning the FlipPanel Control 473
Creating the Solution 474
Starting the FlipPanel Class 474
Adding the Default Style with Generic.xaml 476
Choosing Parts and States 478
Starting the Default Control Template 480
The FlipButton Control 481
Defining the State Animations 482
Wiring Up the Elements in the Template 484
Using the FlipPanel 486
Using a Different Control Template 487
The Last Word 489
■Chapter 14: Browser Integration 491
Interacting with HTML Elements 492
Getting Browser Information 493
The HTML Window 494
Popup Windows 495
Inspecting the HTML Document 496
Manipulating an HTML Element 498
Inserting and Removing Elements 501
Changing Style Properties 502
Handling JavaScript Events 503
Code Interaction 505
Calling Browser Script from Silverlight 506
Building a Data Object 542
Displaying a Data Object with DataContext 543
Storing a Data Object as a Resource 546
Editing with Two-Way Bindings 547
Validation 548
ValidatesOnException 548
NotifyOnValidationError 551
The Validation Class 552
Change Notification 553
Building a Data Service 554
Calling the Data Service 557
Binding to a Collection of Objects 559
Displaying and Editing Collection Items 559
Inserting and Removing Collection Items 563
Binding to a LINQ Expression 564
Master-Details Display 567
Data Conversion 570
Formatting Strings with a Value Converter 570
Creating Objects with a Value Converter 574
Applying Conditional Formatting 577
Data Templates 579
Separating and Reusing Templates 581
More Advanced Templates 581
Changing Item Layout 583
www.it-ebooks.info
■ CONTENTS
xviii
The Last Word 584
Sorting 619
DataGrid Editing 620
Editing with Templates 620
Validation and Editing Events 621
The PagedCollectionView 624
Sorting 625
Filtering 625
Grouping 626
Paging 628
The TreeView 631
Filling a TreeView 631
A Data-Bound TreeView 632
www.it-ebooks.info
■ CONTENTS
xix
The Last Word 634
■Chapter 18: Isolated Storage 635
Understanding Isolated Storage 635
The Scope of Isolated Storage 636
What to Put in Isolated Storage 636
Using Isolated Storage 637
Opening an Isolated Store 637
File Management 637
Writing and Reading Data 638
Requesting More Space 640
Storing Objects with XmlSerializer 643
Storing Application Settings 646
The File Dialogs 647
Reading Files with OpenFileDialog 648
Processing Different Types of Data 690
XML 690
Services That Return XML Data 691
Navigating Over an XDocument 694
Querying an XDocument with LINQ 696
Services That Require XML Data 698
Services That Return SOAP Data 701
Services That Return JSON Data 701
RSS 704
Sockets 707
Understanding Sockets and TCP 707
The Policy Server 709
The Policy File 710
The PolicyServer Classes 710
The PolicyConnection Classes 713
The Messaging Server 714
The Messenger Client 718
Connecting to the Server 718
Sending Messages 721
Receiving Messages 722
Local Connections 723
Sending a Message 724
Receiving a Message 725
The Last Word 726
■Index 727
www.it-ebooks.info
xxi
www.it-ebooks.info