Skip to content

lextm/test-sourcegenerator

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Migrating a Hello World sample to the AXSG (XAML Source Generator)

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.

Prerequisites

Overview

  • Set the XAML compiler backend to SourceGen and enable the AXSG compiler switch.
  • Add runtime integration (so extension methods like UseAvaloniaSourceGeneratedXaml() are available).
  • Update Program.cs to opt-in the runtime loader.
  • Replace or experiment with some XAML using C# expression bindings.

Steps: use NuGet

  1. Make a backup of your project before starting the migration, so you can easily revert if needed.

  2. Add the AXSG package to the project:

dotnet add package XamlToCSharpGenerator
  1. Update your project file (csproj) to select the SourceGen backend (minimal example):
<PropertyGroup>
	<AvaloniaXamlCompilerBackend>SourceGen</AvaloniaXamlCompilerBackend>
</PropertyGroup>

<ItemGroup>
	<PackageReference Include="XamlToCSharpGenerator" Version="*" />
</ItemGroup>
  1. Update Program.cs with a call to UseAvaloniaSourceGeneratedXaml() to use the AXSG runtime Bootstrap extension:
using XamlToCSharpGenerator.Runtime;

public static AppBuilder BuildAvaloniaApp() =>
		AppBuilder.Configure<App>()
				.UsePlatformDetect()
				.WithInterFont()
				.UseAvaloniaSourceGeneratedXaml()
				.LogToTrace();
  1. Build and run:
dotnet build -c Debug
dotnet run --project src

Features: using C# expressions in XAML

  • 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.

Features: Hot Reload, Live Preview, Visual Designer, and MCP

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!

Common troubleshooting

  • 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.