This page covers the most common migration mistake when enabling AXSG in an existing Avalonia app: leaving a hand-written InitializeComponent() method that still calls AvaloniaXamlLoader.Load(this).
Keep constructor calls like:
public MainWindow()
{
InitializeComponent();
}
But do not keep an unconditional hand-written method like:
private void InitializeComponent()
{
global::Avalonia.Markup.Xaml.AvaloniaXamlLoader.Load(this);
}
when AXSG is enabled.
For class-backed XAML, AXSG generates an InitializeComponent(bool loadXaml = true) method in the generated partial class. That is the method expected to build the object graph from generated code.
If your code-behind also declares a parameterless InitializeComponent(), then the constructor call InitializeComponent(); binds to the hand-written method, not the generated one. In that case AXSG output exists, but your view never uses it.
Remove the hand-written fallback method entirely:
public MainWindow()
{
InitializeComponent();
}
Keep the fallback only when AXSG is not the active backend:
public MainWindow()
{
InitializeComponent();
}
#if !AXAML_SOURCEGEN_BACKEND
private void InitializeComponent()
{
global::Avalonia.Markup.Xaml.AvaloniaXamlLoader.Load(this);
}
#endif
This is the recommended portable pattern for libraries and apps that still need to build on both AXSG and non-AXSG paths.
The guarded fallback remains appropriate when:
Avoid these patterns:
private void InitializeComponent() => AvaloniaXamlLoader.Load(this);InitializeComponent();This guidance is separate from AppBuilder bootstrap. You still need:
.UseAvaloniaSourceGeneratedXaml()
in the app startup path. The generated InitializeComponent method and the AXSG runtime bootstrap solve different problems:
InitializeComponent builds class-backed object graphs from generated code.UseAvaloniaSourceGeneratedXaml() enables the AXSG runtime loader, registries, and runtime integration surfaceBoth are part of the normal supported setup.
If a class-backed view seems to ignore AXSG output:
AvaloniaXamlCompilerBackend=SourceGen..UseAvaloniaSourceGeneratedXaml().InitializeComponent().obj/... and confirm the class-backed partial includes AXSG-generated InitializeComponent.