Upload
others
View
1
Download
0
Embed Size (px)
Citation preview
Silverlight Recipes
A Problem-Solution Approach.
•
i
Jit Ghosh and Rob Cameron
Apress*
Contents at a Glance iv
Contents v
About the Author xxvi
About the Technical Reviewer xxvli
Acknowledgments xxviil
Introduction xxix
Chapter 1: A Quick Tour of Silverlight 4 Development 1
Getting Up to Speed with Silverlight 1
Silverlight 3 Highlights 3
Silverlight 4 6
Silverlight and Visual Studio 2010 8
i-1. Setting Up the Silverlight 4 Environment 9
Problem 9
Solution 10
How It Works 10
1-2. Installing Additional Silverlight-Related Services and Controls 10
Problem 10
Solution 11
How It Works 11
1-3. Understanding the Structure of a Silverlight Solution 11
Problem 11
Solution 11
How It Works 12
The Code 13
U CONTENTS
1-4. Understanding the Developer/Designer Workflow 21
Problem 21
Solution 21
How It Works 22
1-5. Understanding the Basics of Expression Blend 4 25
Problem 25
Solution 25
How It Works 25
The Code 27
1-6. Accessing Source Control 32
Problem 32
Solution 32
How It Works 32
1 -7. Running Silverlight 4 on a Mac 33
Problem 33
Solution 33
How It Works 33
1-8. Running Silverlight on Linux 33
Problem 33
Solution 34
How It Works 34
Chapter 2: Application Design and Programming Model 35
The Mechanics of Silverlight Applications 35
2-1. Leverage and Locate Controls and Classes 37
Problem 37
Solution 37
How It Works 37
2-2. Dynamically Loading XAML 48
Problem 48
Solution 48
vi
CONTENTS
How It Works 48
The Code 48
2-3. Persisting Data on the Client 54
Problem 54
Solution 54
How It Works 54
The Code 56
2-4. Opening a Local File from a Silverlight Application 61
Problem 61
Solution 61
How It Works 61
The Code , 62
2-5. Accessing XML Data 65
Problem , 65
Solution 65
How It Works 66
The Code 66
2-6. Managing Unhandled Exceptions 70
Problem 70
Solution 71
How It Works 71
2-7. Executing Work on a Background Thread with Updates 71
Problem 71
Solution 72
How It Works 72
The Code 73
2-8. Updating the Ul from a Background Thread 81
Problem 81
Solution 81
vji
CONTENTS
How It Works 82
The Code 83
2-9. Managing XAML Resources 86
Problem 86
Solution 86
How It Works 86
The Code 87
2-10. Managing Embedded Resources 91
Problem 91
Solution 91
How It Works 91
The Code 91
2-11. Creating Silverlight Using Ruby, Python, or JScript 95
Problem 95
Solution 96
How It Works 96
The Code 97
2-12. Creating Application Services 99
Problem 99
Solution 100
How It Works 100
The Code 101
2-13. Managing Resources in Large Projects 105
Problem 105
Solution 105
How It Works 106
The Code 106
2-14. Save a File Anywhere on the User's System 108
Problem 108
Solution 109
viii
CONTENTS
How It Works 109
The Code 109
Chapter 3: Developing User Experiences 111
3-1. Importing Art from Expression Design 112
Problem 112
Solution 112
How It Works 112
3-2. Working with Color and Gradients in Blend 115
Problem 115
Solution 115
How It Works 115
The Code 118
3-3. Positioning Ul Elements 122
Problem 122
Solution 122
How It Works 122
The Code 125
3-4. Drawing with Shapes, Paths, and Geometries 131
Problem 131
Solution 132
How It Works 132
The Code 139
3-5. Providing Scrollable Content 149
Problem 149
Solution 149
How It Works 150
The Code 150
3-6. Applying a Border to Elements 152
Problem 152
Solution 152
CONTENTS
How It Works 152
The Code 155
3-7. Using Simple Animations with Objects 157
Problem 157
Solution 157
How It Works 157
The Code 158
3-8. Animating Ul Elements with Keyframes 164
Problem 164
Solution 164
How It Works 164
The Code 167
3-9. Transforming an Object 169
Problem 169
Solution 170
How It Works 170
The Code 171
3-10. Creating a Simple Cartoon Scene 175
Problem 175
Solution 175
How It Works 175
The Code 175
3-11. Handling Keyboard Input 180
Problem 180
Solution 180
How It Works 180
The Code 181
3-12. Working with Ink 184
Problem 184
Solution 184
x
CONTENTS
How It Works 185
The Code 185
3-13. Adding 3-D Effects to Ul Elements 191
Problem 191
Solution 191
How It Works 191
The Code 192
3-14. Dynamically Creating Bitmaps 198
Problem 198
Solution 198
How It Works 198
The Code 199
3-15. Improving Graphic Animation and Video Performance 205
Problem 205
Solution 205
How It Works 205
The Code 206
3-16. Improve Animation with Custom Easing Functions 209
Problem 209
Solution 209
How It Works 209
The Code 210
3-17. Adding Pixel Shader Visual Effects 216
Problem 216
Solution 216
How It Works 216
The Code 217
3-18. Create and Work with Design-Time Data in Expression Blend 221
Problem 221
Solution 221
3d
CONTENTS
How It Works 221
The Code 224
3-19. Reuse Application Interactivity with Behaviors 231
Problem 231
Solution 231
How It Works 231
The Code 232
3-20. Customizing the Right-Click Context Menu 233
Problem 233
How it Works 234
The Code 234
3-21. Accessing the Clipboard 238
Problem 238
How it Works 238
The Code 239
3-22. Using Right-to-Left Text 241
Problem 241
How it Works 242
The Code 242
3- 23. Prototype Application Design 243
Problem 243
Solution 244
How It Works 244
The Code 244
Chapter 4: Data Binding 247
4-1. Binding Application Data to the Ul 247
Problem 247
Solution 247
How It Works 247
The Code 249
xii
CONTENTS
4-2. Binding Using a DataTemplate 255
Problem 255
Solution 255
How It Works 255
The Code 257
4-3. Receiving Change Notifications for Bound Data .263
Problem 263
Solution 263
How It Works 263
The Code 265
4-4. Converting Values During Data Binding 280
Problem 280
Solution 280
How It Works 280
The Code 282
4-5. Binding Across Elements 293
Problem 293
Solution 293
How It Works 293
The Code 295
4-6. Validating Input for Bound Data 301
Problem 301
Solution 301
How It Works 301
The Code 302
4-7. Controlling Updates 316
Problem 316
Solution 316
How It Works 316
The Code 317
xili
CONTENTS
4-8. Providing reasonable defaults for bound data 326
Problem 326
Solution 326
How It Works 326
The Code 328
Chapter 5: Controls 333
A Word About the Samples 334
5-1. Customizing a Control's Basic Appearance 334
Problem 334
Solution 334
How It Works 334
The Code 336
5-2. Replacing the Default Ul of a Control 338
Problem 338
Solution 338
How It Works 338
The Code 348
5-3. Customizing the Default ListBoxltem Ul 354
Problem 354
Solution 354
How It Works 354
The Code 355
5-4. Displaying Information in a Pop-up 363
Problem 363
Solution 363
How It Works 363
The Code 365
5-5. Displaying Row Details in a DataGrid 375
Problem 375
Solution 375
xiv
CONTENTS
How It Works 375
The Code 375
5-6. Applying Custom Templates to a DataGrid Cell ,
385
Problem 385
Solution 385
How It Works 385
The Code 385
5-7. Creating Custom Column Types for a DataGrid 391
Problem 391
Solution 391
How It Works 391
The Code 393
5-8. Creating a Composite User Control 398
Problem 398
Solution 398
How It Works 398
The Code 403
5-9. Creating a Custom Layout Container 412
Problem 412
Solution 413
How It Works 413
The Code 414
5-10. Creating a Custom Control 425
Problem 425
Solution 426
How It Works 426
The Code 429
5-11. Defining a Custom Visual State 442
Problem ,
442
Solution 442
xv
m CONTENTS
How It Works 442
The Code 443
5-12. Controlling ScrollViewer Scroll Behavior 454
Problem 454
Solution 454
How It Works 454
The Code ,
455
5-13. Customizing the Binding Validation User Interface 463
Problem 463
Solution 464
How It Works 464
The Code 469
5- 14. Control Behavior in Expression Blend 479
Problem 479
Solution 479
How It Works 479
The Code 483
5.15 Enhancing the Design Experience with Behaviors and Triggers 486
Problem 486
Solution 486
How It Works 486
The Code 488
Chapter 6: Browser Integration 495
6- 1. Host Silverlight on Any Technology 495
Problem 495
Solution 496
How It Works 496
The Code 498
xvi
CONTENTS
6-2. Setting Focus for Keyboard Input 500
Problem 500
Solution 500
How It Works ,
501
The Code 501
6-3. Implementing a Full-Screen Ul 506
Problem 506
Solution 506
How It Works 506
The Code 508
6-4. Calling a JavaScript Method from Managed Code 515
Problem 515
Solution 515
How It Works 515
The Code 517
6-5. Calling a Managed Code Method from JavaScript 523
Problem 523
Solution 523
How It Works 524
The Code 525
6-6. Exchanging Data Among Multiple Plug-ins 532
Problem 532
Solution 532
How It Works 532
The Code 533
6-7. Layering HTML over the Silverlight Plug-in 538
Problem 538
Solution 538
How It Works 539
The Code 539
xvii
CONTENTS
6-8. Hosting HTML in a Silverlight Application 541
Problem 541
Solution 541
How It Works 542
The Code 542
6-9. Painting a Silverlight Element with HTML 544
Problem 544
Solution 544
How It Works 544
The Code 544
6-10. Taking Advantage of the Navigation Framework 546
Problem 546
Solution 546
How It Works 546
The Code 549
6-11. Embedding Silverlight within a Windows Gadget 555
Problem 555
Solution 555
How It Works 555
The Code 560
6-12. Embedding Silverlight in an Internet Explorer 8 Web Slice 571
Problem 571
Solution 571
How It Works 571
The Code 571
Chapter 7: Networking and Web Service Integration 577
A Quick Word about the Samples 578
7-1. Consuming a WCF Service 579
Problem 579
Solution 57fl
xviii
« CONTENTS
How It Works 579
The Code 583
7-2. Exchanging XML Messages over HTTP 600
Problem 600
Solution 600
How It Works 600
The Code, 602
7-3. Using JSON Serialization over HTTP 613
Problem 613
Solution 613
How It Works... 613
The Code 615
7-4. Accessing Resources over HTTP 618
Problem 618
Solution 618
How It Works 618
The Code 619
7-5. Using Sockets to Communicate over TCP 643
Problem 643
Solution 643
How It Works 643
The Code 646
7-6. Enabling Cross-Domain Access 676
Problem 676
Solution 676
How It Works 676
The Code 678
7-7. Exchanging Data between Silverlight Applications ...680
Problem 680
Solution 680
xix
CONTENTS
How It Works 680
The Code 683
Chapter 8: Building Out Of Browser Silverlight Applications 699
8-1. Building a Silverlight application to run outside the browser 699
Problem 699
Solution 700
How It Works 700
The Code 706
8-2. Controlling the Application Window 722
Problem 722
Solution 722
How It Works 722
The Code 724
8-3. Using COM Interoperability and File System Access 730
Problem 730
Solution 731
How It Works 731
The Code 733
Chapter 9: Building LOB Applications 745
Silverlight LOB Enhancements 745
Data Access Enhancements 745
WCF Data Services 746
WCF RIA Services 746
9-1. Accessing RESTful Data using OData 746
Problem 746
Solution 746
How It Works 747
The Code 747
xx
CONTENTS
9-2. Using Visual Studio 2010 WCF Data Services Tooling 750
Problem 750
Solution 750
How It Works 750
The Code 750
9-3. Implementing CRUD Operations in WCF Data Services 756
Problem 756
Solution 756
How It Works 757
The Code 757
9-4. Using Visual Studio 2010 WCF RIA Data Services Tooling 760
Problem 760
Solution 760
How It Works 760
The Code 761
9-5. Taking Advantage of the Business Application Template 768
Problem 768
Solution 768
How It Works 768
The Code 768
9-6. Databinding in XAML 770
Problem 770
Solution 770
How It Works 771
The Code 771
9-7. Navigating RIA LOB Data 773
Problem 773
Solution 773
How It Works 773
The Code 773
xxi
CONTENTS
9-8. Implementing CRUD Operations in RIA Services 775
Problem 775
Solution 775
How It Works 776
The Code 776
9-9. Data Validation through Data Annotation .779
Problem 779
Solution 779
How It Works 779
The Code 780
9-10. Printing in a Silverlight LOB Application 783
Problem 783
Solution 783
How It Works 783
The Code 783
Chapter 10: Integrating Rich Media 787
10-1. Adding Video to a Page 787
Problem 787
Solution 788
How It Works 788
The Code 791
10-2. Creating a Complete Video Player 792
Problem 792
Solution 792
How It Works 792
The Code 795
10-3. Adding Streaming Media Support 823
Problem 823
Solution 824
xxii
CONTENTS
How It Works 824
The Code 829
10-4. Using Playliststo Package Media 864
Problem 864
Solution 864
How It Works 864
The Code 867
10-5. Using Markers to Display Timed Content 870
Problem 870
Solution 870
How It Works 870
The Code 872
10-6. Displaying and Seeking Using SMPTE Timecodes.. 886
Problem 886
Solution 887
How It Works 887
The Code 888
10-7. Building a Managed Decoder for Silverlight 900
Problem 900
Solution 900
How It Works 900
The Code 906
10-8. Using a WebCam 924
Problem 924
Solution 925
How It Works 925
The Code 927
10-9. Processing RawWebCam Output 932
Problem 932
Solution 932
xxiii
m CONTENTS
How It Works 932
The Code 933
Chapter 11: Integrating Microsoft IIS Smooth Streaming 949
11-1. Setting up Smooth Streaming 950
Problem 950
Solution 950
How It Works 950
The Code 957
11-2. Using the SmoothStreamingMediaElement 957
Problem 957
Solution 958
How It Works 958
The Code 958
11-3. Adding Metadata Streams 963
Problem 963
Solution 963
How It Works 963
The Code 966
11-4. Merging Data from External Manifests 974
Problem 974
Solution 974
How It Works 974
The Code 975
11-5. Scheduling Additional Clips 977
Problem 977
Solution 977
How It Works 977
The Code 978
xxiv
CONTENTS
11-6. Varying Playback Speeds 981
Problem 981
Solution 982
How It Works 982
The Code 982
11-7. Combining Streams Using Composite Manifests 982
Problem 982
Solution 983
How It Works 983
The Code 985
Index 987
XXV