List of Examples – Silverlight 2 in Action, illustrated edition

List of Examples

Chapter 1. Introducing Silverlight

Snippet 1.1. XAML: A basic XAML file referencing two namespaces

Snippet 1.2. XAML: This example references a custom assembly called MyAssembly.dll that contains a namespace called MyNamespace. To access the elements within MyNamespace, the my prefix is defined.

Snippet 1.3. XAML: A Grid with a gradient background

Snippet 1.4. XAML: An example showing the use of two attached properties in action

Walk-through 1.1. Creating a Silverlight media experience in Visual Studio 2008

Walk-through 1.2. Matting a MediaElement using Expression Blend 2

Walk-through 1.3. Implementing the behavior of the video player in Visual Studio 2008

Chapter 2. Harmony with the web

Snippet 2.1. HTML: Referencing the Silverlight.Js utility file

Snippet 2.4. The Silverlight plug-in on the left uses absolute sizing by specifying pixel values for the height and width properties. The Silverlight plug-in on the right uses relative sizing by providing percentages for the height and width properties.

Snippet 2.6. JavaScript: A sample Silverlight plug-in onLoad event handler

Snippet 2.7. JavaScript: A sample Silverlight plug-in onError event handler

Snippet 2.8. HTML: Registering initialization parameters with a plug-in instance

Snippet 2.9. XML: A sample manifest file in XML

Snippet 2.11. C#: A sample UnhandledException event handler

Snippet 2.12. C#: Retrieving settings associated with hosting plug-in

Snippet 2.13. C#: Referencing an HTML element via managed code

Snippet 2.14. C#: Using the GetProperty and SetProperty methods

Snippet 2.15. C#: Assigning a style attribute from managed code

Snippet 2.16. C# Showing the pairs of the QueryString

Snippet 2.17. C#: Retrieving in-memory representation of hosting browser window in C#

Snippet 2.18. C#: Displaying an alert to a user

Snippet 2.19. C#: Launching the official Silverlight 2 in Action website in a new browser window

Snippet 2.20. C#: Displaying the name of the user’s browser

Snippet 2.21. C#: Preparing a type for scripting

Snippet 2.22. C#: Exposing a managed code item to the scripting world

Snippet 2.23. C#: Exposing an object to the scripting world

Snippet 2.24. JavaScript: Referencing a managed item from the scripting world

Snippet 2.25. C#: Calling JavaScript object’s function from managed code

Chapter 3. Back to the basics: Layout and text

Snippet 3.1. XAML: A basic Canvas element.

Snippet 3.2. XAML Result: A basic Canvas with some content. In this case the content is TextBlock.

Snippet 3.3. XAML: A basic TextBlock using the Canvas element Left and Top attached properties

Snippet 3.5. XAML Result: The default stacking order of elements

Snippet 3.6. XAML Result: Using the ZIndex to move an element into the foreground

Snippet 3.7. C#: Changing the position of an element at runtime

Snippet 3.8. XAML Result: A basic StackPanel with three elements

Snippet 3.9. XAML Result: A basic StackPanel that uses a Horizontal Orientation

Snippet 3.12. XAML: The basic syntax of the Column and Row attached properties

Snippet 3.13. XAML: The syntax of the ColumnSpan and RowSpan attached properties

Snippet 3.16. XAML Result: A Grid using absolute sizing

Snippet 3.17. C#: Programmatically adding a row and a column while setting the height and width

Snippet 3.18. C#: Programmatically removing a row and a column

Snippet 3.19. XAML: Referencing the extended controls assembly

Snippet 3.21. XAML Result: A basic TextBlock in action

Snippet 3.22. XAML: Setting the FontSize at design time

Snippet 3.23. XAML: Setting the FontStyle at design time

Snippet 3.24. C#: Setting the FontStyle at runtime

Snippet 3.25. XAML Result: A TextBlock with Inline elements

Snippet 3.26. XAML: Setting the Foreground of a TextBlock at design time

