用于 Avalonia 的 StyledProperty 源生成器,简化属性定义
$ dotnet add package EasyStyledProperty用于 Avalonia 的 StyledProperty 源生成器,让您通过简单的特性标注自动生成 StyledProperty 和属性代码。A set of three projects that illustrates Roslyn source generators. Enjoy this template to learn from and modify source generators for your own needs.
dotnet add package EasyStyledProperty**You must build this project to see the result (generated code) in the IDE.**
Generators.ReportAttribute attribute.A project that references source generators. Note the parameters of ProjectReference in SourceGenerators.Sample.csproj, they make sure that the project is referenced as a set of source generators.
using EasyStyledProperty;### SourceGenerators.Tests
Unit tests for source generators. The easiest way to develop language-related features is to start with unit tests.
[StyledProperty("Text", typeof(string), "默认文本")]
[StyledProperty("IsEnabled", typeof(bool), true)]## How To?
[StyledProperty("FontSize", typeof(double), 14.0)]### How to debug?
public partial class MyControl : AvaloniaObject- Use the [launchSettings.json](Properties/launchSettings.json) profile.
{- Debug tests.
// 源生成器会自动生成:
// - public static readonly StyledProperty<string> TextProperty### How can I determine which syntax nodes I should expect?
// - public string Text { get; set; }Consider using the Roslyn Visualizer toolwindow, witch allow you to observe syntax tree.
// - public static readonly StyledProperty<bool> IsEnabledProperty
// - public bool IsEnabled { get; set; }### How to learn more about wiring source generators?
// - public static readonly StyledProperty<double> FontSizePropertyWatch the walkthrough video: [Let’s Build an Incremental Source Generator With Roslyn, by Stefan Pölz](https://youtu.be/azJm_Y2nbAI)
// - public double FontSize { get; set; }The complete set of information is available in [Source Generators Cookbook](https://github.com/dotnet/roslyn/blob/main/docs/features/source-generators.cookbook.md).
}
[StyledProperty("Command", typeof(ICommand), null,
BindsTwoWayByDefault = true,
PropertyChangedCallback = "OnCommandChanged",
CoerceCallback = "CoerceCommand")]
public partial class MyButton : Button
{
private static void OnCommandChanged(AvaloniaObject obj, AvaloniaPropertyChangedEventArgs args)
{
// 属性变更回调
}
private static object CoerceCommand(AvaloniaObject obj, object value)
{
// 值强制转换
return value;
}
}
需要在您的项目中安装:
MIT License
HU