This article is more than 1 year old

Shine on Silverlight and Windows with XAML

Learn now, pays later

Web bling tone Extensible Application Markup Language, or XAML, lies at the heart of Microsoft's rich-client strategy. The user interface for both Windows Presentation Foundation (WPF) and Silverlight, which is mostly a subset of WPF, is typically defined in XAML.

It is, therefore, something Windows developers will have to get to grips with if they want to stay in tune with Microsoft's latest and - arguably - most ambitious technology roadmap. Even if WPF itself never really takes off, Silverlight is almost certainly going to be big in Microsoft circles.

But what is XAML? It is easy to call it an XML language for defining a graphical user interface (GUI), but that is not quite right. Microsoft calls it "a system for representing structured information," which is about as generic as you can get, and reveals in its document XAML Object Mapping Specification 2006, here, that although XML is "a common format for XAML... any physical representation may be used."

Editing XAML in Visual Studio 2008

Visual Studio 2008 brings IntelliSense to editing XAML

It is also important to distinguish the XAML system from specific XAML vocabularies, such as WPF. Microsoft also uses XAML for Windows Workflow Foundation (WWF), using a different vocabulary that has nothing to do with GUIs. A better stab at defining XAML is to call it a declarative programming language for .NET, or a means of representing a graph of .NET objects.

It turns out that XAML has an uneasy relationship with XML. All the XAML documents you are likely to see are valid XML, but validating them as XAML is not easy. Microsoft explained back in 2006 why it could not develop a satisfactory XML schema for WPF.

The problem is not the core XAML, usually the namespace with an "X" prefix in a XAML document, but the WPF vocabulary, usually the default namespace. Visual Studio 2005 used a huge XamlPresentation2006.xsd that worked mostly but not always. Visual Studio 2008 replaced this with a language service like that used to support IntelliSense for C# and Visual Basic. While this works better, it is unfortunate for anyone wishing to use other XML tools for XAML editing.

XAML has features that simplify the way it maps to the .NET API. Several of these relate to setting property values. Typically XML does this with attributes, but this is awkward when a property is a complex object. XAML supports property elements, which use dot syntax to define objects that are properties of the containing object. Here is an example:

<Rectangle Height="20" Width="50" >
  <Rectangle.Fill>
   <SolidColorBrush Color="Gold" />
  </Rectangle.Fill>
</Rectangle>

It so happens that in this case you can do the same thing with a conventional attribute:

<Rectangle Height="20" Width="50" Fill="Gold" />

How does the XAML parser know that the string "Gold" must be converted to a SolidColorBrush? This is done through a Type Converter, a class that in this example converts a string to a new SolidColorBrush object.

Type Converters extend the number of properties that can be set as attributes, but do not deal with every possibility. In this example, if you wanted to use a different kind of brush, you would have to use the verbose property element syntax, or possibly a Markup Extension.

More about

TIP US OFF

Send us news


Other stories you might like