HAPPY BOOKSGIVING
Use code BOOKSGIVING during checkout to save 40%-55% on books and eBooks. Shop now.
Register your product to gain access to bonus material or receive a coupon.
This eBook includes the following formats, accessible from your Account page after purchase:
EPUB The open industry format known for its reflowable content and usability on supported mobile devices.
PDF The popular standard, used most often with the free Acrobat® Reader® software.
This eBook requires no passwords or activation to read. We customize your eBook by discreetly watermarking it with your name, making it uniquely yours.
Printed entirely in color, with helpful figures and syntax coloring to make code samples appear as they do in Visual Studio and Expression Blend!
Using Silverlight 4, you can build richer and more powerful applications than ever before, and deploy them seamlessly in browsers, on desktop computers, and even on Windows Phone 7. In Silverlight 4 Unleashed, Microsoft Silverlight MVP Laurent Bugnion covers everything you need to do all this, and much more.
The only full-color, example-rich guide to Silverlight 4 for experienced Microsoft developers, Silverlight 4 Unleashed illuminates the entire development process: from installing Silverlight tools to building great user experiences, managing data to optimizing application performance.
You’ll master core Silverlight features such as controls and properties; communicating with web services; efficiently creating data-oriented line-of-business applications; working with external frameworks, and more. Bugnion concludes by showing how to use advanced features such as 3D-like effects and multitouch to deliver an even smoother, richer experience.
Foreword xxiii
Introduction 1
1 Three Years of Silverlight 7
Discovering Silverlight 4 7
Learning Silverlight Is Betting on the Future 8
How Can They Be So Fast? 9
How About Compatibility with Older Versions? 9
Cross-Browser and Cross-Platform Compatibility 10
Windows and Macintosh 11
Linux 12
Alternatives to Silverlight 12
In the Web Browser 12
Out of the Browser 13
Legacy Technologies 14
A Short History of Silverlight 15
Silverlight 1.0 16
Silverlight 2 16
Silverlight 3 16
And Silverlight 4 17
Previewing the Future of Silverlight 17
Installing Silverlight 4 as a User 18
Opening a Silverlight 4 Application 19
What to Do If Silverlight Is Not Installed? 19
Exploring Silverlight 4 Demos 19
Deep Zooming the Matterhorn 19
Getting Involved Socially with Sobees 20
Navigating with Bing Maps Streetside and PhotoSynth 21
Visualizing Information with the Pivot Viewer 23
Drawing on the Web with Fantasia 25
How Can You Get Involved? 25
Summary 26
2 Setting Up and Discovering Your Environment 27
Installing Visual Studio 27
Visual Studio 2010 27
Visual Web Developer Express 28
Installing the Silverlight Tools for Visual Studio 28
Verifying the Installation 29
Inspecting the Application 31
Unpacking an XAP File 32
Using the Visual Studio Designer 32
Implementing Hello Silverlight 33
Checking the Properties Editor 34
Adding Some Text 36
Using Design Time Width and Height 39
Saving the Application 40
Installing Expression Blend 40
Creating a New Silverlight Application 40
Opening Hello Silverlight 41
Adding an Effect 42
Creating a Pulse Animation 44
Triggering the Storyboard 45
Testing the Application 46
Summary 47
3 Extending Your Application with Controls 49
Extending XAML 49
Mapping a Prefix to a CLR Namespace 49
Why Is a Prefix Not Always Needed? 52
Adding a Namespace to Any Element 52
Defining Your Own URI and Mapping CLR Namespaces 52
What’s a Control? 53
User Controls 53
Custom Controls 63
Summary 73
4 Investigating Existing Controls 75
Reviewing the Basics 75
Show Me Some Code! 76
Changes in Existing Controls 77
Mouse Wheel Support 77
Localizing for Right-to-Left Languages 77
Getting a Control Template’s Current State 78
Adding SelectedValue and SelectedValuePath 79
Adding Command and CommandParameter 79
Presenting and Editing Text with the RichTextBox 84
Zooming with the Viewbox 85
Opening a ChildWindow 87
Finding More Information 90
Where to Find Additional Controls? 90
Do You Really Need a Control? 90
The Silverlight Toolkit 91
Installing the Silverlight Toolkit 92
Third-Party Providers 101
Summary 102
5 Understanding Dependency Properties 103
Inheriting DependencyObject 104
Threading 104
Accessing a Dependency Property’s Value 105
Using a DependencyObject as Data Item 105
Registering Dependency Properties 108
Defining Metadata 109
Initializing Dependency Objects 111
Understanding Attached Properties 114
Using Attached Properties for Values 114
Registering an Attached Property 115
Using Custom Attached Properties in XAML 118
Implementing an Attached Behavior 118
Building on Attached Behaviors with Blend Behaviors 122
Adding a New Property with Snippets 123
Installing the Snippets for Silverlight 123
Using the Snippets 124
Calculating a Dependency Property’s Value 124
Getting the Property’s Base Value 126
Reading the Local Value 126
Summary 127
6 Working with Data: Binding, Grouping, Sorting, and Filtering 129
Diving into Data Bindings 130
Understanding a Binding’s Elements 130
Understanding the Namescope 132
Setting the Source 133
Refining the Path 136
Flowing in Two Directions 138
Converting the Values 138
Changing the Format 139
Handling Special Cases 141
Property Trigger 142
Validating Input 142
Using the Visual Studio Binding Dialog 146
Using the Expression Blend Binding Dialog 148
Debugging Data Bindings 149
Checking the Output Tab 149
Creating a Test Converter 150
Grouping, Filtering, and Sorting Data 151
Working with the CollectionViewSource 151
Using a PagedCollectionView 154
Binding Directly to the Source 154
Summary 155
7 Understanding the Model-View-ViewModel Pattern 157
About Design Patterns 157
Separating the Concerns 158
Why Is Separation Good? 158
Classic Separation Patterns 158
History of MVVM 159
Developing Expression Blend 160
Presentation Model for WPF and Silverlight 160
Architecture of MVVM 160
Translating to Silverlight 161
Two Kinds of View-models 162
Binding the View to the View-model 163
Understanding the Data Context 163
Inheriting the Data Context 164
Binding to the View 164
Building a Sample Application 168
The Model’s Interface 168
Building a CustomerViewModel 170
Calling the Service in the MainViewModel 172
Binding to Results 174
Testing the Application 177
Bridging the Separation 178
Implementing a ViewModelBase Class 178
Using Commands 180
Sending Messages 183
Using an MVVM Framework 184
What Could Be Better? 184
Summary 184
8 Using Data Controls 187
Filtering and Paging with the PagedCollectionView 188
Preparing the Sample 188
Building the PagedCollectionView 190
Filtering Data 192
Paging Through Data 194
Optimizing Data Handling 195
Implementing Custom Sorting 195
Adding a DataPager Control 196
Customizing the Display 197
Validating Data Input 197
Using Interface-Based Validation 198
Validating with Data Annotations 201
Validating Before or After the Data Is Set 203
Validating on the Client and on the Server 203
Reviewing the DataGrid 204
Using the DataGrid with Automatic Columns 204
Choosing Between DataGrid and ListBox 208
Editing Data in the DataForm 208
Adding a Description 210
Validating the Input 211
Committing Changes Manually 211
Defining Fields Manually 212
Getting More Information 213
Making a Simple Property Editor 213
Summary 214
9 Connecting to the Web 215
Getting Information from Cross-Domain Servers 216
Checking Whether a Policy File Exists 216
Working Around Cross-Domain Restrictions 217
Placing Simple Calls 218
Informing the User 218
Learning with a Sample 219
Downloading Strings 219
Detecting Errors, Checking the Result 223
Opening a Resource for Reading 224
Uploading a String 225
Opening a Resource for Writing 227
Accessing Headers 231
Sending Complex Messages 231
Posting a File to the Server with HttpWebRequest 231
Discovering the New Networking Stack 234
Using the Client HTTP Stack 235
Using Other HTTP Methods 236
Using the CookieContainer 236
Handling Responses 237
Handling XML Responses 238
Handling JSON Responses 238
Communicating with WCF 239
Setting Up a Service 239
Connecting the Client Application 241
Updating the Code on the Server 244
Publishing the Service 244
Summary 245
10 Creating Resources, Styles, and Templates 247
Working with Resources in XAML 248
Using Local Resources 248
Merging Dictionaries 249
From the Same Assembly 250
From a Different Assembly 251
Resolving Resources 254
Working with Resources in Blend 256
Merging a Resource Dictionary 256
Creating New Resources 257
Selecting a Resource for a Property 257
Using the Resources Panel 257
Cleaning Up Unused Resources 259
Using the Pistachio Tool 259
Styling a Control 260
Using Implicit Styles 261
Creating a Hierarchy of Styles 263
Creating a New Style in Blend 264
Templating a Control 265
Copying a Template in Blend 265
Creating a Custom Easing Function 269
Making a Control in Blend 270
Applying a Theme 271
Summary 272
11 Mastering Expression Blend 273
What Is Blend, Exactly? 274
Working as a Tool for Integrators 274
Editing XAML Markup 274
When Should You Use User Controls? 274
Making an Application Blend 275
Why Is Some Code Not Executed? 275
Why Does Some Code Fail? 275
Detecting the Cause of an Exception 276
Isolating Code in Design Mode 278
Creating Design Time Data in Blend 280
Understanding the Design-Time Data Context 288
Using Blend Behaviors 289
Behavior, Trigger, or Action? 292
Adding a Blend Behavior in Code 293
Creating a New Blend Behavior 293
Finding More Information 300
Summary 300
12 Sketching User Experience 301
Sketching as a Discovery Process 301
Using Sketching and Wireframing Tools 302
Other Kinds of Sketching 303
Discovering SketchFlow 304
Creating a New SketchFlow Application 305
Checking the Panels 305
Creating and Connecting Screens 305
Building the UI 308
Creating a Component Screen 309
Using Sketch Controls 310
Exploring the Sketch Controls 313
Creating States and Transitions 318
Building an Animation 321
Deploying the SketchFlow Application 323
Running the Prototype 323
Giving Feedback 324
Importing and Managing User Feedback 325
Importing and Exporting 325
Importing from Photoshop and Illustrator 326
Importing from PowerPoint 326
Exporting to Word 326
Integrating and Collaborating 327
Integrating into SharePoint 327
Integrating into Team Foundation Server 327
Summary 328
13 Creating Line-of-Business Applications 329
Preparing the Server-Side 331
Prerequisites 332
Preparing the Server-Side Application 332
Creating the Silverlight Client 333
Bringing the Client and the Server Together 334
Adding a Domain Service 334
Inspecting the Domain Service Class 335
Inspecting the Metadata 336
Creating a New Server-Side Query 336
Working with the Visual Designer 338
Understanding the DomainDataSource 339
Calling a Query with Parameter 339
Sorting the Data 341
Adding a Pager 341
Refactoring the Application to MVVM 342
Adding a View-model 342
Adapting the XAML Markup 345
Customizing the Columns 346
Localizing the User Interface 347
Adding a RelayCommand Class 348
Executing the CRUD Operations in Code 348
Displaying Messages from the View-model 351
Deleting an Order 354
Validating the Values 357
Filtering the Data 360
Showing Feedback While Processing 362
Sharing Code 363
Summary 363
14 Enhancing Line-of-Business Applications and Running Out of the Browser 365
Enhancing LOB Applications 365
Adding Paging 365
Showing Errors 369
Reconciling Data 371
Copying and Pasting Rows 373
Printing 373
Taking Silverlight Out of the Browser 382
Setting Up the Application 383
Uninstalling the Application 384
Debugging the OOB Application 385
Looking Under the Hood 386
Changing the Settings 387
Updating the Application 391
Installing from the Code 393
Saving Files 395
Working Offline 398
Summary 399
15 Developing Navigation Applications and Silverlight for Windows Phone 7 401
Navigating with Silverlight 401
Should You Always Use a Navigation Application? 404
Creating a New Navigation Application 404
Accessing Navigation Information 412
Providing Custom Navigation 413
Adding Navigation to a Non-Navigation Application 414
Developing with Silverlight for Windows Phone 7 415
Getting Hardware 416
Targeting a Specific Audience 416
Developing for Windows Phone 7 416
Developing for a Uniform Hardware Platform 417
Designing for the Phone 418
Installing the Tools 419
Selling Your Applications 420
Building Compatible Applications for the Desktop and the Phone 420
Continuing the Exploration 432
Summary 432
16 Using Effects and Recording Media 435
Creating Effects with Pixel Shaders 435
Writing, Finding, and Compiling Shader Files 436
Creating and Modifying Shaders with Shazzam 437
Integrating Shaders in the Application 438
Adding Properties and Animating Shaders 440
Using Shaders for Transitions in the VSM 443
Accessing the Webcam and the Microphone 443
Getting the List of Devices 444
Enabling Access 448
Displaying the Video Output 448
Detecting Whether Other Applications Use the Device 449
Capturing Audio 450
Converting to a WAV File 450
Creating a Sink 450
Using the SaveFileDialog 452
Using the Sink and Adding Commands 454
Wiring the Commands 456
Testing Audio Recording 457
Writing to a Bitmap 457
Saving the Picture to a PNG File 459
Manipulating Pixels 460
Extending WriteableBitmap 462
Using the Open File Dialog 464
Learning About News in Media 465
Summary 466
17 New Transforms, Right Click, HTML Browser, WebBrowserBrush, and Isolated Storage 467
Transforming Elements in a Projection 467
Setting Additional Properties 469
Using a Matrix3DProjection 470
Animating the PlaneProjection 470
Composing Transforms 472
Handling the Right-Click Event 473
Handling a Routed Event 473
Displaying a Context Menu 476
Hosting an HTML Browser (Out-of-the-Browser Only) 478
Understanding the Limitations 479
Building a Simple Web Browser 480
Loading HTML Content from Memory 484
Invoking JavaScript 485
Writing and Reading in the Isolated Storage 485
Saving to the Isolated Storage 485
Reading from the Isolated Storage 492
Deleting Files 495
Using the IsolatedStorageSettings 496
Trusting the Isolated Storage or Not 499
Painting with HTML 499
Summary 501
18 Drag and Drop, Full Screen, Clipboard, COM Interop, Duplex Polling, Notification Windows, and Splash Screens 503
Dragging and Dropping 503
Dragging Files on the Silverlight Application 504
Drag-and-Drop Restrictions 505
Working in Full Screen 507
Getting Keyboard Support in Full-Screen Mode
(Elevated Permissions) 507
Using Full Screen on a Monitor While Working on Another 509
Copying to and from the Clipboard 510
Working with COM (Elevated Permissions) 512
Understanding the Restrictions 512
Communicating with Microsoft Office 512
Communicating over Duplex Polling 519
Implementing the Server-Side Service 519
Unregistering a Client 524
Configuring the Service 524
Implementing the Client 525
Unsubscribing and Resubscribing 528
Testing the Application 529
Displaying Notification Windows 530
Understanding the Restrictions 530
Adding a Notification Window 531
Queuing Notification Windows 533
Interacting with the Main Window 533
Creating a Custom Splash Screen 534
Summary 537
19 Authentication, Event to Command Binding, Random Animations, Multitouch, Local Communication, and Bing Maps Control 539
Logging In with Authentication 539
Creating a New Website 540
Adding and Managing Users 541
Configuring the Authentication Web Service 542
Checking the Access 544
Adding References to the Services 544
Implementing the Client 545
Binding an Event to a Command 547
Executing a Command When a TextBox Loses Focus 548
Building Random Animations 554
Creating the Base Animation in Blend 554
Randomizing the Animation 555
Implementing Multitouch in Silverlight 557
Getting the Right Computer 558
Investigating Existing Elements 559
Using Multitouch Libraries 560
Scaling, Rotating, and Translating 560
Implementing a Multitouch Application 561
Using Multitouch in Windows Phone 7 Applications 563
Finding More Information 563
Enabling Local Communication 563
Understanding the Restrictions 564
Building a Receiver 564
Building a Sender 565
Testing the Application 567
Mapping with the Bing Maps Control 567
Adding the Map 568
Getting Location Information and Marking It 569
Getting More Information 571
Summary 571
20 Building Extensible and Maintainable Applications 573
Inverting Dependencies with Unity 573
Refactoring to Smaller and Simpler Classes 575
Setting Up the Services 578
Calling the Setup Method and Wiring Up 582
Discovering More About Unity 583
Composing an Application with MEF 583
Exporting and Importing 584
Downloading on Demand 594
What About Prism? 603
Using an MVVM Framework 604
Discovering the Components 604
Sending Messages 604
Summary 607
21 Optimizing Performance 609
Improving the XAP Download Time 609
Loading Content on Demand 611
Caching Common Assemblies 615
Virtualizing the User Interface 616
Virtualizing the ItemsControl 617
Unvirtualizing the ListBox 619
Simplifying the DataTemplate 619
Creating Items in Batches 620
Working in Threads 621
Accelerating the User Interface 624
Enabling Hardware Acceleration in the Browser 624
Enabling Hardware Acceleration Out of the Browser 628
Accelerating with Care 629
Accelerating in the Windows Phone 7 630
Using a Code Profiler 630
Avoiding Memory Leaks 631
Saving an Object on the Stack or the Heap 631
Collecting Garbage and Leaking Memory 632
Living a Shorter Life 635
Unregistering Event Handlers 635
Disposing Objects 637
Using Weak References 637
Finding a Leak 638
Summary 639
22 Advanced Development Techniques 641
Using New C# and NET Features 641
Using Modern Programming Syntax 641
Creating Extension Methods 645
Consuming Dynamic Objects 646
Using Named/Optional Parameters 646
Localizing Applications 647
Adding a Resource File 647
Making an Application Localizable 648
Using Tools 651
Downloading Resource Applications on Demand 652
Encrypting and Decrypting 652
Understanding the Encryption/Decryption Mechanism 652
Multithreading 653
What Is a Thread? 653
Using the ThreadPool 654
Dispatching Back to the UI Thread 657
Creating and Using a BackgroundWorker 660
Locking Critical Resources 663
Enhancing Multithreaded Code 666
Unit Testing the Application 667
Installing a Unit Test Framework 668
Adding Functionality with TDD 668
Using Code Coverage 673
Unit Testing Windows Phone 7 Applications 673
Summary 674
Conclusion 675
Index 677