XAML uses the
xmlns XML attribute for namespace declarations. There are two XAML namespace declarations that are always within the root element of a XAML file. The first defines the default namespace:
The default namespace specifies that elements defined within the XAML file with no prefix refer to .NET Multi-platform App UI (.NET MAUI) classes, such as
The second namespace declaration uses the
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 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 constructs supported by .NET MAUI:
||Specifies constructor arguments for a non-default constructor, or for a factory method object declaration.|
||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.|
||Specifies the type of the object that the XAML element, and it's children, will bind to.|
||Specifies a factory method that can be used to initialize an object.|
||Specifies the access level for generated fields for named XAML elements.|
||Specifies a unique user-defined key for each resource in a
||Specifies a runtime object name for the XAML element. Setting
||Specifies the generic type arguments to the constructor of a generic type.|
For more information about the
x:DataType attribute, see Compiled bindings. For more information about the
x:FieldModifier attribute, see Field modifiers. For more information about the
x:FactoryMethod attributes, see Pass arguments. For more information about the
x:TypeArguments attribute, see Generics.
In addition to the constructs listed above, .NET MAUI also includes markup extensions that can be consumed through the
x namespace prefix. For more information, see Consume XAML Markup Extensions.
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.
Declare 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:
using:– 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 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 app code that's referencing the types.
The character separating the
using 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:MyMauiApp"> ... </ContentPage>
Alternatively, this can be written as:
<ContentPage ... xmlns:local="using:MyMauiApp"> ... </ContentPage>
local prefix is a convention used to indicate that the types within the namespace are local to the app. Alternatively, if the types are in a different assembly, the assembly name should also be defined in the namespace declaration:
<ContentPage ... xmlns:controls="clr-namespace:Controls;assembly=MyControlLibrary" ...> ... </ContentPage>
The namespace prefix is then specified when declaring an instance of a type from an imported namespace:
<controls:Expander IsExpanded="True"> ... </controls:Expander>
For information about defining a custom namespace schema, see Custom namespace schemas.
Submit and view feedback for