doc/articles/features/auto-codebehind.md
Uno Platform can automatically generate minimal code-behind for XAML pages that don't have a developer-authored code-behind class. This eliminates boilerplate when your page only needs a default constructor that calls InitializeComponent().
When you create a XAML file with an x:Class attribute but no corresponding code-behind class, the build system automatically generates a partial class with a constructor that calls InitializeComponent(). This lets you create XAML-only pages without writing any C# boilerplate.
You need two files:
MainPage.xaml
<Page x:Class="MyApp.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<TextBlock Text="Hello, World!" />
</Page>
MainPage.xaml.cs
namespace MyApp
{
public partial class MainPage : Page
{
public MainPage()
{
this.InitializeComponent();
}
}
}
You only need the XAML file:
MainPage.xaml
<Page x:Class="MyApp.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<TextBlock Text="Hello, World!" />
</Page>
The code-behind is generated automatically at build time. The generated class inherits from the correct base type based on the XAML root element.
The following XAML root elements are supported with their corresponding base types:
| Root Element | Generated Base Type |
|---|---|
Page | Microsoft.UI.Xaml.Controls.Page |
UserControl | Microsoft.UI.Xaml.Controls.UserControl |
Window | Microsoft.UI.Xaml.Window |
ContentDialog | Microsoft.UI.Xaml.Controls.ContentDialog |
Application | Microsoft.UI.Xaml.Application |
ResourceDictionary | Microsoft.UI.Xaml.ResourceDictionary |
Custom root elements using using: or clr-namespace: xmlns prefixes are also supported.
x:Class attribute.x:Class does not already exist in the compilation (no developer-authored class defining it), a minimal partial class is generated.x:Class type already exists, no code is generated — the existing class takes full precedence.this.InitializeComponent().Auto code-behind works best for XAML pages that:
x:Bind or {Binding} for data bindingx:Bind for event handlers pointing to a view modelYou still need a manual .xaml.cs file when you need to:
x:Bind)OnNavigatedTo, etc.)You can disable auto code-behind generation for the entire project by setting the UnoGenerateCodeBehind MSBuild property in your .csproj:
<PropertyGroup>
<UnoGenerateCodeBehind>false</UnoGenerateCodeBehind>
</PropertyGroup>
The default value is true (enabled).
You can override the project-level setting for individual XAML files using the UnoGenerateCodeBehind item metadata:
<!-- Disable auto code-behind for a specific file -->
<Page Update="MainPage.xaml" UnoGenerateCodeBehind="false" />
<!-- Enable auto code-behind for a specific file even when globally disabled -->
<Page Update="SimplePage.xaml" UnoGenerateCodeBehind="true" />
Per-file metadata always takes precedence over the project-level property.
| Code | Severity | Description |
|---|---|---|
| UXAML0004 | Warning | The x:Class attribute value is malformed. The value must include a namespace (e.g., MyApp.MainPage, not just MainPage). |
This feature works on all platforms supported by Uno Platform:
| Feature | Windows | Android | iOS | Web (WASM) | macOS | Linux (Skia) |
|---|---|---|---|---|---|---|
| Auto code-behind generation | ✔ | ✔ | ✔ | ✔ | ✔ | ✔ |
| Per-file configuration | ✔ | ✔ | ✔ | ✔ | ✔ | ✔ |
| Project-level configuration | ✔ | ✔ | ✔ | ✔ | ✔ | ✔ |
On Uno Platform targets, code-behind generation is handled by the integrated XAML source generation pipeline. On WinUI (Windows) targets, a standalone incremental source generator provides the same functionality.