Snippet 3.27. XAML: Underlining text in a TextBlock.

Snippet 3.28. XAML Result: Wrapping text within a TextBlock

Snippet 3.29. XAML Result: The Left, Center, and Right TextAlignment options

Snippet 3.31. XAML Result: The effects of the Padding property on a TextBlock

Snippet 3.32. XAML Result: A more granular way to use the Padding property

Snippet 3.33. XAML: Setting the Cursor of a FrameworkElement at design time

Snippet 3.34. XAML Result: A StackPanel with three Visible UIElement items

Snippet 3.35. XAML Result: A StackPanel with a Collapsed UIElement

Snippet 3.37. XAML Result: The darker area represents the Margin; the lighter area represents the Padding.

Chapter 4. Handling user interaction

Snippet 4.1. JavaScript: The JavaScript that gives focus to a Silverlight control so that it can accept input from the keyboard

Snippet 4.2. C# XAML: A page in Silverlight that responds to the KeyDown and KeyUp events. The event handlers associated with these events update the TextBlock to show the key that was used.

Snippet 4.3. C#: A basic example using ModifierKeys

Snippet 4.5. XAML Result: The default look of a TextBox. The TextBox in this case is defined within a StackPanel.

Snippet 4.6. XAML Result: A multiline text box. In line-of-business applications, multiline text boxes are usually used for descriptions as shown here, or for simple text editors. The text in the TextBox shown was entered at runtime.

Snippet 4.7. XAML Result: The syntax of the PasswordBox. Notice the mask used in each field.

Snippet 4.8. XAML Result: The general syntax for a Button

Snippet 4.9. XAML Result: A HyperlinkButton in action

Snippet 4.10. XAML Result: The default appearances of a checked and unchecked RadioButton

Snippet 4.12. XAML Result: A basic CheckBox setup

Snippet 4.13. XAML Result: Using three-state mode check boxes.

Snippet 4.14. XAML Result: A ListBox that displays the days of the week

Snippet 4.15. XAML Result: Using a ListBoxItem as a ContentControl

Snippet 4.16. XAML Result: A ComboBox that has been used to select an item

Snippet 4.17. XAML Result: The basic syntax of a TabControl

Snippet 4.18. XAML Result: Customizing the Header of a TabItem

Snippet 4.19. XAML Result: The default appearance and basic syntax of a Calendar

Snippet 4.20. XAML Result: The default appearance and basic syntax of a DatePicker

Snippet 4.21. XAML Result: A basic ProgressBar

Snippet 4.22. XAML Result: An indeterminate ProgressBar

Snippet 4.23. XAML Result: A basic Slider to choose between 1 and 10

Snippet 4.25. C# Result: Creating an OpenFileDialog using the properties from table 4.13

Snippet 4.26. C#: Retrieving the selection of an OpenFileDialog

Snippet 4.27. C#: Retrieving the selection of an OpenFileDialog. The contents of a selected file are loaded into a fictional TextBlock called myTextBlock.

Snippet 4.28. XAML: A basic InkPresenter

Snippet 4.31. C#: Finishing the Stroke

Snippet 4.32. XAML: Stylizing a Stroke

Chapter 5. Getting down with data binding

Snippet 5.2. C#: Removing a Binding from a TextBox

Snippet 5.3. C# XAML: Binding a property to a TextBox in XAML

Snippet 5.4. C#: Implementing the INotifyPropertyChanged interface

Snippet 5.6. C# XAML Result: Binding a collection of Emoticon objects (from snippet 5.4) to a ListBox through the ItemsSource property

Snippet 5.7. C# XAML Result: Using the DisplayMemberPath to improve the display of a list of items

Snippet 5.9. XAML: Exposing the YesNoValueConverter from snippet 5.8 to XAML

Snippet 5.10. XAML: Using an IValueConverter in a Binding

Snippet 5.11. C# XAML Result: A DataTemplate used with a ContentControl (a Button, to be precise).

