This short guide shows the minimal, practical steps to switch a small v11 app to use the XamlToCSharpGenerator (AXSG) source-generated XAML backend. Two workflows are described: using the published NuGet packages (recommended) and using a local checkout of the AXSG repository for development.
- .NET 10 SDK
- v11 packages already referenced in the sample
- (optional) local checkout of https://github.com/wieslawsoltes/XamlToCSharpGenerator for development and debugging
- Set the XAML compiler backend to
SourceGenand enable the AXSG compiler switch. - Add runtime integration (so extension methods like
UseAvaloniaSourceGeneratedXaml()are available). - Update
Program.csto opt-in the runtime loader. - Replace or experiment with some XAML using C# expression bindings.
-
Make a backup of your project before starting the migration, so you can easily revert if needed.
-
Add the AXSG package to the project:
dotnet add package XamlToCSharpGenerator- Update your project file (
csproj) to select the SourceGen backend (minimal example):
<PropertyGroup>
<AvaloniaXamlCompilerBackend>SourceGen</AvaloniaXamlCompilerBackend>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="XamlToCSharpGenerator" Version="*" />
</ItemGroup>- Update
Program.cswith a call toUseAvaloniaSourceGeneratedXaml()to use the AXSG runtime Bootstrap extension:
using XamlToCSharpGenerator.Runtime;
public static AppBuilder BuildAvaloniaApp() =>
AppBuilder.Configure<App>()
.UsePlatformDetect()
.WithInterFont()
.UseAvaloniaSourceGeneratedXaml()
.LogToTrace();- Build and run:
dotnet build -c Debug
dotnet run --project src- AXSG supports explicit expressions using
{= ... }and shorthand expression forms{Name}and interpolated forms{$'...{expr}...'}. - Prefer single-quoted string literals inside expressions to avoid XML quoting headaches, e.g.
{= InputText != null ? InputText.ToUpper() : 'empty' }.
Note that this repo shows that in the csharp-expressions branch, and just some simple syntax. A full specification is being developed by Microsoft for MAUI and AXSG will align with that spec as it evolves.
Once you finish the initial onboarding steps, your project is ready to work with the new AXSG extension for Visual Studio Code, so get started with more fun!
- Things might be missing or broken: see CONTRIBUTE.md for local-analyzer troubleshooting and fixes, and contribute detailed reports back to AXSG developers to help them improve the experience.