Defining Shapes, Text, and Media
Although Silverlight provides a subset of the XAML language available in WPF, the different declarative elements and attributes available can accomplish a lot and provide functionality that simply isn't available in the HTML language. For example, different types of shapes such as rectangles, ellipses, and lines can be defined and displayed using XAML. Different types of backgrounds can be defined for shapes as well including gradients, images, and media clips. Listing Three demonstrates how a rectangle, ellipse, and line can be defined within a Canvas container.
<Canvas xmlns="http://schemas.microsoft.com/client/2007"> <Canvas Canvas.Left="10" Canvas.Top="10" Height="300" Width="300"> <Rectangle Canvas.Top="25" Canvas.Left="25" Width="200" Height="150" Fill="Navy" /> <Ellipse Height="200" Width="200" Canvas.Left="10" Canvas.Top="20" Stroke="Black" StrokeThickness="3" Fill="Red"/> <Line X1="10" Y1="10" X2="10" Y2="300" Stroke="Black" StrokeThickness="3" /> </Canvas> </Canvas>
A gradient can be provided for the rectangle's background by using XAML elements, such as LinearGradientBrush in Listing Four. In this case, the gradient begins with White and gradually change to Red. Radial gradients can also be defined using the RadialGradientBrush XAML element to provide interesting elliptical effects.
<Canvas xmlns="http://schemas.microsoft.com/client/2007"> <Canvas Canvas.Left="10" Canvas.Top="10" Height="300" Width="300"> <Rectangle Canvas.Top="25" Canvas.Left="25" Width="200" Height="150" <Rectangle.Fill> <LinearGradientBrush StartPoint="0,0" EndPoint="1,1"> <GradientStop Color="White" Offset="0.0" /> <GradientStop Color="Red" Offset="0.75" /> </LinearGradientBrush> </Rectangle.Fill> </Rectangle> </Canvas> </Canvas>
Images can also be used as a shape's background by using the ImageBrush element, as in Listing Five. The ImageBrush element lets an image source be defined using the ImageSource attribute, and also allows control over how the image is drawn within its container through the Stretch attribute.
<Canvas xmlns="http://schemas.microsoft.com/client/2007"> <Canvas Canvas.Left="10" Canvas.Top="10" Height="300" Width="300"> <Rectangle Canvas.Top="25" Canvas.Left="25" Width="200" Height="150" <Rectangle.Fill> <ImageBrush ImageSource="Images/Fancy.jpg" Stretch="Uniform" /> </Rectangle.Fill> </Rectangle> </Canvas> </Canvas>
Valid ImageBrush Stretch attribute values are shown next:
- None. The content does not stretch to fill the output dimensions.
- Fill. The content is scaled to fit the output dimensions. Because the content's height and width are scaled independently, the original aspect ratio of the content might not be preserved.
- Uniform. The content is scaled to fit the output dimension and the aspect ratio of the content is preserved.
- UniformToFill. The content is scaled to fill the output area while preserving its original aspect ratio.
In addition to shapes, text can also be defined using the TextBlock element as in Listing Six. If you've used the ASP.NET Label control before then you'll probably find the TextBlock element to be similar in many ways.
<Canvas xmlns="http://schemas.microsoft.com/client/2007"> <TextBlock Name="tbCanvas" Canvas.Left="10" Canvas.Top="10" Foreground="Green" FontFamily="Tahoma" FontSize="14" FontWeight="Bold" Text="Hello World"> </TextBlock> </Canvas>
Silverlight 1.0 really shines when it comes to its ability to embed different types of media within a canvas. Using the MediaElement XAML tag you can easily allow MP3 or WMA files to be played or display WMV video files. Listing Seven is an example of using the MediaElement object to embed media in a XAML canvas. MediaElement lets the media source be defined using the Source element and can even be used to specify if the media should automatically play or not by using the AutoPlay attribute.
<Canvas xmlns="http://schemas.microsoft.com/client/2007" Width="577" Height="480"> <MediaElement Name="mediaElement" AutoPlay="True" Width="561" Height="396.5" Canvas.Left="8" Canvas.Top="8" Source="Video.wmv" Stretch="Fill" /> </Canvas>