Simple XAML Done Simply

Markup languages combine information about the UI elements (text, images, etc.) with attribute information (boldness, opacity, etc.). In HTML you might write the following:

<b><i>XAML is a markup language</i></b>

This would lead to a web browser displaying the text as follows:

XAML is a markup language

The text is augmented by markup that tells the browser to render it in bold italics.

The same combination of UI elements and markup applies to XAML, making it a very convenient way to approach the presentation layer of your Windows applications. Consider this simple XAML example:

<Page xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" >
   <Grid>
      <Label
          Content="Hello World"
          FontFamily="Verdana"
          FontSize="32pt" />
   </Grid>
</Page>

This displays "Hello World" in 32-point Verdana, as shown in Figure 2-2.

Simple XAML example

Figure 2-2. Simple XAML example

The <Label /> tag represents a standard System.Windows.Controls label. Therefore, you can expect that through the attributes of the tag, you will have access to all the members exposed by the Label type. You can set them declaratively (as shown here with the Content, FontFamily, and FontSize attributes), or you can manipulate them programmatically at runtime.

In general, you will find that XAML obeys completely the XML syntax rules.

Tip

Naming each element that you create using ...

Get Programming .NET 3.5 now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.