XAML Overview
Extensible Application Markup Language (XAML) serves as the primary declarative language for defining user interfaces in Windows Presentation Foundation (WPF). It fulfills a role similar to the combination of HTML, CSS, and JavaScript within web development, acting as the bridge between visual designers and backend developers.
As a purely declarative language, XAML is used exclusively to instantiate UI elements, define visual layouts, and configure animations without requiring procedural logic. This strict separation enforces a decoupled architecture where the presentation layer resides in XAML files while business logic occupies the code-behind files. This "high cohesion, low coupling" structure allows designers to work effectively in tools like Microsoft Blend while developers utilize Visual Studio for the backend logic.
Creating a WPF Solution
To begin development, a new project must be initialized using the WPF App (.NET Framework) template within the development environment. Upon creation, the solution opens to the primary markup file, typically named MainWindow.xaml.
The resulting solution structure contains several key components:
- Properties: Contains application resources such as icons, images, and static string values, alongside configuration settings.
- References: Lists external dependencies and .NET Framework class libraries required by the project.
- App.xaml: Represents the application entry point. It defines global application resources and specifies the main window that launches at startup. The associated
App.xaml.csfile contains the procedural code for this entry point. - MainWindow.xaml: Defines the primary application window. Its layout and visual elements are declared here, while
MainWindow.xaml.cscontains the specific logic for that window.
XAML Syntax and Elements
XAML is an XML-based language and adheres to standard XML structural rules, organizing elements into a hierarchical tree. The fundamental building blocks of a XAML element include the Tag, Attributes, and Content.
Attributes defined in XAML tags typically map directly to properties on the corresponding object instances. The hierarchical relationship of tags—whether nested or parallel—mirrors the containment or ownership relationships of the objects they represent.
<Window x:Class="WpfApplication.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:custom="clr-namespace:WpfApplication"
mc:Ignorable="d"
Title="Main Window" Height="350" Width="525">
<Grid>
</Grid>
</Window>
In this structure, the <Window> tag acts as the root element, instantiating the window object, while the nested <Grid> tag defines a layout panel contained within the window.
Root Element Attributes
- x:Class: This directive instructs the XAML parser to generate a partial class in the specified language (C#). It links the markup file to the code-behind logic defined in the
WpfApplication.MainWindowclass. - xmlns (XML Namespace): These declarations prevent naming conflicts by mapping specific prefixes to assemblies. When class names collide across different libraries, the namespace prefix disambiguates them.
- xmlns:x: This namespace is reserved for language-level parsing rules intrinsic to XAML. The prefix 'x' is a convention used to access keywords like
x:Classorx:Name. - Title, Height, Width: These attributes directly set standard properties on the Window object, defining its appearance and dimensions.
Default Namespaces
A namespace declared without a prefix is considered the "default namespace." There can be only one default namespace per document, and it should generally point to the most frequently used library to reduce verbosity in the markup.
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
In the standard configuration:
- The first declaration (default namespace) maps to the WPF presentation assemblies, containing UI controls and layout logic.
- The second declaration (prefixed with
x:) maps to the XAML language parsing system.
Although these namespaces appear as standard URLs, they function as hardcoded identifiers recognized by the XAML compiler. These strings are not retrieved over the web; instead, they instruct the parser to load specific .NET assemblies and CLR namespaces required to process the markup.