Skip to content

Namespaces

Jasper Zanjani edited this page Aug 13, 2020 · 2 revisions

A XAML file usually declares a default XAML namespace in its root element. This default namespace defines elements that can be declared without qualifying them by a prefix (e.g. x:). XAML namespaces apply only to the specific element on which they are declared and its children, which explains why they are typically placed on the root element. ref

Most XAML files have two xmlns declarations

  • xmlns maps a default XAML namespace
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml

x

xmlns:x maps a separate XAML namespace for XAML-defined language elements to the x: prefix:

  • x:Name uniquely identified object elements for access to the object from code-behind :point_right: x:Name attribute
  • x:Key sets a unique key for each resource in a ResourceDictionary 👉 x:Key attribute
  • x:Class specifies the CLR namespace and class name for the class that provides code-behind for a XAML page. The x:Class directive configures XAML markup compilation to join partial classes between markup and code-behind. In this example it can be seen how it refers to the MainPage class within the Project namespace. 👉 x:Class directive

Language elements are commonly used features necessary for Windows Runtime apps. For example, to join any code-behind to a XAML file through a partial class, you must name that class as the x:Class attribute in the root element of the XAML file.

// C#  
namespace Project
{
  public sealed partial class MainPage : Page
  {
    public MainPage()
    {
      this.InitializeComponent();
    }
    ...
  }
}
<!-- XAML -->
<Page
  x:Class="Project.MainPage"
  ...>
...
</Page>

mc

mc: indicates and supports a markup compatibility mode for reading XAML.

xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
Clone this wiki locally