Snippet 5.12. C# XAML Result: An ItemTemplate used in an ItemsControl (a ListBox, to be precise).

Snippet 5.14. XAML Result: Manually adding columns to a DataGrid

Snippet 5.16. XAML Result: Empowering your users to sort the columns of a DataGrid

Snippet 5.19. C#: A traditional sample that retrieves all the bands within the Rock genre ordered by the band’s name.

Snippet 5.20. C#: A LINQ statement that retrieves all the bands in the Rock genre ordered by their name.

Snippet 5.21. XML: Albums.xml: A catalog of Albums represented within XML

Snippet 5.22. C#: A query statement that retrieves all the albums by the artist with ID of 7

Snippet 5.23. C#: A LINQ to XML example that retrieves all the albums by a specific artist from an XML file

Snippet 5.24. C# XAML Result: An example that shows using LINQ to join to two different types of data (in-memory objects and XML)

Chapter 6. Networking and communications

Snippet 6.1. XML: Base clientaccesspolicy.xml for WebClient or HTTP access

Snippet 6.2. XML: More restrictive client access policy

Snippet 6.4. C# XAML Result: Sending data to SOAP service

Snippet 6.6. C#: Threading the UI

Snippet 6.8. C#: POSTing data to a REST service

Snippet 6.9. XAML Result: Getting the XML

Snippet 6.10. C#: Using XLINQ

Snippet 6.11. C#: Using XmlReader

Snippet 6.12. C#: Using XmlSerializer

Snippet 6.13. JSON Result: Sample JSON

Snippet 6.14. C#: Reading JSON with the JsonObject

Snippet 6.15. C#: Reading JSON with the DataContractJsonSerializer

Snippet 6.16. C#: Downloading and displaying an RSS feed

Snippet 6.17. XML: An example RSS feed based on some blog

Snippet 6.18. C# XAML Result: Displaying Syndicated content

Snippet 6.19. XML: An example Atom feed based on some blog

Snippet 6.20. SL WCF: Reading an OUT parameter

Snippet 6.21. XML Result: Sample application to get score updates

Snippet 6.22. C#: Starting the process

Snippet 6.23. C#: Example asynchronous handler

Snippet 6.24. C#: Opening duplex channel and polling it

Snippet 6.25. C#: Looking for messages

Snippet 6.26. C#: Reading message

Snippet 6.27. XML: clientaccesspolicy.xml for socket access

Snippet 6.28. C#: Snippet 6.28 Opening TCP socket

Snippet 6.29. C#: Handling response

Snippet 6.30. C#: Receiving message

Chapter 7. Managing digital media

Snippet 7.1. XAML: A MediaElement that uses relative syntax

Snippet 7.2. XAML: A MediaElement that uses absolute syntax

Snippet 7.5. C#: An example that shows the NaturalDuration of a MediaElement

Snippet 7.6. XAML: A MediaElement referencing a client-side playlist

Snippet 7.7. XML: An example of an ASX file

Snippet 7.8. XML: A sample WSX file

Snippet 7.9. XAML: Referencing a server-side playlist. Note: This stream doesn’t actually exist.

Snippet 7.10. C# XAML: A sample of using the MarkerReached event to show a caption on a MediaElement

Snippet 7.11. C#: Switching a plug-in into full-screen mode

Snippet 7.12. XAML Result: An Image that uses a picture from another domain

Snippet 7.13. XAML: Referencing a relative multiscale image in XAML

Snippet 7.14. C#: Referencing a relative multiscale image at runtime

Snippet 7.16. XAML: Defining the ViewportOrigin in XAML. Notice the syntax of the ViewportOrigin property.

Snippet 7.17. C#: Defining the ViewportOrigin in C#. Notice the syntax of the ViewportOrigin property.

Snippet 7.18. XAML: Use of the None option on an adequately sized Image element

Snippet 7.19. XAML Result: Use of the None option on an adequately sized Image element

Snippet 7.20. XAML Result: A uniformly stretched Image with a photo smaller than the element

