
Appendix B: Introduction to XAML | 411 |
is that it is an XML document with elements, attributes, and namespaces. Each of the items you see has special meaning, as will be discussed in the following sections.
Elements as Classes
For XAML to be meaningful as code, elements must be associated with classes. The Window element in Listing
.MainWindow, specified by the x:Class attribute. The x prefix aliases the http://schemas
.microsoft.com/winfx/2006/xaml namespace, where the Class attribute is defined. By mapping the element to a class, you allow VS to compile the XAML into code that runs. Notice that the default namespace is http://schemas.microsoft.com/winfx/2006/xaml/ presentation, which defines how each of the elements without prefixes will be compiled to code. The important fact to realize here is that when writing XAML, you are creating a document that will be translated into executable code for you at compile time.
Attributes as Properties
Title, Height, and Width are attributes of the Window element in Listing
MainWindow class will have Title, Height, and Width properties. Each of the properties will be set with the value assigned to their corresponding attributes.
Executing the XAML Document
Remember that this is not a tutorial on WPF and that the focus needs to be on understanding how XAML works. Nevertheless, it’s informative to see what happens when XAML is compiled and executed. Press F5 or click the Start Debugging button on the toolbar to run this program. What you’ll see is a window similar to Figure