This is a [C# `SourceGenerator`](https://devblogs.microsoft.com/dotnet/introducing-c-source-generators/) built for generating strongly-typed references to controls with `x:Name` (or just `Name`) attributes declared in XAML (or, in `.axaml`).The source generator will look for the `xaml` (or `axaml`) file with the same name as your partial C# class subclasse of `Avalonia.INambe` and parses the XML markup, finds all XML tags with `x:Name` attributes and generates the C# code.
This is a [C# `SourceGenerator`](https://devblogs.microsoft.com/dotnet/introducing-c-source-generators/) built for generating strongly-typed references to controls with `x:Name` (or just `Name`) attributes declared in XAML (or, in `.axaml`).The source generator will look for the `xaml` (or `axaml`) file with the same name as your partial C# class that is a subclass of `Avalonia.INamed` and parses the XAML markup, finds all XAML tags with `x:Name` attributes and generates the C# code.
### Getting Started
### Getting Started
Add reference the source generator by installing a NuGet package:
In order to get started, just install the NuGet package:
```
```
dotnet add package XamlNameReferenceGenerator
dotnet add package XamlNameReferenceGenerator
@ -20,16 +20,28 @@ Or, if you are using [submodules](https://git-scm.com/docs/git-submodule), you c
```xml
```xml
<ItemGroup>
<ItemGroup>
<!-- Remember to include XAML files via <AdditionalFiles>,
otherwise C# source generators won't see the XAML files.
If you are using a NuGet package, this is done automatically. -->
After installing the NuGet package, declare your view class as `partial`. Typed C# references to Avalonia controls declared in XAML files will be generated for all classes that inherit from the `Avalonia.INamed` interface (including those classes that inherit from `Window`, `UserControl`, `ReactiveWindow<T>`, `ReactiveUserControl<T>`). For example, for the following XAML markup:
A new C# public property named `UserNameTextBox` of type `TextBox` will be generated:
```cs
```cs
using Avalonia.Controls;
using Avalonia.Controls;
@ -39,22 +51,22 @@ public partial class SignUpView : Window
public SignUpView()
public SignUpView()
{
{
AvaloniaXamlLoader.Load(this);
AvaloniaXamlLoader.Load(this);
UserNameTextBox.Text = "Joseph"; // Coolstuff!
UserNameTextBox.Text = "Joseph"; // Coolstuff!
}
}
}
}
```
```
If you just want specific classes:
### Usage (Opt-in)
add at your csproj this lines:
If you don't want to generate typed `x:Name` references for every window or user control in your assembly, you can always turn off this default behavior by setting the `AvaloniaNameGenerator` MsBuild property to `false` in your C# project file (`.csproj`). Just add the following property group to your `<Project />` tag:
Finally, decorate yours class with attribute `[GenerateTypedNameReferences]`:
From now on, the source generator will process only those files that are decorated with the `[GenerateTypedNameReferences]` attribute. Other window or user control classes will be left unchanged, and you won't have to mark them as `partial`.
```cs
```cs
using Avalonia.Controls;
using Avalonia.Controls;
@ -65,7 +77,7 @@ public partial class SignUpView : Window