Channels ▼
RSS

.NET

Getting Started with XAML in Silverlight


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.

<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>
Listing Two: Defining multiple Canvas elements in a XAML file.

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.


Related Reading


More Insights






Currently we allow the following HTML tags in comments:

Single tags

These tags can be used alone and don't need an ending tag.

<br> Defines a single line break

<hr> Defines a horizontal line

Matching tags

These require an ending tag - e.g. <i>italic text</i>

<a> Defines an anchor

<b> Defines bold text

<big> Defines big text

<blockquote> Defines a long quotation

<caption> Defines a table caption

<cite> Defines a citation

<code> Defines computer code text

<em> Defines emphasized text

<fieldset> Defines a border around elements in a form

<h1> This is heading 1

<h2> This is heading 2

<h3> This is heading 3

<h4> This is heading 4

<h5> This is heading 5

<h6> This is heading 6

<i> Defines italic text

<p> Defines a paragraph

<pre> Defines preformatted text

<q> Defines a short quotation

<samp> Defines sample computer code text

<small> Defines small text

<span> Defines a section in a document

<s> Defines strikethrough text

<strike> Defines strikethrough text

<strong> Defines strong text

<sub> Defines subscripted text

<sup> Defines superscripted text

<u> Defines underlined text

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.

 
Disqus Tips To upload an avatar photo, first complete your Disqus profile. | View the list of supported HTML tags you can use to style comments. | Please read our commenting policy.
 

Video