.NET

Getting Started with XAML in Silverlight

By Dan Wahlin, March 24, 2008

Declarative languages such as XAML provide a means of displaying data in more rich and engaging ways than HTML

Dan Wahlin is a .NET development instructor and architecture consultant at Interface Technical Training. Dan founded the XML for ASP.NET Developers Web site, which focuses on using ASP.NET, XML, AJAX, and Web Services in Microsoft's .NET platform. Dan blogs at http://weblogs.asp.net/dwahlin and http://blogs.interfacett.com/dan-wahlins-blog.

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.

HTML can only do so much on its own which is why additional features such as JavaScript on the client-side and dynamic programming languages on the server-side have been created over the years. In Silverlight, 1.0 XAML relies on JavaScript in cases where dynamic events need to be raised and handled. Silverlight 2.0 ups the ante by adding support for several languages including VB.NET and C#.

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>

Listing One: Using the Silverlight 1.0 Canvas element in an XAML file. This file would have a .xaml extension.

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.

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.

Dr. Dobb's encourages readers to engage in spirited, healthy debate, including taking us to task.
However, Dr. Dobb's moderates all comments posted to our site, and reserves the right to modify or remove any content that it determines to be derogatory, offensive, inflammatory, vulgar, irrelevant/off-topic, racist or obvious marketing or spam. Dr. Dobb's further reserves the right to disable the profile of any commenter participating in said activities.

Video

This month's Dr. Dobb's Journal

This month,
Dr. Dobb's Journal is devoted to mobile programming. We introduce you to Apple's new Swift programming language, discuss the perils of being the third-most-popular mobile platform, revisit SQLite on Android
, and much more!