A cross-platform UI framework for .NET
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
Artyom V. Gorchakov 93b6c0624a
docs: Document the new Generator Options (#33)
5 years ago
.github/workflows housekeeping: Rename to Avalonia.NameGenerator (#16) 5 years ago
external feature: Support x:FieldModifier Directive (#20) 5 years ago
src docs: Document the new Generator Options (#33) 5 years ago
.gitignore feature: Add XamlXRawNameReferenceXamlParser (#4) 5 years ago
.gitmodules fix: Use https in .gitmodules 5 years ago
LICENSE housekeeping: Rename to Avalonia.NameGenerator (#16) 5 years ago
README.md docs: Document the new Generator Options (#33) 5 years ago
version.json docs: Document the new Generator Options (#33) 5 years ago

README.md

NuGet Stats downloads Build License Size

C# SourceGenerator for Typed Avalonia x:Name References

This is a C# SourceGenerator 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, you can reference the generator as such:

<ItemGroup>
    <!-- Remember to ensure XAML files are included via <AdditionalFiles>,
         otherwise C# source generator won't see XAML files. -->
    <AdditionalFiles Include="**\*.xaml"/>
    <ProjectReference Include="..\Avalonia.NameGenerator\Avalonia.NameGenerator.csproj"
                      OutputItemType="Analyzer"
                      ReferenceOutputAssembly="false" />
</ItemGroup>

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:

<Window xmlns="https://github.com/avaloniaui"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        x:Class="Sample.App.SignUpView">
    <TextBox x:Name="UserNameTextBox" x:FieldModifier="public" />
</Window>

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:

using Avalonia.Controls;

namespace Sample.App
{
    public partial class SignUpView : Window
    {
        public SignUpView()
        {
            AvaloniaXamlLoader.Load(this);
            UserNameTextBox.Text = "Joseph"; // Cool stuff!
        }
    }
}

Why do I need this?

The typed x:Name references might be useful if you decide to use e.g. ReactiveUI code-behind bindings:

// UserNameValidation and PasswordValidation are auto generated.
public partial class SignUpView : ReactiveWindow<SignUpViewModel>
{
    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);
        });
    }
}

Advanced Usage

The x:Name generator can be configured via MsBuild properties that you can put into your C# project file (.csproj). Using such options, you can configure the generator behavior, the default field modifier, namespace and path filters. The generator supports the following options:

  • AvaloniaNameGeneratorBehavior
    Possible values: OnlyProperties, InitializeComponent
    Determines if the generator should generate get-only properties, or the InitializeComponent method.

  • AvaloniaNameGeneratorDefaultFieldModifier
    Possible values: internal, public, private, protected
    The default field modifier that should be used when there is no x:FieldModifier directive specified.

  • AvaloniaNameGeneratorFilterByPath
    Posssible format: glob_pattern, glob_pattern;glob_pattern
    The generator will process only XAML files with paths matching the specified glob pattern(s).
    Example: */Views/*View.xaml, *View.axaml;*Control.axaml

  • AvaloniaNameGeneratorFilterByNamespace
    Posssible format: glob_pattern, glob_pattern;glob_pattern
    The generator will process only XAML files with base classes' namespaces matching the specified glob pattern(s).
    Example: MyApp.Presentation.*, MyApp.Presentation.Views;MyApp.Presentation.Controls

The default values are given by:

<Project Sdk="Microsoft.NET.Sdk">
    <PropertyGroup>
        <AvaloniaNameGeneratorBehavior>OnlyProperties</AvaloniaNameGeneratorBehavior>
        <AvaloniaNameGeneratorDefaultFieldModifier>internal</AvaloniaNameGeneratorDefaultFieldModifier>
        <AvaloniaNameGeneratorFilterByPath>*</AvaloniaNameGeneratorFilterByPath>
        <AvaloniaNameGeneratorFilterByNamespace>*</AvaloniaNameGeneratorFilterByNamespace>
    </PropertyGroup>
    <!-- ... -->
</Project>

What do the generated sources look like?

For SignUpView, we get the following generated output when the source generator is in the OnlyProperties mode:

// <auto-generated />

using Avalonia.Controls;

namespace Avalonia.NameGenerator.Sandbox.Views
{
    partial class SignUpView
    {
        internal global::Avalonia.NameGenerator.Sandbox.Controls.CustomTextBox UserNameTextBox => this.FindControl<global::Avalonia.NameGenerator.Sandbox.Controls.CustomTextBox>("UserNameTextBox");
        public global::Avalonia.Controls.TextBlock UserNameValidation => this.FindControl<global::Avalonia.Controls.TextBlock>("UserNameValidation");
        private global::Avalonia.Controls.TextBox PasswordTextBox => this.FindControl<global::Avalonia.Controls.TextBox>("PasswordTextBox");
        internal global::Avalonia.Controls.TextBlock PasswordValidation => this.FindControl<global::Avalonia.Controls.TextBlock>("PasswordValidation");
        internal global::Avalonia.Controls.TextBox ConfirmPasswordTextBox => this.FindControl<global::Avalonia.Controls.TextBox>("ConfirmPasswordTextBox");
        internal global::Avalonia.Controls.TextBlock ConfirmPasswordValidation => this.FindControl<global::Avalonia.Controls.TextBlock>("ConfirmPasswordValidation");
        internal global::Avalonia.Controls.Button SignUpButton => this.FindControl<global::Avalonia.Controls.Button>("SignUpButton");
        internal global::Avalonia.Controls.TextBlock CompoundValidation => this.FindControl<global::Avalonia.Controls.TextBlock>("CompoundValidation");
    }
}

If you enable the InitializeComponent source generator mode, you get:

// <auto-generated />

using Avalonia.Controls;
using Avalonia.Markup.Xaml;

namespace Sample.App
{
    partial class SampleView
    {
        internal global::Avalonia.NameGenerator.Sandbox.Controls.CustomTextBox UserNameTextBox { get; set; }
        public global::Avalonia.Controls.TextBlock UserNameValidation { get; set; }
        private global::Avalonia.Controls.TextBox PasswordTextBox { get; set; }
        internal global::Avalonia.Controls.TextBlock PasswordValidation { get; set; }
        internal global::Avalonia.Controls.ListBox AwesomeListView { get; set; }
        internal global::Avalonia.Controls.TextBox ConfirmPasswordTextBox { get; set; }
        internal global::Avalonia.Controls.TextBlock ConfirmPasswordValidation { get; set; }
        internal global::Avalonia.Controls.Button SignUpButton { get; set; }
        internal global::Avalonia.Controls.TextBlock CompoundValidation { get; set; }

        public void InitializeComponent(bool loadXaml = true)
        {
            if (loadXaml)
            {
                AvaloniaXamlLoader.Load(this);
            }

            UserNameTextBox = this.FindControl<global::Avalonia.NameGenerator.Sandbox.Controls.CustomTextBox>("UserNameTextBox");
            UserNameValidation = this.FindControl<global::Avalonia.Controls.TextBlock>("UserNameValidation");
            PasswordTextBox = this.FindControl<global::Avalonia.Controls.TextBox>("PasswordTextBox");
            PasswordValidation = this.FindControl<global::Avalonia.Controls.TextBlock>("PasswordValidation");
            AwesomeListView = this.FindControl<global::Avalonia.Controls.ListBox>("AwesomeListView");
            ConfirmPasswordTextBox = this.FindControl<global::Avalonia.Controls.TextBox>("ConfirmPasswordTextBox");
            ConfirmPasswordValidation = this.FindControl<global::Avalonia.Controls.TextBlock>("ConfirmPasswordValidation");
            SignUpButton = this.FindControl<global::Avalonia.Controls.Button>("SignUpButton");
            CompoundValidation = this.FindControl<global::Avalonia.Controls.TextBlock>("CompoundValidation");
        }
    }
}