{"id":13629539,"url":"https://github.com/AvaloniaUI/Avalonia.NameGenerator","last_synced_at":"2025-04-17T09:34:35.907Z","repository":{"id":39575629,"uuid":"307158045","full_name":"AvaloniaUI/Avalonia.NameGenerator","owner":"AvaloniaUI","description":"Generates x:Name refs to Avalonia XAML controls.","archived":true,"fork":false,"pushed_at":"2023-06-28T05:42:47.000Z","size":213,"stargazers_count":86,"open_issues_count":1,"forks_count":7,"subscribers_count":6,"default_branch":"main","last_synced_at":"2025-03-30T15:33:00.097Z","etag":null,"topics":["avalonia","avaloniaui","csharp","dotnet","hacktoberfest","sourcegenerator","xaml","xamlx"],"latest_commit_sha":null,"homepage":"https://www.nuget.org/packages/XamlNameReferenceGenerator/","language":"C#","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/AvaloniaUI.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null}},"created_at":"2020-10-25T17:51:18.000Z","updated_at":"2025-01-11T17:08:56.000Z","dependencies_parsed_at":"2024-01-23T21:35:33.017Z","dependency_job_id":null,"html_url":"https://github.com/AvaloniaUI/Avalonia.NameGenerator","commit_stats":null,"previous_names":["worldbeater/xamlnamereferencegenerator"],"tags_count":22,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/AvaloniaUI%2FAvalonia.NameGenerator","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/AvaloniaUI%2FAvalonia.NameGenerator/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/AvaloniaUI%2FAvalonia.NameGenerator/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/AvaloniaUI%2FAvalonia.NameGenerator/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/AvaloniaUI","download_url":"https://codeload.github.com/AvaloniaUI/Avalonia.NameGenerator/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":249331880,"owners_count":21252661,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["avalonia","avaloniaui","csharp","dotnet","hacktoberfest","sourcegenerator","xaml","xamlx"],"created_at":"2024-08-01T22:01:13.145Z","updated_at":"2025-04-17T09:34:35.634Z","avatar_url":"https://github.com/AvaloniaUI.png","language":"C#","funding_links":[],"categories":["Source Generators","Do not want to test 112 ( old ISourceGenerator )"],"sub_categories":["XAML / WPF / Avalonia","1. [ThisAssembly](https://ignatandrei.github.io/RSCG_Examples/v2/docs/ThisAssembly) , in the [EnhancementProject](https://ignatandrei.github.io/RSCG_Examples/v2/docs/rscg-examples#enhancementproject) category"],"readme":"\u003e **Warning** The development of this package has been [**moved**](https://github.com/AvaloniaUI/Avalonia/pull/10407) to the [main AvaloniaUI repository](https://github.com/avaloniaUI/Avalonia). Please consider opening issues or pull requests in the [main AvaloniaUI repository](https://github.com/avaloniaUI/Avalonia). Head over to [AvaloniaUI Gitter](https://gitter.im/AvaloniaUI/Avalonia) or check [AvaloniaUI Discussions](https://github.com/AvaloniaUI/Avalonia/discussions) if you have any questions regarding the migration process.\n\n[![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)\n\n### C# `SourceGenerator` for Typed Avalonia `x:Name` References \n\nThis 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.\n\n### Getting Started\n\nIn order to get started, just install the NuGet package:\n\n```\ndotnet add package XamlNameReferenceGenerator\n```\n\nOr, if you are using [submodules](https://git-scm.com/docs/git-submodule), you can reference the generator as such:\n\n```xml\n\u003cItemGroup\u003e\n    \u003c!-- Remember to ensure XAML files are included via \u003cAdditionalFiles\u003e,\n         otherwise C# source generator won't see XAML files. --\u003e\n    \u003cAdditionalFiles Include=\"**\\*.xaml\"/\u003e\n    \u003cProjectReference Include=\"..\\Avalonia.NameGenerator\\Avalonia.NameGenerator.csproj\"\n                      OutputItemType=\"Analyzer\"\n                      ReferenceOutputAssembly=\"false\" /\u003e\n\u003c/ItemGroup\u003e\n```\n\n### Usage\n\nAfter 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:\n\n```xml\n\u003cWindow xmlns=\"https://github.com/avaloniaui\"\n        xmlns:x=\"http://schemas.microsoft.com/winfx/2006/xaml\"\n        x:Class=\"Sample.App.SignUpView\"\u003e\n    \u003cTextBox x:Name=\"UserNameTextBox\" x:FieldModifier=\"public\" /\u003e\n\u003c/Window\u003e\n```\n\nA 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:\n\n```cs\nusing Avalonia.Controls;\n\nnamespace Sample.App\n{\n    public partial class SignUpView : Window\n    {\n        public SignUpView()\n        {\n            // This method is generated. Call it before accessing any\n            // of the generated properties. The 'UserNameTextBox'\n            // property is also generated.\n            InitializeComponent();\n            UserNameTextBox.Text = \"Joseph\";\n        }\n    }\n}\n```\n\n\u003cimg src=\"https://hsto.org/getpro/habr/post_images/d9f/4aa/a1e/d9f4aaa1eb450f5dd2fca66631bc16a0.gif\" /\u003e\n\n### Why do I need this?\n\nThe 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/):\n\n```cs\n// UserNameValidation and PasswordValidation are auto generated.\npublic partial class SignUpView : ReactiveWindow\u003cSignUpViewModel\u003e\n{\n    public SignUpView()\n    {\n        InitializeComponent();\n        this.WhenActivated(disposables =\u003e\n        {\n            this.BindValidation(ViewModel, x =\u003e x.UserName, x =\u003e x.UserNameValidation.Text)\n                .DisposeWith(disposables);\n            this.BindValidation(ViewModel, x =\u003e x.Password, x =\u003e x.PasswordValidation.Text)\n                .DisposeWith(disposables);\n        });\n    }\n}\n```\n\n### Advanced Usage\n\n\u003e Never keep a method named `InitializeComponent` in your code-behind view class if you are using the generator with `AvaloniaNameGeneratorBehavior` set to `InitializeComponent` (this is the default value). The private `InitializeComponent` method declared in your code-behind class hides the `InitializeComponent` method generated by `Avalonia.NameGenerator`, see [Issue 69](https://github.com/AvaloniaUI/Avalonia.NameGenerator/issues/69). If you wish to use your own `InitializeComponent` method (not the generated one), set `AvaloniaNameGeneratorBehavior` to `OnlyProperties`.\n\nThe `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:\n\n- `AvaloniaNameGeneratorBehavior`  \n    Possible values: `OnlyProperties`, `InitializeComponent`  \n    Default value: `InitializeComponent`  \n    Determines if the generator should generate get-only properties, or the `InitializeComponent` method.\n\n- `AvaloniaNameGeneratorDefaultFieldModifier`  \n    Possible values: `internal`, `public`, `private`, `protected`  \n    Default value: `internal`  \n    The default field modifier that should be used when there is no `x:FieldModifier` directive specified.\n\n- `AvaloniaNameGeneratorFilterByPath`  \n    Posssible format: `glob_pattern`, `glob_pattern;glob_pattern`  \n    Default value: `*`  \n    The generator will process only XAML files with paths matching the specified glob pattern(s).  \n    Example: `*/Views/*View.xaml`, `*View.axaml;*Control.axaml`\n\n- `AvaloniaNameGeneratorFilterByNamespace`  \n    Posssible format: `glob_pattern`, `glob_pattern;glob_pattern`  \n    Default value: `*`  \n    The generator will process only XAML files with base classes' namespaces matching the specified glob pattern(s).  \n    Example: `MyApp.Presentation.*`, `MyApp.Presentation.Views;MyApp.Presentation.Controls`\n\n- `AvaloniaNameGeneratorViewFileNamingStrategy`  \n    Possible values: `ClassName`, `NamespaceAndClassName`  \n    Default value: `NamespaceAndClassName`  \n    Determines how the automatically generated view files should be [named](https://github.com/AvaloniaUI/Avalonia.NameGenerator/issues/92).\n\nThe default values are given by:\n\n```xml\n\u003cProject Sdk=\"Microsoft.NET.Sdk\"\u003e\n    \u003cPropertyGroup\u003e\n        \u003cAvaloniaNameGeneratorBehavior\u003eInitializeComponent\u003c/AvaloniaNameGeneratorBehavior\u003e\n        \u003cAvaloniaNameGeneratorDefaultFieldModifier\u003einternal\u003c/AvaloniaNameGeneratorDefaultFieldModifier\u003e\n        \u003cAvaloniaNameGeneratorFilterByPath\u003e*\u003c/AvaloniaNameGeneratorFilterByPath\u003e\n        \u003cAvaloniaNameGeneratorFilterByNamespace\u003e*\u003c/AvaloniaNameGeneratorFilterByNamespace\u003e\n        \u003cAvaloniaNameGeneratorViewFileNamingStrategy\u003eNamespaceAndClassName\u003c/AvaloniaNameGeneratorViewFileNamingStrategy\u003e\n    \u003c/PropertyGroup\u003e\n    \u003c!-- ... --\u003e\n\u003c/Project\u003e\n```\n\n![](https://user-images.githubusercontent.com/6759207/107812261-7ddfea00-6d80-11eb-9c7e-67bf95d0f0d4.gif)\n\n### What do the generated sources look like?\n\nFor [`SignUpView`](https://github.com/avaloniaui/Avalonia.NameGenerator/blob/main/src/Avalonia.NameGenerator.Sandbox/Views/SignUpView.xaml), we get the following generated output when the source generator is in the `InitializeComponent` mode:\n\n```cs\n// \u003cauto-generated /\u003e\n\nusing Avalonia.Controls;\nusing Avalonia.Markup.Xaml;\n\nnamespace Sample.App\n{\n    partial class SampleView\n    {\n        internal global::Avalonia.NameGenerator.Sandbox.Controls.CustomTextBox UserNameTextBox;\n        public global::Avalonia.Controls.TextBlock UserNameValidation;\n        private global::Avalonia.Controls.TextBox PasswordTextBox;\n        internal global::Avalonia.Controls.TextBlock PasswordValidation;\n        internal global::Avalonia.Controls.ListBox AwesomeListView;\n        internal global::Avalonia.Controls.TextBox ConfirmPasswordTextBox;\n        internal global::Avalonia.Controls.TextBlock ConfirmPasswordValidation;\n        internal global::Avalonia.Controls.Button SignUpButton;\n        internal global::Avalonia.Controls.TextBlock CompoundValidation;\n\n        public void InitializeComponent(bool loadXaml = true, bool attachDevTools = true)\n        {\n            if (loadXaml)\n            {\n                AvaloniaXamlLoader.Load(this);\n            }\n\n// This will be added only if you install Avalonia.Diagnostics.\n#if DEBUG\n            if (attachDevTools)\n            {\n                this.AttachDevTools();\n            } \n#endif\n\n            UserNameTextBox = this.FindNameScope()?.Find\u003cglobal::Avalonia.NameGenerator.Sandbox.Controls.CustomTextBox\u003e(\"UserNameTextBox\");\n            UserNameValidation = this.FindNameScope()?.Find\u003cglobal::Avalonia.Controls.TextBlock\u003e(\"UserNameValidation\");\n            PasswordTextBox = this.FindNameScope()?.Find\u003cglobal::Avalonia.Controls.TextBox\u003e(\"PasswordTextBox\");\n            PasswordValidation = this.FindNameScope()?.Find\u003cglobal::Avalonia.Controls.TextBlock\u003e(\"PasswordValidation\");\n            AwesomeListView = this.FindNameScope()?.Find\u003cglobal::Avalonia.Controls.ListBox\u003e(\"AwesomeListView\");\n            ConfirmPasswordTextBox = this.FindNameScope()?.Find\u003cglobal::Avalonia.Controls.TextBox\u003e(\"ConfirmPasswordTextBox\");\n            ConfirmPasswordValidation = this.FindNameScope()?.Find\u003cglobal::Avalonia.Controls.TextBlock\u003e(\"ConfirmPasswordValidation\");\n            SignUpButton = this.FindNameScope()?.Find\u003cglobal::Avalonia.Controls.Button\u003e(\"SignUpButton\");\n            CompoundValidation = this.FindNameScope()?.Find\u003cglobal::Avalonia.Controls.TextBlock\u003e(\"CompoundValidation\");\n        }\n    }\n}\n```\n\nIf you enable the `OnlyProperties` source generator mode, you get:\n\n```cs\n// \u003cauto-generated /\u003e\n\nusing Avalonia.Controls;\n\nnamespace Avalonia.NameGenerator.Sandbox.Views\n{\n    partial class SignUpView\n    {\n        internal global::Avalonia.NameGenerator.Sandbox.Controls.CustomTextBox UserNameTextBox =\u003e this.FindNameScope()?.Find\u003cglobal::Avalonia.NameGenerator.Sandbox.Controls.CustomTextBox\u003e(\"UserNameTextBox\");\n        public global::Avalonia.Controls.TextBlock UserNameValidation =\u003e this.FindNameScope()?.Find\u003cglobal::Avalonia.Controls.TextBlock\u003e(\"UserNameValidation\");\n        private global::Avalonia.Controls.TextBox PasswordTextBox =\u003e this.FindNameScope()?.Find\u003cglobal::Avalonia.Controls.TextBox\u003e(\"PasswordTextBox\");\n        internal global::Avalonia.Controls.TextBlock PasswordValidation =\u003e this.FindNameScope()?.Find\u003cglobal::Avalonia.Controls.TextBlock\u003e(\"PasswordValidation\");\n        internal global::Avalonia.Controls.TextBox ConfirmPasswordTextBox =\u003e this.FindNameScope()?.Find\u003cglobal::Avalonia.Controls.TextBox\u003e(\"ConfirmPasswordTextBox\");\n        internal global::Avalonia.Controls.TextBlock ConfirmPasswordValidation =\u003e this.FindNameScope()?.Find\u003cglobal::Avalonia.Controls.TextBlock\u003e(\"ConfirmPasswordValidation\");\n        internal global::Avalonia.Controls.Button SignUpButton =\u003e this.FindNameScope()?.Find\u003cglobal::Avalonia.Controls.Button\u003e(\"SignUpButton\");\n        internal global::Avalonia.Controls.TextBlock CompoundValidation =\u003e this.FindNameScope()?.Find\u003cglobal::Avalonia.Controls.TextBlock\u003e(\"CompoundValidation\");\n    }\n}\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FAvaloniaUI%2FAvalonia.NameGenerator","html_url":"https://awesome.ecosyste.ms/projects/github.com%2FAvaloniaUI%2FAvalonia.NameGenerator","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FAvaloniaUI%2FAvalonia.NameGenerator/lists"}