[![NuGet Stats](https://img.shields.io/nuget/v/XamlNameReferenceGenerator.svg)](https://www.nuget.org/packages/XamlNameReferenceGenerator) [![downloads](https://img.shields.io/nuget/dt/XamlNameReferenceGenerator)](https://www.nuget.org/packages/XamlNameReferenceGenerator) ![Build](https://github.com/avaloniaui/Avalonia.NameGenerator/workflows/Build/badge.svg) ![License](https://img.shields.io/github/license/avaloniaui/Avalonia.NameGenerator.svg) ![Size](https://img.shields.io/github/repo-size/avaloniaui/Avalonia.NameGenerator.svg) > **Warning** This tool hasn't been extensively tested, so use at your own risk. ### C# `SourceGenerator` for Typed Avalonia `x:Name` References 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 In order to get started, just install the NuGet package: ``` dotnet add package XamlNameReferenceGenerator ``` Or, if you are using [submodules](https://git-scm.com/docs/git-submodule), you can reference the generator as such: ```xml ``` ### Usage 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 classes referenced by the `x:Class` directive in XAML files. For example, for the following XAML markup: ```xml ``` A new C# partial class named `SignUpView` with a single `public` property named `UserNameTextBox` of type `TextBox` will be generated in the `Sample.App` namespace. We won't see the generated file, but we'll be able to access the generated property as shown below: ```cs using Avalonia.Controls; namespace Sample.App { public partial class SignUpView : Window { public SignUpView() { AvaloniaXamlLoader.Load(this); UserNameTextBox.Text = "Joseph"; // Cool stuff! } } } ``` ### What do the generated sources look like? For the [`SignUpView` view class](https://github.com/avaloniaui/Avalonia.NameGenerator/blob/main/src/Avalonia.NameGenerator.Sandbox/Views/SignUpView.xaml) from [the sandbox project](https://github.com/avaloniaui/Avalonia.NameGenerator/tree/main/Avalonia.NameGenerator.Sandbox), we get the following generated output: ```cs // using Avalonia.Controls; namespace Avalonia.NameGenerator.Sandbox.Views { partial class SignUpView { internal global::Avalonia.NameGenerator.Sandbox.Controls.CustomTextBox UserNameTextBox => this.FindControl("UserNameTextBox"); public global::Avalonia.Controls.TextBlock UserNameValidation => this.FindControl("UserNameValidation"); private global::Avalonia.Controls.TextBox PasswordTextBox => this.FindControl("PasswordTextBox"); internal global::Avalonia.Controls.TextBlock PasswordValidation => this.FindControl("PasswordValidation"); internal global::Avalonia.Controls.TextBox ConfirmPasswordTextBox => this.FindControl("ConfirmPasswordTextBox"); internal global::Avalonia.Controls.TextBlock ConfirmPasswordValidation => this.FindControl("ConfirmPasswordValidation"); internal global::Avalonia.Controls.Button SignUpButton => this.FindControl("SignUpButton"); internal global::Avalonia.Controls.TextBlock CompoundValidation => this.FindControl("CompoundValidation"); } } ``` ### Why do I need this? The typed `x:Name` references might be useful if you decide to use e.g. [ReactiveUI code-behind bindings](https://www.reactiveui.net/docs/handbook/data-binding/): ```cs // UserNameValidation and PasswordValidation are auto generated. public partial class SignUpView : ReactiveWindow { public SignUpView() { AvaloniaXamlLoader.Load(this); this.WhenActivated(disposables => { this.BindValidation(ViewModel, x => x.UserName, x => x.UserNameValidation.Text) .DisposeWith(disposables); this.BindValidation(ViewModel, x => x.Password, x => x.PasswordValidation.Text) .DisposeWith(disposables); }); } } ```