#86 – The Class Attribute Points to Your Code-Behind

The root element in a XAML file can have an x:Class attribute that specifies how to find the code-behind associated with the class being defined in the markup.

Examples:

 <Application x:Class="WpfApplication1.App"

or

 <Window x:Class="WpfApplication1.MainWindow"

When you build your project, the XAML is compiled and two things occur: 1) the XAML is converted into tokenized binary BAML; and 2) code is generated (e.g. C#) which will serve as a partial class that matches the partial class for your code-behind.  Notice that the value of the Class attribute matches the name of the corresponding class.

So in a default project, after building, you’ll get the following code files:

  • Main application
    • App.g.cs – code generated from App.xaml
    • App.xaml.cs – your code-behind for App class
  • Main window
    • MainWindow.g.cs – code generated from MainWindow.xaml
    • MainWindow.xaml.cs – your code-behind for MainWindow class

Advertisement