This article is more than 1 year old

Shine on Silverlight and Windows with XAML

Learn now, pays later

So what's a Markup Extension? This is an expression within braces that generally returns an object instance. In Silverlight XAML this is either a static resource or a dynamic binding. You could define a LinearGradientBrush as a resource, and then use that within a Markup Extension using attribute syntax:

<Rectangle Height="20" Width="50" Fill="{StaticResource MyGradientBrush}" />

Another XAML feature is called Attached Properties. This is where a class defines static properties that can be attached to objects of a different class. For example, this XAML specifies where a button appears in a grid layout, presuming it is contained within a Grid element:

<Button Content="Click me" Grid.Row="1" Grid.Column="0" />

At runtime, the Grid element has code that iterates through its children, checks the attached properties, and positions them accordingly. Attached properties are a special case of a feature called dependency properties, which are calculated at runtime and can change dynamically.

You can also extend XAML with custom objects. The main requirement is that classes used in XAML must have a parameterless constructor. The procedure is straightforward. Define a class; make sure your application has a reference to the assembly containing the class; then add a namespace declaration for the assembly. You can then define elements in XAML that map to your class, and at runtime these will become object instances. XAML has a curious story when it comes to formatted text, especially in Silverlight. In one sense it is rather limited. XAML has no understanding of common formats such as HTML, CSS or RTF, let alone the fancy new OOXML. Silverlight developers have to interact with the browser DOM in order to display HTML.

XAML preserved in Silverlight

No escaping it: Silverlight .XAP bundle preserves the original XAML

That said, XAML with WPF actually is a document format. The full WPF has an element called FlowDocument and rich formatting capabilities. Silverlight lacks FlowDocument, but does have a TextBlock with basic formatting options via the inline <Run> object. It also supports the Glyph element. This is interesting because it is the core element in XPS, Microsoft's invented-here alternative to Adobe's PDF.

XPS uses a subset of XAML to describe fixed layouts. In consequence, and with some compromises, you can use Silverlight to display XPS.

The bottom line is that XAML is a way of programming .NET declaratively. Its more intricate features improve the mapping between XAML and .NET. The result is we have design tools like Microsoft's Expression Blend and a clean separation between UI objects and program code, which is a considerable achievement.

As ever there's a downside, and with Microsoft it's the classic: this is thoroughly proprietary, and the schema issues make it difficult to validate with standard XML tools. If this doesn't deter you, then check out Microsoft's XAML and WPF documentation is here

More about

TIP US OFF

Send us news


Other stories you might like