如何自动为Avalonia应用生成StyledProperty和DirectProperty?

摘要:最近学习了源生成器,遂仿照CommunityToolkitWindows中的DependencyPropertyGenerator写了个生成器,可自动生成Avalonia中的StyledProperty和DirectProperty Nu
最近学习了源生成器,遂仿照CommunityToolkit/Windows中的DependencyPropertyGenerator写了个生成器,可自动生成Avalonia中的StyledProperty和DirectProperty NuGet:https://www.nuget.org/packages/PropertyGenerator.Avalonia Github:https://github.com/zxbmmmmmmmmm/PropertyGenerator 先决条件 Avalonia版本:≥ 11.3.0 由于使用了field关键字和部分属性,需要在项目文件内将LangVersion设置为preview StyledProperty 在需要生成StyledProperty的部分属性上添加GeneratedStyledProperty特性即可 [GeneratedStyledProperty] public partial int Count { get; set; } 生成的代码: StyledProperty<int> CountProperty = AvaloniaProperty.Register<MainWindow, int>(name: nameof(Count)); public partial int Count { get => GetValue(CountProperty); set => SetValue(CountProperty, value); } StyledProperty不支持直接设置默认值,需要使用以下写法 [GeneratedStyledProperty(10)] public partial int Count { get; set; } 生成的代码: Avalonia.StyledProperty<int> CountProperty = AvaloniaProperty.Register<MainWindow, int>(name: nameof(Count), defaultValue: 10); public partial int Count { get => GetValue(CountProperty); set => SetValue(CountProperty, value); } StyledProperty的所有功能都被支持(仅作展示) [GeneratedStyledProperty( DefaultValueCallback = nameof(DefaultValueCallback), DefaultValue = true, Validate = nameof(Validate), Coerce = nameof(Coerce), EnableDataValidation = true, Inherits = true, DefaultBindingMode = BindingMode.TwoWay)] public partial bool? IsStarted { get; set; } private static bool DefaultValueCallback() { return true; } private static bool Validate(bool? value) { return true; } private static bool? Coerce(AvaloniaObject x, bool? y) { return true; } 生成的代码: StyledProperty<bool?> IsStartedProperty = AvaloniaProperty.Register<MainWindow, bool?>( name: nameof(IsStarted), defaultValue: DefaultValueCallback(), validate: Validate, coerce: Coerce, enableDataValidation: true, inherits: true, defaultBindingMode:BindingMode.TwoWay); public partial bool? IsStarted { get => GetValue(IsStartedProperty); set => SetValue(IsStartedP
阅读全文