#73 – Multiple Namespaces Using Prefixes

You can use multiple namespaces in a XAML file, allowing you to use names from multiple namespaces in the same file.

When you use more than one namespace, you need to associate all but one of the namespaces with a prefix, which you use to qualify names belonging to the prefixed namespaces.  You can include one namespace (the default namespace) that does not include a prefix.

In WPF, two namespaces are typically used:

<Application x:Class="WpfApplication1.App"
                 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                 StartupUri="MainWindow.xaml">

In this example, the second namespace uses the “x” prefix, declaring the prefix as part of the xmlns attribute.  All names qualified with the “x” prefix, e.g. x:Class, belong to this namespace.

The first namespace in the example does not include a prefix.  All names in the XAML file not qualified with a prefix are assumed to belong to this namespace–e.g. StartupUri.

Advertisements

#72 – XAML Namespaces

In XAML, a namespace is a collection of names, identified by a URI reference, which are used in XAML documents as element types and attribute names.  (See W3C definition of namespace).

In addition to being just a collection of allowed element and attribute names, a XAML namespace maps this set of names to the corresponding .NET classes in one or more .NET namespaces.

A namespace is uniquely identified by a URI that is typically included as an attribute in the root element in a XAML file, using the xmlns attribute.  The root element must include at least one xmlns attribute.  This attribute defines the namespace that contains definitions of elements and attributes in the XAML file.

The default namespace used in WPF is http://schemas.microsoft.com/winfx/2006/xaml/presentation.

<Window x:Class="WpfApplication1.MainWindow"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             Title="MainWindow" Height="350" Width="525">

See also – XAML Namespaces and Namespace Mapping for WPF XAML

#71 – XAML Files Have A Single Root Element

Each XAML file has exactly one root element, the outermost XAML element.  In WPF, the root element is typically one of:

  • <Page> – A page of content that can be hosted in a browser (System.Windows.Controls.Page)
  • <Window> – A WPF window (System.Windows.Window)
  • <Application> – A WPF application (System.Windows.Application)
  • <ResourceDictionary> – A collection of resources (System.Windows.ResourceDictionary)
  • <UserControl> – A user-defined control (System.Windows.Controls.UserControl)

Root elements also typically include attributes that indicate the namespace(s) used to interpret the content of the XAML file.

#70 – Specifying Event Handlers in XAML

In addition to setting property values, you can also wire up event handlers in XAML.  You do this by specifying the name of the event and the name of the method in your code-behind that should be called when the event fires.

Here’s an example:

<Button Name="btnClickMe" Content="Click Me" Height="23" Width="75" Click="Button_Click"/>

This wires up this button’s Click event to the Button_Click method in the containing window’s code-behind (.cs) file.

private void Button_Click(object sender, RoutedEventArgs e)
{
}

Adding the event handler in XAML is equivalent to doing it in code as follows:

this.btnClickMe.Click += new RoutedEventHandler(Button_Click);

#69 – Internet Explorer Can Interpret XAML Directly

XAML is not code to be executed, but a definition of a user interface that can be parsed by a tool that understands it.  You can load a .xaml file directly into Internet Explorer and it will render the user interface elements defined by the XAML.

For example, consider the following XAML fragment.

<StackPanel xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" >
    <Button Content="Click Me" Height="23" Width="75"/>
</StackPanel>

You can save this fragment to a .xaml file and load it into Internet Explorer.  The browser will then interpret the XAML and render the user interface that it describes:

#68 – Nesting XAML Elements Expresses Containment

Nesting elements is a fundamental part of XML syntax.  In XAML, nested elements typically expresses containment:

the .NET object corresponding to the child element is contained in the .NET object corresponding to the parent element.

Here’s an example.  The XAML fragment below defines a StackPanel control that contains two Button controls.

<StackPanel Height="100" Name="stackPanel1" Width="200">
    <Button Content="Button" Height="23" Name="button1" Width="75" />
    <Button Content="Button" Height="23" Name="button2" Width="75" />
</StackPanel>

The structure of the XAML elements (Button as child of StackPanel) mirrors the structure of the actual controls in the user interface (the buttons are children of the StackPanel).

#67 – XAML Attributes Map to Property Values

In the same way that XAML elements map to instances of .NET types, XAML attributes map to properties of those types.

In the example below, we create a new instance of a Button and then specify values for the following properties: Content, Height, Width, Name and Background.

<Button Content="Press Me" Height="23" Name="button2" Width="75" Background="#FFE3E316" />

Note that we set each property by specifying a string, despite the Button object’s properties having different types:

  • Content – object
  • Height – double
  • Width – double
  • Name – string
  • Background – System.Windows.Media.Brush

When the XAML file is processed, the string values will be converted to the appropriate types.