Skip to content

Avalonia.PropertyGenerator generates the appropriate CLR members for Avalonia property definitions.

License

Notifications You must be signed in to change notification settings

jp2masa/Avalonia.PropertyGenerator

Repository files navigation

Build status NuGet MyGet

Avalonia.PropertyGenerator

Avalonia.PropertyGenerator generates the appropriate CLR members for Avalonia property definitions.

Usage

  1. Add reference to jp2masa.Avalonia.PropertyGenerator.CSharp package:
<PackageReference Include="jp2masa.Avalonia.PropertyGenerator.CSharp" Version="0.10.0-beta2" PrivateAssets="All" />
  1. Declare Avalonia properties as usual, except the CLR members, which are now automatically generated!

Example

Source

using Avalonia.Controls;
using Avalonia.Controls.Primitives;

namespace Avalonia.PropertyGenerator.CSharp.Demo
{
    internal sealed partial class DemoControl : TemplatedControl
    {
        public static readonly StyledProperty<double> NumberProperty =
            AvaloniaProperty.Register<DemoControl, double>(nameof(Number));

        [BackingField(Name = "m_text", Accessibility = BackingFieldAccessibility.Internal)]
        public static readonly DirectProperty<DemoControl, string?> TextProperty =
            AvaloniaProperty.RegisterDirect<DemoControl, string?>(nameof(Text), o => o.Text, (o, v) => o.Text = v);

        public static readonly AttachedProperty<bool> BoolProperty =
            AvaloniaProperty.RegisterAttached<DemoControl, Control, bool>("Bool");

        public DemoControl()
        {
            m_text = "Hello World!";
        }
    }
}

Generated code

namespace Avalonia.PropertyGenerator.CSharp.Demo
{
    partial class DemoControl
    {
        public double Number
        {
            get => GetValue(NumberProperty);
            set => SetValue(NumberProperty, value);
        }

        internal string? m_text;

        public string? Text
        {
            get => m_text;
            set => SetAndRaise(TextProperty, ref m_text, value);
        }

        public static bool GetBool(Avalonia.AvaloniaObject obj) => obj.GetValue(BoolProperty);

        public static void SetBool(Avalonia.AvaloniaObject obj, bool value) => obj.SetValue(BoolProperty, value);
    }
}

TODO

  • Readonly direct properties
  • Generate XML documentation

About

Avalonia.PropertyGenerator generates the appropriate CLR members for Avalonia property definitions.

Topics

Resources

License

Stars

Watchers

Forks

Sponsor this project

 

Packages

No packages published

Languages