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:
©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
CONTENTS
x
12.4 ItemsControls 258
The ListBox 259
■
The ComboBox 262
■
The
TabControl 262
12.5 Creating UserControls 265
Defining the appearance 266
■
Defining behavior of a
control 267
■
Calling the control 270
12.6 Summary 271
13
Animation and behaviors 272
13.1 Animating a value over time 273
13.2 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 284
Controlling the Storyboard 287
■
Resources 289
13.4 Keyframing 291
13.5 Interpolation 293
Linear interpolation 294
■
Spline interpolation 294
Discrete interpolation 296
■
KeyTime 296
13.6 Easing functions 297
Using easing functions 298
■
Creating a custom easing
function 301
13.7 Behaviors, triggers, and actions 302
Using existing behaviors 303
■
Creating your own
behavior 305
13.8 Summary 306
14
Resources, styles, and control templates 307
14.1 Being resourceful 308
Declarative resources 308
■
Accessing loose resources 314
Bundled resources 315
14.2 Giving your elements style 317
Defining the look 319
■
Explicitly keyed style definitions 321
Implicit style definitions 322
CONTENTS
xi
14.3 Creating templates 323
Building a control template 323
■
Creating reusable
templates 328
14.4 Dealing with visual states 329
Understanding the components 329
■
Leveraging the
VisualStateManager 331
14.5 Sharing your visual states 335
14.6 Summary 335
15
Extensions, converters, custom controls, and panels 337
15.1 Markup extensions 338
Creating a simple custom markup extension 339
■
Creating a
parameterized markup extension 340
15.2 Custom type converters 342
Creating the converter 342
■
Using the converter 343
15.3 Creating a custom panel 345
Project setup 346
■
The OrbitPanel class 346
Properties 347
■
Custom layout 350
■
Enhancements 354
15.4 Creating a custom control 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 Summary 363
PART 3 WORKING WITH DATA AND SERVICES 365
16
Binding 367
16.1 Binding basics 368
Mastering the binding syntax 369
■
Choosing a binding
mode 371
16.2 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
CONTENTS
xii
16.3 Binding to dynamic properties 386
ICustomTypeProvider overview 387
■
Creating the helper
classes 387
■
Using the helper class 390
16.4 Customizing the display 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 Creating implicit data templates 403
16.7 Summary 409
17
Data controls: DataGrid and DataForm 410
17.1 The DataGrid 411
Displaying your data 411
■
Editing grid data 417
Sorting items 417
17.2 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
■
The Editable attribute 430
17.4 Summary 431
18
Input validation 433
18.1 The validation example source and UI 434
18.2 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 IData-
ErrorInfo 443
■
Combining exceptions and IDataErrorInfo 445
18.4 Asynchronous validation with INotifyDataErrorInfo 446
The INotifyDataErrorInfo interface 446
■
Implementing the
interface 447
■
Binding support 448
■
Building the WCF
CONTENTS
xiii
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 Comparison of validation approaches 457
18.7 Summary 458
19
Networking basics 460
19.1 The web request/response pattern 461
WebRequest and HttpWebRequest 462
■
WebResponse and
HttpWebResponse 463
19.2 Simplifying the request/response pattern with WebClient 465
String operations 465
■
Stream operations 468
19.3 Asynchronous communication 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 481
■
Cookies 482
■
Caching 482
19.6 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 Checking the network state 489
19.8 Summary 490
20
Working with SOAP services 491
20.1 Introducing ASP.NET SOAP services using ASP.NET 492
Silverlight-compatible SOAP services 492
■
Service
references 495
■
Receiving data with the proxy 495
Sending data using the proxy 499
CONTENTS
xiv
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 Using the configuration file 511
20.4 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 Summary 518
21
RESTful services with the ASP.NET Web API 520
21.1 Creating a RESTful service using the ASP.NET Web API 522
Solution setup 523
■
Creating the services 525
■
Testing the
service using the browser 530
■
Adding the Silverlight
project 531
21.2 Consuming REST services 539
REST service GET operations 540
■
Updating resources by
POSTing to the service 544
■
Removing resources using
DELETE 546
21.3 Summary 548
22
Working with XML, JSON, RSS, and Atom 549
22.1 Parsing plain old XML 550
LINQ to XML 550
■
XmlSerializer 554
22.2 Working with JSON 558
JsonObject and JsonArray 559
■
DataContractJsonSerializer 562
22.3 Working with RSS and Atom 566
Reading syndication feeds 566
■
Working with feed
items 569
22.4 Summary 573
23
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
CONTENTS
xv
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 Summary 599
PART 4 2D AND 3D GRAPHICS 601
24
Graphics and effects 603
24.1 Shapes 604
Lines 605
■
Rectangle 605
■
Ellipse 606
Polyline 607
■
Polygon 608
24.2 Geometry 609
Simple geometries 609
■
Path geometries 611
Composite geometries 612
24.3 Brushes 613
SolidColorBrush 613
■
LinearGradientBrush 614
RadialGradientBrush 616
■
ImageBrush 618
VideoBrush 619
24.4 Effects 620
Using built-in effects 620
■
Creating custom pixel shaders 624
24.5 Summary 629
25
Working with images 630
25.1 Basic imaging 631
25.2 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
CONTENTS
xvi
25.4 Dealing with dead space 643
Filling the space 644
■
Uniform sizing 645
■
Fill the
area 646
■
UniformToFill 647
25.5 Summary 647
26
Introduction to 3D 649
26.1 3D—a natural way of interacting with information 650
26.2 The Silverlight/XNA 3D API 652
Rendering pipeline 652
■
Project templates 653
26.3 Detecting capabilities with the GraphicsDeviceManager 654
26.4 Using the DrawingSurface 658
26.5 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 668
■
Adding the triangle to
the scene 670
26.7 Primitives 671
The TriangleList primitive 672
■
The TriangleStrip
primitive 673
■
Tessellating a sphere 674
■
Rendering the
sphere with primitives 676
26.8 Summary 678
27
3D lighting, texturing, and animation 679
27.1 Lighting and normal vectors 680
Lighting the scene 681
■
Sphere normal vectors 682
Indexed vertices 684
27.2 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
CONTENTS
xvii
PART 5 MAKING THE MOST OF THE PLATFORM 709
28
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 Creating native windows 717
Creating a normal window 718
■
Customizing window
chrome 723
28.3 Running in full screen 730
Normal full-screen mode 731
■
Elevated trust full-screen
mode 732
28.4 Summary 733
29
Navigation 734
29.1 Browser navigation background 735
Browser journals 735
■
Anchor hashtags 736
Back and forth 737
29.2 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 746
Frames and URIs 748
■
Caching pages 751
Navigating to pages in other assemblies 753
29.4 Navigation out of the browser 755
Providing custom navigation controls 756
29.5 Summary 759
30
Working with files and directories 761
30.1 Using the file open and save dialogs 762
Working with the OpenFileDialog 763
■
Saving files with the
SaveFileDialog 767
30.2 Working with directories 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
CONTENTS
xviii
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
■
Reading and
writing files: the isolated storage way 795
30.5 Summary 797
31
Printing 798
31.1 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 Summary 830
32
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
■
The Win32 API
interface 860
32.4 Summary 865
CONTENTS
xix
PART 6 BEST PRACTICES 867
33
Structuring and testing with the MVVM pattern 869
33.1 Project setup and traditional code-behind approach 870
Project and service setup 871
■
A typical code-behind
solution 875
33.2 Model-View-ViewModel basics 878
Myths about MVVM Model-View-ViewModel 881
■
Keep it
simple: a basic ViewModel implementation 882
33.3 Factoring out reusable code 887
Business rules and logic 888
■
Data access and service
calls 890
33.4 Better separation from the UI 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 906
Testing the ViewModel 910
■
Testing asynchronous
operations 911
33.6 Summary 912
34
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 924
Inspecting individual requests 926
34.4 Summary 928
CONTENTS
xx
35
The install experience and preloaders 929
35.1 Handling the ‘Silverlight not installed’ scenarios 930
Creating your own install experience 931
35.2 Using a custom preloader 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 939
index 945
online appendixes
appendix B Media basics
appendix C Raw media, webcam, and microphone
appendix D Introduction to WCF RIA Services
appendix E WCF RIA endpoints, security, business logic, and decoupling
appendix F WCF RIA Services and WVVM
Online appendixes available as a free download from
www.manning.com/Silverlight5inAction
xxi
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 Win-
dows 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 dan-
gerous 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.
PREFACE
xxii
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 com-
parison 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), non-
smooth-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 net-
work) application scenario that makes it easier for you to use technologies you’ll be
Figure 1 Adoption of Adobe Flash vs. Microsoft Silverlight in 2012
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 build-
ing 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 sys-
tem 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.
xxiv
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 with-
out 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 inter-
face with Manning and my continued mentor as an author. Jeff made a positive
contribution to this book and to my writing in general.