The popularity of declarative markup languages has gradually increased since the initial release of HTML. This shouldn't come as a surprise to anyone given that markup languages let information be presented to end users without requiring any knowledge of a programming language. For years HTML has served as the declarative language of choice for presenting information to end users through a browser and it certainly isn't going anywhere in the near future. However, new declarative languages such as Extensible Application Markup Language (XAML) have emerged, providing an alternate means for displaying data in more rich and engaging ways than HTML is capable of doing.
In this article, I introduce the XAML language and describe several ways it can be used in Silverlight applications. The topics covered will focus on functionality available in Silverlight 1.0. Future articles will introduce new XAML features available in Silverlight 2.0.
What Is XAML?
Extensible Application Markup Language (XAML) was originally created for the Windows Presentation Foundation (WPF) technology released with .NET 3.0. WPF and XAML provide a way to integrate designers into the application development process and create rich and interactive desktop (and even Web) applications that can bind to a variety of data sources. The release of Silverlight 1.0 brought XAML to the world of rich internet application development. Silverlight exposes a subset of the XAML language found in WPF that can be run directly in the browser once the Silverlight plug-in has been installed.
So what exactly is XAML and why should you learn it? If you have any background in HTML, then you'll quickly learn that XAML is like HTML is many ways (although XAML follows the rules defined in the XML specification which means that it's stricter than standard HTML). For example, HTML relies heavily on <div> elements to position child objects. Although XAML doesn't provide a <div> element, other elements such as <Canvas> can be used to accomplish the same type of task. If you've already learned HTML then you'll find that learning XAML is similar although XAML is capable of doing many more things from animating and transforming objects to dynamically displaying shapes with custom gradients.
Throughout the rest of this article I introduce several key aspects of XAML and demonstrate how you can use XAML elements and attributes in Silverlight applications.
Handling Layout in Silverlight
As mentioned previously, Silverlight 1.0 provides a Canvas element that acts as a container for child objects much like div elements act as containers in HTML. The Canvas element can be used to organize objects and define where and how the objects should be displayed. Silverlight 1.0 applications have a root Canvas element defined that includes a special XML namespace (see Listing One). This Canvas acts as the parent for all other objects placed into a Silverlight application.
<Canvas xmlns="http://schemas.microsoft.com/client/2007"> </Canvas>
Additional Canvas elements can be nested inside of the root Canvas element as well. This is especially useful when your application has several different visual sections such as a header, content area and footer. By using different Canvas elements you can group related child controls quite easily. Listing Two shows an example of a nested Canvas element.
<Canvas xmlns="http://schemas.microsoft.com/client/2007"> <Canvas Canvas.Left="10" Canvas.Top="10" Height="300" Width="300"> <!-- Child content goes here --> </Canvas> </Canvas>
Notice that it defines the height and width as well as where the child canvas should be positioned relative to its parent container. The Canvas.Left and Canvas.Top attributes are referred to as "attached properties" in the world of XAML programming. Their values are relative to the parent. In this case the child Canvas will be positioned 10 pixels from the left and 10 pixels from the top of the parent container.
By using Canvas elements appropriately within a Silverlight application you can easily position shapes, media, and text, and show/hide groups of objects easily.