Snippet 7.21. XAML Result: A uniformly stretched Image with a photo larger than the element

Snippet 7.22. XAML Result: An image using the Fill option to stretch an Image

Snippet 7.23. XAML Result: An Image using the UniformToFill Stretch option

Chapter 8. Getting a grip on graphics

Snippet 8.1. XAML Result: A basic Line

Snippet 8.2. XAML Result:A basic Rectangle

Snippet 8.3. XAML Result: A Rectangle with rounded corners

Snippet 8.4. XAML Result: A bulging rectangle

Snippet 8.5. XAML Result: The syntax and look of a basic Ellipse

Snippet 8.6. XAML Result: A Polyline

Snippet 8.7. XAML Result: A basic Polygon in action

Snippet 8.9. Line XAML Path XAML Result: Comparison between the Line and LineGeometry

Snippet 8.10. Rectangle XAML Path XAML Result: A RectangleGeometry compared to a Rectangle

Snippet 8.11. Elipse XAML Path XAML Result: An EllipseGeometry compared to an Ellipse

Snippet 8.12. XAML Result: A PathGeometry

Snippet 8.13. XAML Result: A composite geometry to make a key

Snippet 8.14. XAML Result: A basic SolidColorBrush in action

Snippet 8.15. XAML: The alternative approach to the SolidColorBrush

Snippet 8.16. XAML Result: A basic LinearGradientBrush rendered diagonally

Snippet 8.17. XAML Result: A basic LinearGradientBrush rendered horizontally

Snippet 8.18. XAML Result: A horizontal LinearGradientBrush with multiple transitions

Snippet 8.19. XAML Result: A basic RadialGradientBrush

Snippet 8.20. XAML Result: Comparing various uses of the RadiusX and RadiusY properties of the RadialGradientBrush

Snippet 8.21. XAML Result: An example of an ImageBrush

Snippet 8.22. XAML: An example of a VideoBrush

Snippet 8.23. XAML: An example of a VideoBrush within a TextBlock

Snippet 8.24. XAML Result: A square that has been rotated by 30 degrees

Snippet 8.25. XAML Result: A square that has been scaled by a magnitude of 2.5

Snippet 8.26. XAML Result: A Rectangle that has been skewed by 18 degrees

Snippet 8.27. XAML Result: A basic translation in action

Snippet 8.28. XAML Result: A sample of all of the transforms on a TextBox. Notice how the content of the TextBox is also transformed as the text is entered

Walk-through 8.1. Creating an ellipse within Expression Blend 2

Walk-through 8.2. Turning an ellipse into a ball within Expression Blend 2

Walk-through 8.3. Adding event handlers to the ball in Visual Studio

Chapter 9. Bringing it to life: Animation

Snippet 9.3. XAML: The XAML for animating a double-precision floating-point number

Snippet 9.4. XAML: XAML for animating pair of x-and-y coordinates

Snippet 9.5. XAML: XAML for shifting between colors

Snippet 9.6. XAML: XAML demonstrating From property within DoubleAnimation

Snippet 9.7. XAML: XAML demonstrating absence of From property within DoubleAnimation

Snippet 9.8. XAML: XAML demonstrating the To property within DoubleAnimation

Snippet 9.9. XAML: XAML demonstrating By property within a DoubleAnimation

Snippet 9.10. XAML: XAML demonstrating From property within DoubleAnimation

Snippet 9.11. XAML: Timeline with offset of 5 seconds

Snippet 9.12. XAML: Basic syntax of Storyboard element Snippet

Snippet 9.13. XAML: Syntax of Storyboard element with multiple animations

Snippet 9.15. XAML: Using the daisy-chain syntax to change the individual colors of a LinearGradientBrush

Snippet 9.18. C#: Starting a Storyboard from code-behind

Snippet 9.19. XAML: Defining an EventTrigger

Snippet 9.20. XAML: Adding the EventTrigger to the collection of Triggers

Snippet 9.21. XAML: Defining a Storyboard as an event trigger

