Dela via


x:Class attribute

[This article is for Windows 8.x and Windows Phone 8.x developers writing Windows Runtime apps. If you’re developing for Windows 10, see the latest documentation]

Configures XAML compilation to join partial classes between markup and code-behind. The code partial class is defined in a separate code file, and the markup partial class is created by code generation during XAML compilation.

XAML attribute usage

<object x:Class="namespace.classname"...>
  ...
</object>

XAML values

Term Description

namespace

Optional. Specifies a namespace that contains the partial class identified by classname. If namespace is specified, a dot (.) separates namespace and classname. If namespace is omitted, classname is assumed to have no namespace.

classname

Required. Specifies the name of the partial class that connects the loaded XAML and your code-behind for that XAML.

 

Remarks

x:Class can be declared as an attribute for any element that is the root of a XAML file/object tree and is being compiled by build actions, or for the Application root in the application definition of a compiled application. Declaring x:Class on any element other than a page root or application root, and under any circumstances for a XAML file that is not compiled with the Page build action, results in a compile-time error.

The class used as x:Class cannot be a nested class.

The value of the x:Class attribute must be a string that specifies the fully qualified name of a class. You can omit namespace information so long as that is how the code-behind is structured also (your class definition starts at the class level). The code-behind file for a page or application definition must be within a code file that is included as part of the project. The code-behind class must be public. The code-behind class must be partial.

CLR language rules

Although your code-behind file can be a C++ file, there are certain conventions that still follow the CLR language form, so that there is no difference in the XAML syntax. In particular, the separator between the namespace and classname components of any x:Class value is always a dot ("."), even though the separator between namespace and classname in the C++ code file associated with the XAML is "::". If you declare nested namespaces in C++, then the separator between the successive nested namespace strings should also be "." rather than "::" when you specify the namespace part of the x:Class value.

XAML overview

XAML namescopes

C#, VB, and C++ project templates for Windows Runtime apps