IN ACTION
Revised Edition of
Silverlight 4 in Action
Pete Brown
MANNING
www.it-ebooks.info
Silverlight 5 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:
©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
1 2 3 4 5 6 7 8 9 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 1
PART 2
CORE SILVERLIGHT ...................................................... 1
1
■
Introducing Silverlight 3
2
■
XAML and the property system
3
■
The application model and the plug-in
4
■
Working with HTML and browsers
5
■
Out-of-browser applications
6
■
The security model and elevated trust 114
26
47
73
95
CREATING THE USER INTERFACE .............................. 125
7
■
Rendering, layout, and transforming
8
■
Panels 160
9
■
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 3
PART 4
PART 5
PART 6
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
1
xxxi
CORE SILVERLIGHT .......................................... 1
Introducing Silverlight 3
1.1
A Silverlight primer
4
Silverlight and the web 5 Silverlight and WPF
Types of Silverlight applications 6
■
1.2
A brief history of Silverlight
6
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
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
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
Object trees 39
2.3
2.4
2.5
3
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
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
5
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
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 2
7
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
8
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
9
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
x
CONTENTS
12.4
ItemsControls
258
The ListBox 259
TabControl 262
12.5
■
The ComboBox 262
■
The
Creating UserControls 265
Defining the appearance 266 Defining behavior of a
control 267 Calling the control 270
■
■
12.6
13
Summary 271
Animation and behaviors 272
13.1
13.2
Animating a value over time 273
Mastering the timeline 275
What type of property are you animating? 275 Where are you
starting from and where are you going? 277 How long should
the animation run? 280
■
■
13.3
Storyboards 283
Understanding the storyboard 283 Storyboard target
Controlling the Storyboard 287 Resources 289
■
284
■
13.4
13.5
Keyframing 291
Interpolation 293
Linear interpolation 294 Spline interpolation
Discrete interpolation 296 KeyTime 296
■
294
■
13.6
Easing functions 297
Using easing functions 298
function 301
13.7
14
Creating a custom easing
Behaviors, triggers, and actions
Using existing behaviors
behavior 305
13.8
■
303
■
302
Creating your own
Summary 306
Resources, styles, and control templates 307
14.1
Being resourceful 308
Declarative resources 308
Bundled resources 315
14.2
■
Accessing loose resources 314
Giving your elements style 317
Defining the look 319
Implicit style definitions
■
Explicitly keyed style definitions 321
322
www.it-ebooks.info
xi
CONTENTS
14.3
14.4
Creating templates 323
Building a control template
templates 328
323
Dealing with visual states
329
■
Creating reusable
Understanding the components 329
VisualStateManager 331
14.5
14.6
15
Sharing your visual states
Summary 335
■
Leveraging the
335
Extensions, converters, custom controls, and panels 337
15.1
Markup extensions 338
Creating a simple custom markup extension
parameterized markup extension 340
15.2
■
Creating a
Custom type converters 342
Creating the converter
15.3
339
342
■
Creating a custom panel
Using the converter
343
345
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 DATA AND SERVICES ............. 365
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 385
■
■
■
■
www.it-ebooks.info
xii
CONTENTS
16.3
Binding to dynamic properties
386
ICustomTypeProvider overview 387
classes 387 Using the helper class
■
16.4
Customizing the display
■
Creating the helper
390
395
Formatting values 395 Converting values during
binding 395 Providing default fallback values 399
Handling null values 399
■
■
16.5
Creating explicit data templates 400
Using a DataTemplate with a ContentControl 400
Rendering an ItemsControl with a DataTemplate 402
16.6
16.7
17
Creating implicit data templates 403
Summary 409
Data controls: DataGrid and DataForm 410
17.1
The DataGrid 411
Displaying your data 411
Sorting items 417
17.2
■
Editing grid data
417
The DataForm 418
Displaying your data 419 Binding to lists of data 421
Customizing display 424 Customizing edit, add, and display
templates 425 Finer control over editing and committing
data 427
■
■
■
17.3
Annotating for display 428
The Display attribute 429
17.4
18
The Editable attribute 430
Summary 431
Input validation
18.1
18.2
■
433
The validation example source and UI 434
Exception-based property validation 438
Handling exception validation errors 438 Custom
validation code 439 Validation error display 440
■
■
18.3
Synchronous validation with IDataErrorInfo
441
The IDataErrorInfo interface 441 Simple validation with
IDataErrorInfo 442 Cross-field validation with IDataErrorInfo 443 Combining exceptions and IDataErrorInfo 445
■
■
■
18.4
Asynchronous validation with INotifyDataErrorInfo
The INotifyDataErrorInfo interface 446
interface 447 Binding support 448
■
www.it-ebooks.info
■
■
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 functions 454 Creating custom
validators 456
■
■
18.6
18.7
19
Comparison of validation approaches
Summary 458
457
Networking basics 460
19.1
The web request/response pattern
WebRequest and HttpWebRequest
HttpWebResponse 463
19.2
462
WebResponse and
Simplifying the request/response pattern with WebClient 465
String operations 465
19.3
461
■
■
Stream operations
Asynchronous communication
468
469
When async methods attack 470 Saving your sanity
with Rx 471 Simplifying with tasks 474
■
■
19.4
Trust and cross-domain network access 476
Structuring the cross-domain file 477 Other cross-domain
policy files 479 Locating your cross-domain policy 480
■
■
19.5
The browser HTTP stack 481
Connection count limit
19.6
481
■
Cookies 482
■
Caching
482
The client HTTP stack 483
Manually creating the client stack 483 Automatically using
the client stack 484 Automatically setting the HTTP Referer
and other headers 484 Authentication credentials 485
Managing cookies with the CookieContainer 486 When to
use the client stack 488
■
■
■
■
19.7
19.8
20
Checking the network state 489
Summary 490
Working with SOAP services 491
20.1
Introducing ASP.NET SOAP services using ASP.NET
Silverlight-compatible SOAP services 492 Service
references 495 Receiving data with the proxy 495
Sending data using the proxy 499
■
■
www.it-ebooks.info
492
xiv
CONTENTS
20.2
Using WCF services and complex data types
500
Creating the Silverlight-enabled WCF service 501 Sharing
type definitions 505 Adding the service reference 508
Using the service 509
■
■
20.3
20.4
Using the configuration file 511
Error handling with WCF 513
Using an out parameter 513 Exposing exception information
for debugging 515 Error handling with WCF SOAP
faults 516
■
■
20.5
21
Summary 518
RESTful services with the ASP.NET Web API 520
21.1
Creating a RESTful service using the ASP.NET Web API
Solution setup 523 Creating the services 525 Testing the
service using the browser 530 Adding the Silverlight
project 531
■
■
■
21.2
Consuming REST services
REST service GET operations
POSTing to the service 544
DELETE 546
21.3
22
539
540 Updating resources by
Removing resources using
■
■
Summary 548
Working with XML, JSON, RSS, and Atom 549
22.1
Parsing plain old XML
LINQ to XML
22.2
550
■
550
XmlSerializer 554
Working with JSON 558
JsonObject and JsonArray
22.3
559
■
Working with RSS and Atom
Reading syndication feeds 566
items 569
22.4
23
DataContractJsonSerializer
566
■
Working with feed
Summary 573
Duplex, sockets, and local connections 575
23.1
WCF polling duplex services
576
Creating the project and callback contract 577 Creating the
service 579 Creating the service logic 582 Managing
client subscriptions 584 Using the duplex service 585
■
■
■
■
www.it-ebooks.info
562
522
xv
CONTENTS
23.2
Connecting to sockets 589
Serving the policy file 590 Opening the connection 591
Handling the response 591
■
23.3
Multicast sockets 593
Any-Source Multicast/Internet Standard Multicast 593
Source-Specific Multicast 594
23.4
Connecting to other local Silverlight applications
595
Creating the receiver 596 Creating the sender 597
Putting it all together 597
■
23.5
PART 4
24
Summary 599
2D AND 3D GRAPHICS .................................. 601
Graphics and effects 603
24.1
Shapes
604
Lines 605 Rectangle 605
Polyline 607 Polygon 608
■
■
Ellipse 606
■
24.2
Geometry 609
Simple geometries 609 Path geometries
Composite geometries 612
■
24.3
Brushes
611
613
SolidColorBrush 613 LinearGradientBrush 614
RadialGradientBrush 616 ImageBrush 618
VideoBrush 619
■
■
24.4
Effects
620
Using built-in effects
24.5
25
620
■
Creating custom pixel shaders
Summary 629
Working with images 630
25.1
25.2
Basic imaging 631
Creating images at runtime
632
Creating from existing images 633 Creating from UI
elements 634 A Mandelbrot fractal generator 636
■
■
25.3
Deep Zoom 638
Showing an image 639 Zooming in and out 639
Managing the viewport 641 Deploying multiscale
images 643
■
■
www.it-ebooks.info
624
xvi
CONTENTS
25.4
Dealing with dead space
643
Filling the space 644 Uniform sizing 645
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 templates 653
Detecting capabilities with the GraphicsDeviceManager
Using the DrawingSurface 658
Project structure: the scene and objects 659
The scene 660 Renderable scene objects 663
The camera 665
■
26.6
Vertices
667
Building a triangle using vertices
the scene 670
26.7
668
■
Adding the triangle to
Primitives 671
The TriangleList primitive 672 The TriangleStrip
primitive 673 Tessellating a sphere 674 Rendering the
sphere with primitives 676
■
■
26.8
27
■
Summary 678
3D lighting, texturing, and animation 679
27.1
Lighting and normal vectors
Lighting the scene 681
Indexed vertices 684
27.2
■
680
Sphere normal vectors
682
Applying a texture 687
The ContentManager 687 Texturing the sphere 688
Adding and texturing a background 691
■
27.3
Coordinate spaces and matrices
695
The three coordinate space conversion matrices 696
The Matrix class 697
27.4
Keyframe animation 698
The KeyFrame and KeyframeAnimation classes 698
Using animation 703
27.5
Summary 706
www.it-ebooks.info
654
xvii
CONTENTS
PART 5
28
MAKING THE MOST OF THE PLATFORM 709
Pop-ups, windows, and full-screen applications 711
28.1
Showing pop-ups and child windows
712
The Popup control 713 Displaying a dialog box with the
ChildWindow control 713
■
28.2
28.3
Creating native windows
717
Creating a normal window
chrome 723
718
Running in full screen
730
Normal full-screen mode 731
mode 732
28.4
29
Customizing window
■
■
Elevated trust full-screen
Summary 733
Navigation 734
29.1
Browser navigation background 735
Browser journals 735
Back and forth 737
29.2
■
Anchor hashtags
736
The Navigation Application template 738
Creating a navigation application 738 Adding a new
page 740 Changing the application theme 742
■
■
29.3
Navigating to pages
744
The Page class 744 The NavigationService class
Frames and URIs 748 Caching pages 751
Navigating to pages in other assemblies 753
■
746
■
29.4
Navigation out of the browser 755
Providing custom navigation controls 756
29.5
30
Summary 759
Working with files and directories 761
30.1
Using the file open and save dialogs
Working with the OpenFileDialog
SaveFileDialog 767
30.2
Working with directories
763
762
■
Saving files with the
770
Getting directory timestamps 772 Checking for directory
existence 774 Getting the directory root 774 Creating
and deleting directories 775 Listing directory contents 777
Accessing special folders 778
■
■
■
■
www.it-ebooks.info
xviii
CONTENTS
30.3
Working with individual files
780
Creating a file 784 Writing to a file 785 Reading from a
file 787 Getting and setting file metadata 789
File utility functions 790
■
■
■
30.4
Storing data in isolated storage
791
IsolatedStorageFile: the virtual filesystem 792
writing files: the isolated storage way 795
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 content to fit 814
■
■
31.3
Multipage printing dedicated trees 816
Prerequisites 816 Printing line items 820 Adding
multipage support 825 Adding a header and footer 827
■
■
■
31.4
32
Summary 830
COM, Native Extensions, and p-invoke 832
32.1
COM automation 833
Detecting COM automation availability 834 Using COM
automation to make Silverlight talk 835 Accessing GPS data
using COM automation 836 Automating Excel 837
■
■
■
32.2
Native Extensions for Silverlight 839
Accessing an accelerometer 840 Integrating with the
Windows taskbar 849 Runtime automation server
installation 855
■
■
32.3
P-invoke for API calls 857
Setting up the printer application 857
interface 860
32.4
Summary 865
www.it-ebooks.info
■
The Win32 API
xix
CONTENTS
PART 6
33
BEST PRACTICES .......................................... 867
Structuring and testing with the MVVM pattern 869
33.1
Project setup and traditional code-behind approach
Project and service setup 871
solution 875
33.2
■
Model-View-ViewModel basics
A typical code-behind
878
Myths about MVVM Model-View-ViewModel 881
simple: a basic ViewModel implementation 882
33.3
Factoring out reusable code
Business rules and logic
calls 890
33.4
888
870
■
Keep it
887
■
Better separation from the UI
Data access and service
892
Using commands 892 Using the CallMethodAction
behavior 896 View-specific entities and ViewModels 898
Interfaces, IoC, and ViewModel locators 903
■
■
33.5
Testing MVVM applications 906
Introduction to the Silverlight Unit Testing Framework
Testing the ViewModel 910 Testing asynchronous
operations 911
906
■
33.6
34
Summary 912
Debugging your application 913
34.1
Debugging basics
914
Using the Debug class 914 IDE breakpoints 916
The good old MessageBox 918
■
34.2
Binding debugging 920
Viewing 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
www.it-ebooks.info
924
xx
CONTENTS
35
The install experience and preloaders 929
35.1
Handling the ‘Silverlight not installed’ scenarios
Creating your own install experience
35.2
Using a custom preloader
930
931
933
Creating the appearance 934 Integrating the custom splash
screen 936 Monitoring the load progress 936
■
■
35.3
Summary 937
appendix A Database, connection, and data model setup
index 945
939
online appendixes
appendix B
appendix C
appendix D
appendix E
appendix F
Media basics
Raw media, webcam, and microphone
Introduction to WCF RIA Services
WCF RIA endpoints, security, business logic, and decoupling
WCF RIA Services and WVVM
Online appendixes available as a free download from
www.manning.com/Silverlight5inAction
www.it-ebooks.info
preface
Coding on the client is fun. I started on the Commodore 64 in seventh grade in the
1980s; later moved to DOS with dBASE, QuickBasic, and C++; and eventually began Windows programming using C++, Borland Delphi 1.0, PowerBuilder, Visual Basic 3-6, and
.NET. I like the ozone smell of making my CPU work for a living. I like being able to tap
into the power of the local machine. I want to be able to hear the individual bits moving
across the bus.
I like client application development, and I really like XAML. I like Silverlight, WPF,
and Windows 8 XAML. I even like working in the WPF subset on the .NET Micro
Framework and Gadgeteer boards I own. Sometimes, when I’m feeling especially dangerous I’ll write some C++, or C, or even a little assembly. It’s all about the power.
A year and a half ago, Manning published Silverlight 4 in Action. As proud as I am of
that book, I’m even more excited to bring you this updated version. Not only because
of the work involved in bringing you this edition, but because of how far Silverlight
has come in that time. With Silverlight 5, Silverlight has the power.1
What a difference a year and a half makes! Silverlight 4 saw significant uptake
among business application developers, and those same developers helped drive the
features that made it into Silverlight 5. These developers are writing the types of
bread-and-butter applications that leverage Silverlight for its simple deployment and
great validation and data binding capabilities.
1
And now, you have C&C Music Factory’s “I Got the Power” looping in your head. You can thank me later.
xxi
www.it-ebooks.info
xxii
PREFACE
Figure 1
Adoption of Adobe Flash vs. Microsoft Silverlight in 2012
General adoption of Silverlight is up too, as you can see in figure 1. These unofficial
third-party charts, reformatted for print from data obtained in
early 2012, show Silverlight and Adobe Flash adoption taken from a large segment of
the general internet population. They reveal overall Silverlight adoption at a hair over
75 percent with version 4 at a bit over 67 percent and overall flash adoption around 96
percent, with Flash 11 at a hair over 70 percent.
I’m not a marketing person; I’m a developer. Although such a blunt numbers comparison between the two competing products may look dire at first glance, adoption
numbers like these aren’t at all bad, considering Silverlight was a far more recent
introduction and didn’t benefit from either the late 1990s boom in website intro apps
or bundling with Internet Explorer 6. You can do a lot with 75 percent of the PCs out
there. It’s my goal to help you do it.
2011 was also a rocky year for Silverlight and Flash developers, with confusion over
what Silverlight is to be used for and where it fits into the development strategy at
Microsoft and in the web as a whole. The Apple position on Flash didn’t help Flash or
plug-ins in general. Flash definitely had a reputation for websites, whereas Silverlight
evolved to more of a browser-delivered client technology. There were as many, if not
more, out-of-browser apps as in-browser apps written in Silverlight.
With HTML5 (and JavaScript and CSS3, all of which I’ll lump into “HTML5” just as
the media tends to) having picked up serious speed, especially on the media and
casual games front on the public web, I think it’s a sure bet to become the way forward
for broad-reach, public-facing website content. There’s so much momentum behind
HTML5 that to fight it would simply land you behind others. HTML5 will suit you just
fine, especially for run-of-the-mill, non-digital rights management (DRM), nonsmooth-streaming media like what you see on YouTube.
But for many, HTML5 isn’t a realistic choice. Either you can’t count on browser
support for key features, or you have a behind-the-firewall (or other controlled network) application scenario that makes it easier for you to use technologies you’ll be
www.it-ebooks.info
PREFACE
xxiii
more productive in. Two key points rise above any trendy discourse and well into the
realm of GSD (Getting Stuff Done):
■
■
Know your audience —Develop in what your users can use (behind the firewall
has more choice and control than the public web).
Know your skills and requirements —Develop in what you can be most productive
in—what will meet the project requirements with the least amount of fudging.
Without a doubt, most .NET developers are more productive in Silverlight when building business applications. There’s so much there, right in the box, that makes it easy
for you to quickly create stunning, feature-rich apps. Not only that, but the added system integration features of Silverlight 5 (like COM and p-invoke) make it even more
compelling as a platform.
As a developer, or manager of developers, you must choose technology based not
only on the longevity of that technology, but also on what makes it possible for you to
deliver the best possible application for the most reasonable cost.
Regardless of where Silverlight goes in the long term, you already know that XAML,
C#, and Visual Basic are all here to stay. In addition to continued desktop support for
Silverlight and WPF, Microsoft is using XAML in Windows 8 Metro and on the Windows
Phone. I believe in XAML strongly enough to have written this book (around 1,200
pages if you include the downloadable content) while working at Microsoft, as well as
a book dedicated to XAML on Windows 8. Given the resources dedicated to XAML
development and tools at Microsoft, I’m glad to see they believe in it too.
Ultimately, it’s good to have a choice. I choose XAML. I choose Silverlight.
www.it-ebooks.info
acknowledgments
A book of this size and complexity takes a large number of people to write (and to
lift). Though my name is on the cover, there’s no way I could’ve completed this without the support and hard work of many others. I’d like to thank:
■
■
■
■
■
Chad Campbell and John Stockton for creating such an excellent first edition.
Without their hard work covering Silverlight 2, I’d never have thought to create
a Silverlight 4 edition or this Silverlight 5 edition.
The Silverlight product team for their continued help in digging into the
details of the runtime. There were too many helpful people to list here.
My stalwart tech reviewer Tom McKearney. I appreciate most that he didn’t just
die on the spot when he was told the 800 pages he had to review were actually in
the 1,200 neighborhood.
I’d like to thank my friends at Manning. People like Mary Piergies, Liz Welch,
Elizabeth Martin, and others worked tirelessly to get this book published in
time. Michael Stephens, Maureen Spencer, and of course, Marjan Bace all
worked with me to help ensure that you, the reader, get all the text I wrote,
without them having to print a multivolume set. I thank them and the rest of
the folks at Manning.
Unique in this thanks is my development editor, Jeff Bleiel. A good editor can
make the difference between a horrible authoring experience and a good one,
and for the second time, Jeff definitely made that difference. He was my interface with Manning and my continued mentor as an author. Jeff made a positive
contribution to this book and to my writing in general.
xxiv
www.it-ebooks.info