OutWit.Common.MVVM.Avalonia.Generator
2.0.4
dotnet add package OutWit.Common.MVVM.Avalonia.Generator --version 2.0.4
NuGet\Install-Package OutWit.Common.MVVM.Avalonia.Generator -Version 2.0.4
<PackageReference Include="OutWit.Common.MVVM.Avalonia.Generator" Version="2.0.4"> <PrivateAssets>all</PrivateAssets> <IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets> </PackageReference>
<PackageVersion Include="OutWit.Common.MVVM.Avalonia.Generator" Version="2.0.4" />
<PackageReference Include="OutWit.Common.MVVM.Avalonia.Generator"> <PrivateAssets>all</PrivateAssets> <IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets> </PackageReference>
paket add OutWit.Common.MVVM.Avalonia.Generator --version 2.0.4
#r "nuget: OutWit.Common.MVVM.Avalonia.Generator, 2.0.4"
#:package OutWit.Common.MVVM.Avalonia.Generator@2.0.4
#addin nuget:?package=OutWit.Common.MVVM.Avalonia.Generator&version=2.0.4
#tool nuget:?package=OutWit.Common.MVVM.Avalonia.Generator&version=2.0.4
OutWit.Common.MVVM.Avalonia.Generator
Roslyn Source Generator for automatic Avalonia StyledProperty, DirectProperty, and AttachedProperty generation. This generator reads properties marked with the corresponding attributes and generates the necessary property fields and implementations at compile time.
?? Important: Don't Install Directly
This package should NOT be installed directly! It's automatically included when you install OutWit.Common.MVVM.Avalonia.
# ? Correct - Install the Avalonia package
dotnet add package OutWit.Common.MVVM.Avalonia
# ? Wrong - Don't install the generator directly
dotnet add package OutWit.Common.MVVM.Avalonia.Generator
What It Does
This source generator:
Finds Properties - Scans your code for properties marked with:
[StyledProperty]- generates AvaloniaStyledProperty[DirectProperty]- generates AvaloniaDirectPropertywith backing field[AttachedProperty]- generates AvaloniaAttachedProperty
Generates Code - Creates:
public static readonly StyledProperty<T>/DirectProperty<TOwner, T>/AttachedProperty<T>fields- Backing fields for
DirectProperty Get{PropertyName}andSet{PropertyName}methods for attached properties
Convention-Based Discovery - Automatically finds callback methods:
On{PropertyName}Changedfor property changed handlers{PropertyName}Coercefor coerce callbacks
How It Works
Input (Your Code)
using Avalonia.Controls;
using OutWit.Common.MVVM.Avalonia.Attributes;
namespace MyApp.Controls
{
public partial class CustomButton : Button
{
[StyledProperty(DefaultValue = "Click Me")]
public string Label { get; set; }
[DirectProperty(DefaultValue = 0)]
public int ClickCount { get; set; }
// Convention: On{PropertyName}Changed
private void OnLabelChanged(AvaloniaPropertyChangedEventArgs<string> e)
{
UpdateLabel();
}
}
}
Output (Generated Code)
// <auto-generated/>
#nullable enable
namespace MyApp.Controls
{
partial class CustomButton
{
public static readonly StyledProperty<string> LabelProperty =
AvaloniaProperty.Register<CustomButton, string>(nameof(Label), "Click Me");
private int m_clickCount = 0;
public static readonly DirectProperty<CustomButton, int> ClickCountProperty =
AvaloniaProperty.RegisterDirect<CustomButton, int>(
nameof(ClickCount),
o => o.m_clickCount,
(o, v) => o.m_clickCount = v,
unsetValue: 0);
}
}
Features
? Three Property Types
| Type | Use Case | Style System |
|---|---|---|
StyledProperty |
Most properties | Yes |
DirectProperty |
Frequently changing values | No |
AttachedProperty |
Properties on other objects | Yes |
?? Convention-Based Callbacks
- Automatically finds
On{PropertyName}Changedmethods - Automatically finds
{PropertyName}Coercemethods
?? Attached Properties
public static partial class MyAttachedProperties
{
[AttachedProperty(DefaultValue = false)]
public static bool IsHighlighted { get; set; }
}
// Generates:
// - public static readonly AttachedProperty<bool> IsHighlightedProperty
// - public static bool GetIsHighlighted(AvaloniaObject obj)
// - public static void SetIsHighlighted(AvaloniaObject obj, bool value)
Requirements
- .NET SDK: 6.0 or later
- Target Framework: net6.0 or later
- Platform: Cross-platform (Avalonia)
Troubleshooting
Generated Code Not Appearing
- Clean and rebuild:
dotnet clean && dotnet build - Ensure class is marked as
partial - Check using directive:
using OutWit.Common.MVVM.Avalonia.Attributes;
View Generated Files
<PropertyGroup>
<EmitCompilerGeneratedFiles>true</EmitCompilerGeneratedFiles>
<CompilerGeneratedFilesOutputPath>$(BaseIntermediateOutputPath)Generated</CompilerGeneratedFilesOutputPath>
</PropertyGroup>
Related Packages
- OutWit.Common.MVVM.Avalonia - Main Avalonia package (install this!)
- OutWit.Common.MVVM - Cross-platform base classes
License
Licensed under the Apache License, Version 2.0. See LICENSE.
Attribution (optional)
If you use OutWit.Common.MVVM.Avalonia.Generator in a product, a mention is appreciated (but not required), for example: "Powered by OutWit.Common.MVVM.Avalonia.Generator (https://ratner.io/)".
Trademark / Project name
"OutWit" and the OutWit logo are used to identify the official project by Dmitry Ratner.
You may:
- refer to the project name in a factual way (e.g., "built with OutWit.Common.MVVM.Avalonia.Generator");
- use the name to indicate compatibility (e.g., "OutWit.Common.MVVM.Avalonia.Generator-compatible").
You may not:
- use "OutWit.Common.MVVM.Avalonia.Generator" as the name of a fork or a derived product in a way that implies it is the official project;
- use the OutWit.Common.MVVM.Avalonia.Generator logo to promote forks or derived products without permission.
Remember: This package is automatically included with OutWit.Common.MVVM.Avalonia. Don't install it separately!
Learn more about Target Frameworks and .NET Standard.
-
.NETStandard 2.0
- No dependencies.
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.