Building a WPF Application Using XAML

The Extensible Application Markup Language (XAML) is an XML-based grammar for describing trees of .
NET objects. XAML and WPF go hand in hand, but XAML can be used to describe any non-generic type
that supports a default constructor. A XAML description of a tree of objects maps to a runtime object model.

In the case of WPF, XAML descriptions are a direct mapping to the properties and events of the type being
described. The opening element defines the name of the class with XML attributes used to set the property
values and event handler names. The
Name attribute refers to the name of the instance of the class. Consider
the following (functionally identical) manners to declare a WPF Button type.


<!-- Defining a WPF Button in XAML -->
<Button Name = "btnClickMe" Height = "40" Width = "100"
      Content = "Click Me" Click = "btnClickMe_Click"/>




// Defining the same WPF Button in C# code.
// (VB code would be similar)
Button btnClickMe = new Button();
btnClickMe.Height = 40;
btnClickMe.Width = 100;
btnClickMe.Content = "Click Me";
btnClickMe.Click += new RoutedEventHandler(btnClickMe_Click);


It is possible to build an entire WPF application using nothing more than XAML descriptions. Recall that
XAML is not limited to the description of GUI elements. Any .NET object can be described in XAML,
including a WPF’s
Application type, which has no associated user interface.

Here is a XAML description of a Window-derived type. The XML namespaces are required and simply
qualify the various WPF XAML definitions (more information will come later in this chapter on XAML
namespaces). Notice that code such as event handlers can be wrapped within
CDATA sections contained
within
<x:Code> elements. It is not recommended to inject code within a XAML file, as it violates the
separation of concerns.


<!-- Here is the Window definition -->
<Window x:Class = "SimpleXamlApp.MainWindow"
xmlns = "http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x = "http://schemas.microsoft.com/winfx/2006/xaml"
Title = "My Xaml App" Height = "200" Width = "300"
WindowStartupLocation = "CenterScreen">

<!--Define the button content -->
<Button Width = "133" Height = "24" Name = "btnExitApp"
        Click = "btnExitApp_Clicked" Content = "Exit Application"/>

<!--The C# implementation of the button's Click event handler. -->
<x:Code>
  <![CDATA[
  private void btnExitApp_Clicked(object sender, RoutedEventArgs e)
  {
    // Get a handle to the current app and shut it down.
    Application.Current.Shutdown();
  }
  ]]>
</x:Code>
</Window>



Here would be the XAML description of the Application type of the WPF application. The StartupUri
property is essentially the XAML equivalent of defining a Main() method. This property is set to the *.xaml
file containing the definition of the main window of the application.


<!-- The main method seems to be missing!
   However, the StartupUri attribute is the
   functional equivalent. -->
<Application x:Class = "SimpleXamlApp.MyApp"
xmlns = "http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x = "http://schemas.microsoft.com/winfx/2006/xaml"
StartupUri = "MainWindow.xaml" Exit ="AppExit">

<x:Code>
  <![CDATA[
  private void AppExit(object sender, ExitEventArgs e)
  {
    MessageBox.Show("App has exited");
  }
  ]]>
</x:Code>

</Application>



These XAML files can now be compiled into a .NET assembly using msbuild.exe and a related build script.
Msbuild.exe is a command line tool that enables complex builds. In fact, Visual Studio makes use of msbuild.
exe in the background. The same build script can be processed by
msbuild.exe or Visual Studio.

Msbuild.exe makes use of various ‘targets’ that contain instructions on how to transform XAML definitions
into equivalent .NET code. Your next lab will illustrate how to do so. Until then, dig deeper into the syntax of
XAML itself.
Building a WPF Application Using XAML
Table of Contents
Copyright (c) 2008.  Intertech, Inc. All Rights Reserved.  This information is to be used exclusively as an
online learning aid.  Any attempts to copy, reproduce, or use for training is strictly prohibited.
Courseware
Training Resources
Tutorials
Services