XAML Namespaces

Declaring XAML namespaces to reference types

PDF for offline use
Related Articles:

Let us know how you feel about this


0/250

last updated: 2016-06

XAML uses the xmlns XML attribute for namespace declarations. This article introduces the XAML namespace syntax, and demonstrates how to declare a XAML namespace in order to access a type.

Overview

There are two XAML namespace declarations that are always within the root element of a XAML file. The first defines the default namespace, as shown in the following XAML code example:

xmlns="http://xamarin.com/schemas/2014/forms"

The default namespace specifies that elements defined within the XAML file with no prefix refer to Xamarin.Forms classes, such as ContentPage.

The second namespace declaration uses the x prefix, as shown in the following XAML code example:

xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"

XAML uses prefixes to declare non-default namespaces, with the prefix being used when referencing types within the namespace. The x namespace declaration specifies that elements defined within the XAML with a prefix of x are used for elements and attributes that are intrinsic to XAML (specifically the 2009 XAML specification).

The following table outlines the x namespace attributes supported by Xamarin.Forms:

Construct Description
x:Arguments Specifies constructor arguments for a non-default constructor, or for a factory method object declaration.
x:Class Specifies the namespace and class name for a class defined in XAML. The class name must match the class name of the code-behind file. Note that this construct can only appear in the root element of a XAML file.
x:FactoryMethod Specifies a factory method that can be used to initialize an object.
x:Key Specifies a unique user-defined key for each resource in a ResourceDictionary. The key's value is used to retrieve the XAML resource, and is typically used as the argument for the StaticResource markup extension.
x:Name Specifies a runtime object name for the XAML element. Setting x:Name is similar to declaring a variable in code.
x:TypeArguments Specifies the generic type arguments to the constructor of a generic type.

For more information about the x:Arguments, x:FactoryMethod, and x:TypeArguments attributes, see Passing Arguments in XAML.

In XAML, namespace declarations inherit from parent element to child element. Therefore, when defining a namespace in the root element of a XAML file, all elements within that file inherit the namespace declaration.

Declaring Namespaces for Types

Types can be referenced in XAML by declaring a XAML namespace with a prefix, with the namespace declaration specifying the Common Language Runtime (CLR) namespace name, and optionally an assembly name. This is achieved by defining values for the following keywords within the namespace declaration:

  • clr-namespace: – the CLR namespace declared within the assembly that contains the types to expose as XAML elements. This keyword is required.
  • assembly= – the assembly that contains the referenced CLR namespace. This value is the name of the assembly, without the file extension. The path to the assembly should be established as a reference in the project file that contains the XAML file that will reference the assembly. This keyword can be omitted if the clr-namespace value is within the same assembly as the application code that's referencing the types.

Note that the character separating the clr-namespace token from its value is a colon, whereas the character separating the assembly token from its value is an equal sign. The character to use between the two tokens is a semicolon.

The following code example shows a XAML namespace declaration:

<ContentPage ... xmlns:local="clr-namespace:HelloWorld" ...>
  ...
</ContentPage>

The local prefix is a convention used to indicate that the types within the namespace are local to the application. Alternatively, if the types are in a different assembly, the assembly name should also be defined in the namespace declaration, as demonstrated in the following XAML code example:

<ContentPage ... xmlns:behaviors="clr-namespace:Behaviors;assembly=BehaviorsLibrary" ...>
  ...
</ContentPage>

The namespace prefix is then specified when declaring an instance of a type from an imported namespace, as demonstrated in the following XAML code example:

<ListView ...>
  <ListView.Behaviors>
    <behaviors:EventToCommandBehavior EventName="ItemSelected" ... />
  </ListView.Behaviors>
</ListView>

Summary

This article introduced the XAML namespace syntax, and demonstrated how to declare a XAML namespace in order to access a type. XAML uses the xmlns XML attribute for namespace declarations, and types can be referenced in XAML by declaring a XAML namespace with a prefix.

Xamarin Workbook

If it's not already installed, install the Xamarin Workbooks app first. The workbook file should download automatically, but if it doesn't, just click to start the workbook download manually.