XAML syntax
Based on XML, defines a set of tags and attributes that represent interface objects and their presentations.
XML syntax
XAML is an XML-derived tag language. Graphic components are defined by public or private tags with attributes.
Example tag with content:
<TextBlock> Un texte </TextBlock>
and without content:
<TextBlock />
Attributes are similar to variables assigned by a value. In XML, these values are enclosed in quotation marks, unlike the contents of text or one or more other tags.
For example, we specify the name t1 with the Name attribute:
<TextBlock Name="t1" /> <TextBlock Name="t1"> Un texte </TextBlock>
You can see that tags can contain other tags, and even attributes can become tags or vice versa, for example:
<TextBlock Text="Un texte" />
Property syntax
An object property, meaning what characterizes it, can be written as an attribute. For example, the background color property of a rectangle is written with the Fill attribute:
<Rectangle Fill="Red" />
To describe complex properties, XAML has an alternative format called "Property element syntax," which extends the XML syntax and gives the point a new meaning.
Attribute value must be a string in XML. In XAML, it can be another language object.
But the object is not directly assigned to the attribute with an equal sign, it is associated with a point in the syntax inherent in XAML, which has the form:
nomElement.nomPropriété
Consider an example Rectangle object and the Fill property, which is the fill color, the attribute is converted to tags:
<Rectangle> <Rectangle.Fill> </Rectangle.Fill> </Button>
To add tags and attributes to the Fill property, such as a photo texture, as shown in this tutorial.
Another example is represented by a language specification, that is, the button with which the drop-down list is associated:
<Button> <Button.ContextMenu> <ContextMenu> <MenuItem> Ouvrir </MenuItem> <MenuItem> Fermer </MenuItem> </ContextMenu> </Button.ContextMenu> </Button>
You can see that ContextMenu, which is a list of menus, becomes a property of the button thanks to "Button.ContextMenu," and declares itself inside the description of the button.
The content of the tag can be treated as a property. Thus, the button text is a property that is written to the content or as the value of the Text or Content attribute depending on the object:
<TextBlock Text="Un texte" />
Namespaces
Namespaces are specified as attributes of the global container itself in a XAML, Canvas, Windows, or Page file. These are the specified URLs that will be given in the examples and correspond to the type of XAML definition.
Example:
<Canvas xmlns="http://schemas.microsoft.com/client/2007" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
For namespaces other than the default (first line), each element of this namespace must be prefixed (as x above). In this case, we will deliver:
x:nomElement
for each namespace element x.
Attached Properties
This is the XAML concept. The syntax is the same as for the property elements shown above, the property name is associated with the type name (not the element name such as Button).
nomType.nomPropriété
The goal is to add properties to the type. Elements of this type can have these properties.
Events related to
In XAML, you can define an event by type, while event handlers will be attached to objects represented by tags (for example, Button). The syntax is always the same:
nomType.nomEvènement
Expand language
You can extend the XAML language using a special syntax: an extension is placed between {}, consisting of a class name followed by an instance.
Example taken from language specification:
<TextBlock Style="{StaticResource MonStyle}"> Un texte </TextBlock>
The StaticResource class contains the definitions you added, and the MonStyle instance becomes the property of the button. We will be able to use Button.MonStyle in the button definition and take advantage of the new features implemented in the class.
Root characteristics and tags: Canvas, Page, Application
.Case sensitivity
XAML is case sensitive. The initial letters of the words must be preserved.
This is not necessarily the case for attribute values, so True and True are valid if the parent recognizes them.
Gaps
Extra spaces are ignored, as well as special characters, such as tab code, equivalent to a space.
Root tag
Like any XML document, the XAML definition must be included in a single tag, the so-called root element.
A WPF page uses a Page or Windows tag as a container.
For Silverlight, it's Canvas.
For the application, this is the Application .
Further information
- XML language.