Udostępnij za pośrednictwem


x:Class Attribute

Configures XAML compilation to join partial classes between markup and code-behind. The code partial class is defined in a separate code file in a Common Language Specification (CLS) language, whereas the markup partial class is created by code generation during XAML compilation.

XAML Attribute Usage

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

XAML Values

namespace

Optional. Specifies a CLR namespace that contains the partial class identified by classname. If namespace is specified, a dot (.) separates namespace and classname.

classname

Required. Specifies the CLR 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 element tree and is being compiled (where the XAML is included in a project with Page build action), 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, will result in a compile-time error.

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

x:Class is optional in the sense that is it is entirely legal to have a XAML page with no code-behind at all, but if your page declares event handling attribute values, or instantiates custom elements where the defining classes are in the code-behind class, providing the x:Class reference (or x:Subclass) to the appropriate class for code-behind is ultimately required. 

The value of the x:Class attribute must be a string that specifies the fully qualified name of a class. For simple applications, you can omit CLR namespace information so long as that is how the code-behind is structured also (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 that produces a compiled application. You must follow name rules for CLR classes; for details, see Type Definitions. By default, the code-behind class must be public, but can be defined to be a different access level by using the x:ClassModifier Attribute.

Note that this meaning of the x:Class attribute value is specific to the WPF XAML implementation. Other XAML implementations outside of WPF might not use managed code, and thus might use a different class resolution formula.

See Also

Concepts

XAML and Custom Classes

Code-Behind and XAML

Reference

x:Subclass Attribute

x:ClassModifier Attribute