XAML in a Nutshell - Basic Rules for XAML Elements
(Page 2 of 4 )
XAML developers need to be aware that in order for application logic developers to access specific XAML elements, the elements must be named using either the Name or ID attribute. Developers will use one of these attributes to reference and manipulate the element directly from code. In Example 3-1, the
Button ’s Name attribute was declared as MyButton . The same name was then used in both code examples to reference and directly access the object.
There are three basic rules to follow when declaring XAML elements:
- XAML is case-sensitive. Element and attribute names must be properly cased.
- All attribute values, regardless of data type, must be enclosed in double quotes.
- The resulting XML must be well-formed.
The basic syntax for declaring XAML elements and attributes is:
<ElementName AttributeName="Value" AttributeName="Value" ... />
A simple login user interface, as shown in Figure 3-1, could be described with the code in Example 3-5, which illustrates these basic rules. Note the careful atten tion to case in declaring elements and attributes, the enclosure of all attribute values (regardless of underlying data type) in double quotes, and the fact that all elements are well-formed and closed with an end tag.

Figure 3-1. A simple user-login XAML page
Example 3-5. A simple login user interface
<StackPanel
xmlns=http://schemas.microsoft.com/ winfx/avalon/2005
HorizontalAlignment="Left"
Margin="10">
<Label
Margin="5 "
Content="Username" />
<TextBox
Margin="5"
BorderBrush="Blue"
BorderThickness="1"
Background="AliceBlue"
Foreground="Black"
Width="200"/>
<Label
Margin="5"
Content="Password" />
<PasswordBox
Margin="5"
BorderBrush="Blue"
BorderThickness="1"
Background="AliceBlue"
Foreground="Black"
Width="200" />
<Button
Margin="10"
Background="AliceBlue"
Foreground="Black"
Width="100"
Height="20"
Content="Submit" />
</StackPanel>
Formatting is a matter of style and corporate standards. The format for the exam ples in this book was chosen because it is readable and clearly displays the nesting of elements in more complex markup. Elements can be declared all on one line, or attribute declarations can be split across lines; formatting is completely up to you. Because XAML is compiled into BAML before deployment, the amount of space taken up by elements in a XAML file is irrelevant. There are no advantages to using less space by declaring elements on a single line and no disadvantages to the formatting used in this book. The elements will become binary representations before deployment, and the whitespace will have no impact on the footprint of finished applications.
Clearly, XAML is comprised of elements and their attributes. The rest of this chapter will examine each of these concepts in depth.
Next: 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.
|
|