XAML in a Nutshell
(Page 1 of 4 )
Extensible Application Markup Language (XAML) is a .NET technology. It's a markup language that can be used to help create desktop applications, web pages, and printable documents. This article, the first of three parts, introduces you to the syntax. It is excerpted from chapter 3 of
The Basics of XAML, written by Lori A. MacVittie (O'Reilly, 2006; ISBN: 0596526733). Copyright © 2006 O'Reilly Media, Inc. All rights reserved. Used with permission from the publisher. Available from booksellers or direct from O'Reilly Media.
XAML is an XML-based markup language. Given that, it shares many properties with other XML documents, such as case sensitivity and having to be well-formed. XAML has some specific syntax peculiarities designed for easing the declaration of specific types of elements. It provides abbreviated markup syntax for specific types of elements that take advantage of the underlying Common Language Runtime (CLR) class constructors.
This chapter will examine the core XAML syntax, as well as some of the peculiarities of its abbreviated markup syntax, in preparation for understanding more complex concepts in later chapters.
Core XAML Syntax
XAML generally follows XML syntax rules, just as any other XML-based markup language does. Each XAML element has a name and one or more attributes. Attributes correspond directly to object properties, and the name of the XAML element exactly matches the name of a CLR class definition.
XAML is pure markup, which means that while the names of event handlers are specified as attributes, you must implement the actual logic of the event handler in code. If you’re familiar with ASP.NET programming techniques, then you’ll be familiar with the term codebehind , which refers to the code “behind” a XAML interface element that is responsible for providing application logic such as event handlers. It can be implemented in either C# or VB.NET. In both cases, the code can be placed inline in the XAML file, although this contradicts best practices in separating the presentation and application logic layers.
How does this work? Every event in XAML can be assigned to a codebehind handler, which is implemented in a supported .NET language. For example, it’s a common task to do something when a Button is clicked. So, first a Button is declared with the XAML code shown in Example 3-1.
Example 3-1. XAML Button declaration
<Button
OnClick="ButtonClickedHandler"
Name="MyButton"
Width="50"
Content="Click Me!" />
Then, a corresponding codebehind handler is declared, and, when the Button is clicked, the handler is automatically executed (Examples 3-2 and 3-3).
Example 3-2. Button OnClick handler in C#
void ButtonClickedHandler(object sender, RoutedEventArgs eventArgs)
{
MyButton.Width = 100;
MyButton.Content = "Thank you!";
}
Example 3-3. Button OnClick handler in VB.NET
Sub ButtonClickedHandler(ByVal sender As Object,
ByVal eventArgs as RoutedEventArgs )
MyButton.Width = 100
MyButton.Content = "Thank you!"
End Sub
In both Examples 3-2 and 3-3, the handler will change the width of the Button from 50 to 100 and change the text displayed on it from “Click Me!” to “Thank you!”. All XAML attributes can be manipulated within code because they are simply XML representations of actual CLR class attributes. You could just as easily change the button’s background color, height, and even its position in code, just as you could in a traditional Windows application.
It is also acceptable to inline code in the XAML file by specifying the <x:Code> element. All inline code must be enclosed in the <CDATA[...]]> tag to ensure that the parser does not try to interpret the code. The XAML code from Example 3-1 and the C# code from Example 3-2 yield Example 3-4.
Example 3-4. Inlining code within a XAML file
<Button
OnClick="ButtonClickedHandler"
Name="MyButton"
Width="50"
Content="Click Me!" />
<x:Code>
<![CDATA
void ButtonClickedHandler(object sender, RoutedEventArgs eventArgs)
{
MyButton.Width = 100;
MyButton.Content = "Thank you!";
}
Example 3-4. Inlining code within a XAML file (continued)
]]>
</x:Code>
Application developers familiar with C# or VB.NET will immediately grasp the concept of codebehind and inline code and will be able to apply their existing skills to develop the code that drives the application.
Next: Basic Rules for XAML Elements >>
More XML Articles
More By O'Reilly Media
|
This article is excerpted from chapter 3 of The Basics of XAML, written by Lori A. MacVittie (O'Reilly, 2006; ISBN: 0596526733). Check it out today at your favorite bookstore. Buy this book now.
|
|