You are previewing Pro Silverlight 2 in C# 2008.
O'Reilly logo
Pro Silverlight 2 in C# 2008

Book Description

Silverlight is a lightweight browser plug-in that frees your code from the traditional confines of the browser. It's a rules-changing, groundbreaking technology that allows you to run rich client applications right inside the browser. Even more impressively, it's able to host true .NET applications in non-Microsoft browsers (like Firefox) and on non-Microsoft platforms (like Mac OS X). Silverlight is still new and evolving fast, and you need a reliable guidebook to make sense of it.

With four-color graphics and screenshots throughout, Pro Silverlight 2 in C# 2008 is the perfect reference: you'll learn about the features that put Silverlight in direct competition with Adobe Flash, such as rich support for 2D drawing, animations, and media playback, and best of all, you'll experience the plumbing of .NET and the design model of WPF through Silverlight-all of the same .NET technology that developers use to design next-generation Windows applications. Author Matthew MacDonald provides an expertly crafted tutorial written from professional developer to professional developer. You'll learn to create rich media applications using Silverlight in the environment you're most productive in-no matter what the target platform.

Table of Contents

  1. Copyright
  2. About the Author
  3. About the Technical Reviewer
  4. Acknowledgments
  5. Introduction
    1. Understanding Silverlight
      1. Silverlight System Requirements
      2. Silverlight vs. Flash
      3. Silverlight 1 and 2
      4. Silverlight and WPF
    2. About This Book
      1. What You Need to Use This Book
      2. Code Samples
      3. Feedback
    3. The Last Word
  6. 1. Introducing Silverlight
    1. 1.1. Silverlight and Visual Studio
      1. 1.1.1. Understanding Silverlight Websites
    2. 1.2. Creating a Stand-Alone Silverlight Project
    3. 1.3. Creating a Simple Silverlight Page
      1. 1.3.1. Adding Event Handling Code
      2. 1.3.2. Browsing the Silverlight Class Libraries
      3. 1.3.3. Testing a Silverlight Application
    4. 1.4. Silverlight Compilation and Deployment
      1. 1.4.1. Compiling a Silverlight Application
      2. 1.4.2. Deploying a Silverlight Application
      3. 1.4.3. The HTML Test Page
      4. 1.4.4. The Application Manifest
    5. 1.5. Creating an ASP.NET-Hosted Silverlight Project
    6. 1.6. The Last Word
  7. 2. XAML
    1. 2.1. XAML Basics
      1. 2.1.1. XAML Namespaces
      2. 2.1.2. The Code-Behind Class
    2. 2.2. Properties and Events in XAML
      1. 2.2.1. Simple Properties and Type Converters
      2. 2.2.2. Complex Properties
      3. 2.2.3. Attached Properties
      4. 2.2.4. Nesting Elements
      5. 2.2.5. Events
      6. 2.2.6. The Full Eight Ball Example
    3. 2.3. Resources
      1. 2.3.1. The Resources Collection
      2. 2.3.2. The Hierarchy of Resources
      3. 2.3.3. Accessing Resources in Code
    4. 2.4. The Last Word
  8. 3. Layout
    1. 3.1. The Layout Containers
      1. 3.1.1. The Panel Background
      2. 3.1.2. Borders
    2. 3.2. Simple Layout with the StackPanel
      1. 3.2.1. Layout Properties
      2. 3.2.2. Alignment
      3. 3.2.3. Margins
      4. 3.2.4. Minimum, Maximum, and Explicit Sizes
    3. 3.3. The Grid
      1. 3.3.1. Fine-Tuning Rows and Columns
      2. 3.3.2. Nesting Layout Containers
      3. 3.3.3. Spanning Rows and Columns
      4. 3.3.4. The GridSplitter
    4. 3.4. Coordinate-Based Layout with the Canvas
      1. 3.4.1. Layering with ZIndex
      2. 3.4.2. Clipping
    5. 3.5. Custom Layout Containers
      1. 3.5.1. The Two-Step Layout Process
      2. 3.5.2. A Wrapping Panel
    6. 3.6. Sizing Pages
      1. 3.6.1. Scrolling
      2. 3.6.2. Scaling
      3. 3.6.3. Full Screen
    7. 3.7. Navigation
      1. 3.7.1. Loading Child User Controls
      2. 3.7.2. Hiding Elements
    8. 3.8. The Last Word
  9. 4. Dependency Properties and Routed Events
    1. 4.1. Dependency Properties
      1. 4.1.1. Defining and Registering a Dependency Property
      2. 4.1.2. Dynamic Value Resolution
      3. 4.1.3. Attached Properties
      4. 4.1.4. The WrapPanel Example
    2. 4.2. Routed Events
      1. 4.2.1. The Core Element Events
      2. 4.2.2. Event Bubbling
      3. 4.2.3. Handled (Suppressed) Events
      4. 4.2.4. An Event Bubbling Example
      5. 4.2.5. Mouse Movements
      6. 4.2.6. Capturing the Mouse
      7. 4.2.7. A Mouse Event Example
      8. 4.2.8. Mouse Cursors
      9. 4.2.9. Key Presses
      10. 4.2.10. Key Modifiers
      11. 4.2.11. Focus
    3. 4.3. The Last Word
  10. 5. Elements
    1. 5.1. The Silverlight Elements
    2. 5.2. Static Text
      1. 5.2.1. Font Properties
      2. 5.2.2. Underlining
      3. 5.2.3. Runs
      4. 5.2.4. Wrapping Text
    3. 5.3. Images
      1. 5.3.1. Image Sizing
      2. 5.3.2. Image Errors
    4. 5.4. Content Controls
      1. 5.4.1. The Content Property
      2. 5.4.2. Aligning Content
    5. 5.5. Buttons
      1. 5.5.1. The HyperlinkButton
      2. 5.5.2. The ToggleButton and RepeatButton
      3. 5.5.3. The CheckBox
      4. 5.5.4. The RadioButton
    6. 5.6. Tooltips and Pop-Ups
      1. 5.6.1. Customized ToolTips
      2. 5.6.2. The Popup
    7. 5.7. Items Controls
      1. 5.7.1. The ListBox
      2. 5.7.2. The ComboBox
      3. 5.7.3. The TabControl
    8. 5.8. Text Controls
      1. 5.8.1. Text Selection
      2. 5.8.2. The PasswordBox
    9. 5.9. Range-Based Controls
      1. 5.9.1. The Slider
      2. 5.9.2. The ProgressBar
    10. 5.10. Date Controls
    11. 5.11. The Last Word
  11. 6. The Application Model
    1. 6.1. Application Events
      1. 6.1.1. Application Startup
      2. 6.1.2. Application Shutdown
      3. 6.1.3. Unhandled Exceptions
      4. 6.1.4. XAML Resources
    2. 6.2. Application Tasks
      1. 6.2.1. Accessing the Current Application
      2. 6.2.2. Initialization Parameters
      3. 6.2.3. Changing the Page
      4. 6.2.4. Splash Screens
    3. 6.3. Resources
      1. 6.3.1. Placing Resources in the Application Assembly
      2. 6.3.2. Placing Resources in the Application Package
      3. 6.3.3. Placing Resources on the Web
    4. 6.4. Class Library Assemblies
      1. 6.4.1. Using Resources in an Assembly
      2. 6.4.2. Downloading Assemblies on Demand
    5. 6.5. The Last Word
  12. 7. Shapes and Geometries
    1. 7.1. Basic Shapes
      1. 7.1.1. The Shape Classes
      2. 7.1.2. Rectangle and Ellipse
      3. 7.1.3. Sizing and Placing Shapes
      4. 7.1.4. Line
      5. 7.1.5. Polyline
      6. 7.1.6. Polygon
      7. 7.1.7. Line Caps and Line Joins
      8. 7.1.8. Dashes
    2. 7.2. Paths and Geometries
      1. 7.2.1. Line, Rectangle, and Ellipse Geometries
      2. 7.2.2. Combining Shapes with GeometryGroup
      3. 7.2.3. Curves and Lines with PathGeometry
      4. 7.2.4. The Geometry Mini-Language
      5. 7.2.5. Clipping with Geometry
    3. 7.3. Exporting Clip Art
      1. 7.3.1. Expression Design
      2. 7.3.2. Conversion
      3. 7.3.3. Save or Print to XPS
    4. 7.4. The Last Word
  13. 8. Brushes and Transforms
    1. 8.1. Brushes
      1. 8.1.1. The LinearGradientBrush
      2. 8.1.2. The RadialGradientBrush
      3. 8.1.3. The ImageBrush
    2. 8.2. Transparency
      1. 8.2.1. Opacity Masks
      2. 8.2.2. Making the Silverlight Control Transparent
    3. 8.3. Transforms
      1. 8.3.1. Transforming Shapes
      2. 8.3.2. Transforms and Layout Containers
      3. 8.3.3. A Reflection Effect
    4. 8.4. The Last Word
  14. 9. Animation
    1. 9.1. Understanding Silverlight Animation
      1. 9.1.1. The Rules of Animation
    2. 9.2. Creating Simple Animations
      1. 9.2.1. The Animation Class
      2. 9.2.2. The Storyboard Class
      3. 9.2.3. Starting an Animation with an Event Trigger
      4. 9.2.4. Starting an Animation with Code
      5. 9.2.5. Configuring Animation Properties
      6. 9.2.6. Animation Lifetime
      7. 9.2.7. Simultaneous Animations
      8. 9.2.8. Controlling Playback
      9. 9.2.9. Desired Frame Rate
    3. 9.3. Animation Types Revisited
      1. 9.3.1. Animating Transforms
      2. 9.3.2. Animating Brushes
      3. 9.3.3. Key Frame Animation
    4. 9.4. Animations in Code
      1. 9.4.1. The Main Page
      2. 9.4.2. The Bomb User Control
      3. 9.4.3. Dropping the Bombs
      4. 9.4.4. Intercepting a Bomb
      5. 9.4.5. Counting Bombs and Cleaning Up
    5. 9.5. Encapsulating Animations
      1. 9.5.1. Page Transitions
      2. 9.5.2. The Base Class
      3. 9.5.3. The Wipe Transition
    6. 9.6. Frame-Based Animation
    7. 9.7. The Last Word
  15. 10. Sound, Video, and Deep Zoom
    1. 10.1. Supported File Types
    2. 10.2. The MediaElement
      1. 10.2.1. Controlling Playback
      2. 10.2.2. Handling Errors
      3. 10.2.3. Playing Multiple Sounds
      4. 10.2.4. Changing Volume, Balance, and Position
      5. 10.2.5. Playing Video
      6. 10.2.6. Progressive Downloading and Streaming
      7. 10.2.7. Client-Side Playlists
      8. 10.2.8. Server-Side Playlists
    3. 10.3. Advanced Video Playback
      1. 10.3.1. Video Encoding
      2. 10.3.2. Markers
      3. 10.3.3. VideoBrush
      4. 10.3.4. Video Effects
    4. 10.4. Deep Zoom
      1. 10.4.1. Creating a Deep Zoom Image Set
      2. 10.4.2. Using a Deep Zoom Image Set in Silverlight
    5. 10.5. The Last Word
  16. 11. Styles, Templates, and Custom Controls
    1. 11.1. Styles
      1. 11.1.1. Defining a Style
      2. 11.1.2. Applying a Style
      3. 11.1.3. Organizing Styles
    2. 11.2. Template Basics
      1. 11.2.1. Creating a Template
    3. 11.3. The Parts and States Model
      1. 11.3.1. Understanding States with the Button Control
      2. 11.3.2. Showing a Focus Cue
      3. 11.3.3. Transitions
      4. 11.3.4. Understanding Parts with the Slider Control
    4. 11.4. Creating Templates for Custom Controls
      1. 11.4.1. Planning the Expander
      2. 11.4.2. Creating the Solution
      3. 11.4.3. Starting the Expander Class
      4. 11.4.4. Adding the Default Style with Generic.xaml
      5. 11.4.5. Choosing Parts and States
      6. 11.4.6. Starting the Default Control Template
      7. 11.4.7. The Expand or Collapse Button
      8. 11.4.8. Defining the State Animations
      9. 11.4.9. Wiring Up the Elements in the Template
      10. 11.4.10. Using the Expander
      11. 11.4.11. Using a Different Control Template
    5. 11.5. The Last Word
  17. 12. Browser Integration
    1. 12.1. Interacting with HTML Elements
      1. 12.1.1. Getting Browser Information
      2. 12.1.2. The HTML Window
      3. 12.1.3. Inspecting the HTML Document
      4. 12.1.4. Manipulating an HTML Element
      5. 12.1.5. Handling JavaScript Events
    2. 12.2. Code Interaction
      1. 12.2.1. Calling Browser Script from Silverlight
      2. 12.2.2. Calling Silverlight Methods from the Browser
      3. 12.2.3. Instantiating Silverlight Objects in the Browser
      4. 12.2.4. A Browser History Example
    3. 12.3. Combining Silverlight and HTML Content
      1. 12.3.1. Sizing the Silverlight Control to Fit Its Content
      2. 12.3.2. Placing the Silverlight Control Next to an HTML Element
    4. 12.4. The Last Word
  18. 13. ASP.NET and Web Services
    1. 13.1. ASP.NET Controls That Use Silverlight
      1. 13.1.1. The Silverlight Control
      2. 13.1.2. The MediaPlayer Control
      3. 13.1.3. Custom Controls
      4. 13.1.4. Hybrid Pages
    2. 13.2. Building Web Services for Silverlight
      1. 13.2.1. Creating a Web Service
      2. 13.2.2. Consuming a Web Service
      3. 13.2.3. Data Types
      4. 13.2.4. Cross-Domain Web Service Calls
      5. 13.2.5. Using ASP.NET Platform Services
    3. 13.3. The Last Word
  19. 14. Data Binding
    1. 14.1. Binding to Data Objects
      1. 14.1.1. Building a Data Object
      2. 14.1.2. Displaying a Data Object with DataContext
      3. 14.1.3. Storing a Data Object As a Resource
      4. 14.1.4. Editing with Two-Way Bindings
      5. 14.1.5. Validation
      6. 14.1.6. Change Notification
      7. 14.1.7. Building a Data Service
      8. 14.1.8. Calling the Data Service
    2. 14.2. Binding to a Collection of Objects
      1. 14.2.1. Displaying and Editing Collection Items
      2. 14.2.2. Inserting and Removing Collection Items
      3. 14.2.3. Binding to a LINQ Expression
      4. 14.2.4. Master-Details Display
    3. 14.3. Data Conversion
      1. 14.3.1. Formatting Strings with a Value Converter
      2. 14.3.2. Creating Objects with a Value Converter
      3. 14.3.3. Applying Conditional Formatting
    4. 14.4. Data Templates
      1. 14.4.1. Separating and Reusing Templates
      2. 14.4.2. More Advanced Templates
      3. 14.4.3. Changing Item Layout
    5. 14.5. The DataGrid
      1. 14.5.1. Creating a Simple Grid
      2. 14.5.2. Resizing and Rearranging Columns
      3. 14.5.3. Defining Columns
      4. 14.5.4. Formatting and Styling Columns
      5. 14.5.5. Formatting Rows
      6. 14.5.6. Row Details
      7. 14.5.7. Selection
      8. 14.5.8. Sorting Rows
      9. 14.5.9. Freezing Columns
    6. 14.6. The Last Word
  20. 15. Isolated Storage
    1. 15.1. Understanding Isolated Storage
      1. 15.1.1. The Scope of Isolated Storage
      2. 15.1.2. What to Put in Isolated Storage
    2. 15.2. Using Isolated Storage
      1. 15.2.1. File Management
      2. 15.2.2. Writing and Reading Data
      3. 15.2.3. Requesting More Space
      4. 15.2.4. Storing Objects with the XmlSerializer
      5. 15.2.5. Storing Application Settings
    3. 15.3. Reading Local Files with OpenFileDialog
    4. 15.4. The Last Word
  21. 16. Multithreading
    1. 16.1. Understanding Multithreading
      1. 16.1.1. The Goals of Multithreading
      2. 16.1.2. The DispatcherTimer
    2. 16.2. The Thread Class
      1. 16.2.1. Marshalling Code to the User Interface Thread
      2. 16.2.2. Creating a Thread Wrapper
      3. 16.2.3. Creating the Worker Class
      4. 16.2.4. Using the Thread Wrapper
      5. 16.2.5. Cancellation Support
    3. 16.3. The BackgroundWorker
      1. 16.3.1. Creating the BackgroundWorker
      2. 16.3.2. Running the BackgroundWorker
      3. 16.3.3. Tracking Progress
      4. 16.3.4. Supporting Cancellation
    4. 16.4. The Last Word
  22. 17. Networking
    1. 17.1. Interacting with the Web
      1. 17.1.1. Cross-Domain Access
      2. 17.1.2. HTML Scraping
      3. 17.1.3. REST and Other Simple Web Services
    2. 17.2. Processing Different Types of Data
      1. 17.2.1. XML
      2. 17.2.2. Services That Return XML Data
      3. 17.2.3. Services That Require XML Data
      4. 17.2.4. Services That Return SOAP Data
      5. 17.2.5. Services That Return JSON Data
      6. 17.2.6. RSS
    3. 17.3. Sockets
      1. 17.3.1. Understanding Sockets and TCP
      2. 17.3.2. The Policy Server
      3. 17.3.3. The Messaging Server
      4. 17.3.4. The Messenger Client
    4. 17.4. The Last Word