Snippet 9.22. XAML: Creating a bouncing ball using key frames

Chapter 10. Giving it style

Snippet 10.2. C# XAML: The same SolidColorBrush defined as a resource at design time and runtime

Snippet 10.3. XAML: Referencing a resource at design time. Notice the use of the StaticResource keyword.

Snippet 10.4. XAML: An invalid use of a resource at design time

Snippet 10.5. C#: Retrieving the SolidColorBrush defined in snippet 10.4

Snippet 10.6. XAML: A Uri accessing a relative loose resource

Snippet 10.7. XAML: A Uri accessing a relative loose resource in a subdirectory

Snippet 10.8. XAML: An absolute Uri accessing a loose resource

Snippet 10.9. XAML: An absolute Uri accessing a loose resource in a subdirectory

Snippet 10.10. XAML: Referencing a relative content file at design time. This path is based on the structure shown in figure 10.2.

Snippet 10.11. C#: Referencing an assumed embedded file called embedded.png at runtime

Snippet 10.12. XAML Result: The brute-force approach to defining the visual properties of multiple elements. The text in the TextBox elements is entered at runtime.

Snippet 10.15. XAML: Defining the TargetType of a Style

Snippet 10.18. XAML Result: Using a ContentPresenter to display the Content of a ContentControl

Snippet 10.19. XAML Result: Using an ItemsPresenter to display the Items of an ItemsControl

Snippet 10.20. XAML Result: Declaring the ItemsPanel to arrange the Items of an ItemsControl

Snippet 10.24. XAML: Creating a Button that enlarges when a user hovers over it

Chapter 11. Enhancing the experience

Snippet 11.1. C#: Retrieving the isolated storage for a user

Snippet 11.2. C#: Retrieving explicitly named files and directories

Snippet 11.3. C#: Using the * wildcard character

Snippet 11.4. C#: Removing items from the isolated storage area

Snippet 11.5. C#: Creating a directory within the isolated storage area

Snippet 11.6. C#: Creating subdirectories within the isolated storage area

Snippet 11.8. C# Result: Requesting more isolated storage space. This specific dialog box asks the user if the Silverlight application can double the amount of available isolated storage.

Snippet 11.11. C#: Loading some XAML at runtime

Snippet 11.12. C#: Loading XAML with nested elements at runtime

Snippet 11.13. XAML Result: The XAML for the progress bar. The figure shown is simply for illustration. This illustration represents a task halfway through the completion.

Snippet 11.19. C#: Asynchronously requesting binary content using the WebClient class

Snippet 11.20. C#: Responding to the DownloadProgressChanged event. This event handler displays the progress of a download within an assumed TextBlock.

Snippet 11.21. C#: Canceling a download request. This snippet assumes a pretend Cancel button triggers the cancellation. In addition, this snippet assumes a WebClient variable called webClient exists.

Snippet 11.22. C#: This snippet uses a WebClient to call a JSON web service. This web service returns the weather conditions for a latitude and longitude in JSON.

Snippet 11.23. C#: Loading media content that has been downloaded on demand

Snippet 11.24. C#: Loading media content that has been downloaded on demand. Note that the somefont.TTF file doesn’t exist. This snippet is solely for demonstration.

Snippet 11.27. C#: Ensuring that a download was successful

Chapter 12. Share the light: Distribution and deployment

Snippet 12.3. c#: Registering a DependencyProperty with the dependency system

Snippet 12.4. c#: Registering a DependencyProperty with the dependency system

Snippet 12.5. XAML: Using the LockableTextBox

Snippet 12.6. XAML: The XAML of the NavigationService.xaml file

Snippet 12.8. C#: The code to go back to the list view

Snippet 12.9. c#: The code to load the details page into view

Snippet 12.13. XAML: The code for the Page.xaml file of a basic application

Snippet 12.14. XML: The manifest file used to deploy a basic Silverlight application

Snippet 12.15. JavaScript: Referencing a hosted Silverlight application