Hacktoberfest 2026:维护者为十月标记出来的 issue,仍然开放、适合新手。 浏览 Hacktoberfest issue

[MVVM] `AttachedProperty`/`DirectProperty`/`StyledProperty` source generators

未关闭
#1,203 0 条评论 1 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

评估

难度
5/5
预计耗时
一周以上
新手友好度
35/100
Issue 类型
功能
描述清晰度
基本清楚
活跃度
冷清
技术栈
csharp
领域
tooling

调研方向

先从 API 分解和 QuxControl.cs 使用示例开始,然后将提议的生成输出与现有的 toolkit 结构进行比较。issue 没有列出任何实现文件或测试,因此第一项任务是定位 source-generator 的入口点和相关测试项目。支持这三个 attribute,并生成所示的 property 和 Avalonia property 成员,即表示完成。

由索引模型根据 Issue 内容生成。

描述

feature request :mailbox_with_mail:
Overview

Examples like these require a considerable amount of repetitive code:

public bool Foo
{
  get => GetValue(FooProperty);
  set => SetValue(FooProperty, value);
}

public ObservableCollection<BarViewModel>? BarsSource
{
  get => GetValue(BarsSourceProperty);
  set => SetValue(BarsSourceProperty, value);
}

public ICommand? BazCommand
{
  get => GetValue(BazCommandProperty);
  set => SetValue(BazCommandProperty, value);
}

public static readonly StyledProperty<bool> FooProperty =
  AvaloniaProperty.Register<QuxControl, bool>(nameof(Foo));

public static readonly StyledProperty<ObservableCollection<BarViewModel>?> BarsSourceProperty =
  AvaloniaProperty.Register<QuxControl, ObservableCollection<BarViewModel>?>(nameof(BarsSource));

public static readonly StyledProperty<ICommand?> BazCommandProperty =
  AvaloniaProperty.Register<QuxControl, ICommand?>(nameof(BazCommand));
API breakdown
namespace CommunityToolkit.Mvvm.ComponentModel;

[AttributeUsage(AttributeTargets.Field | AttributeTargets.Property, AllowMultiple = false, Inherited = false)]
public sealed class AttachedPropertyAttribute : Attribute;

[AttributeUsage(AttributeTargets.Field | AttributeTargets.Property, AllowMultiple = false, Inherited = false)]
public sealed class DirectPropertyAttribute : Attribute;

[AttributeUsage(AttributeTargets.Field | AttributeTargets.Property, AllowMultiple = false, Inherited = false)]
public sealed class StyledPropertyAttribute : Attribute;
Usage example

QuxControl.cs

public partial class QuxControl : Panel
{
  [StyledProperty]
  public partial bool Foo { get; set; }

  [StyledProperty]
  public partial ObservableCollection<BarViewModel>? BarsSource { get; set; }

  [StyledProperty]
  public partial ICommand? BazCommand { get; set; }
}

MyProject.Views.QuxControl.g.cs

namespace MyProject.Views
{
  partial class QuxControl
  {
    /* ... */
    public partial bool Foo
    {
      get => GetValue(FooProperty);
      set => SetValue(FooProperty, value);
    }

    /* ... */
    public partial ObservableCollection<BarViewModel>? BarsSource
    {
      get => GetValue(BarsSourceProperty);
      set => SetValue(BarsSourceProperty, value);
    }

    /* ... */
    public partial ICommand? BazCommand
    {
      get => GetValue(BazCommandProperty);
      set => SetValue(BazCommandProperty, value);
    }

    /* ... */
    public static readonly StyledProperty<bool> FooProperty =
      AvaloniaProperty.Register<QuxControl, bool>(nameof(Foo));

    /* ... */
    public static readonly StyledProperty<ObservableCollection<BarViewModel>?> BarsSourceProperty =
      AvaloniaProperty.Register<QuxControl, ObservableCollection<BarViewModel>?>(nameof(BarsSource));

    /* ... */
    public static readonly StyledProperty<ICommand?> BazCommandProperty =
      AvaloniaProperty.Register<QuxControl, ICommand?>(nameof(BazCommand));
  }
}
Breaking change?

No

Alternatives

There are existing packages (https://github.com/jp2masa/Avalonia.PropertyGenerator, https://github.com/lucdem/AvaSourceGenerators) that don't entirely work in the proposed way.

Additional context

No response

Help us help you

No, just wanted to propose this

主要语言
C#
星标
3.8k
派生
401
PR 合并指标
30 天内没有已合并 PR

环境准备

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

CommunityToolkit/dotnet 的其他 Issue

查看 CommunityToolkit/dotnet 的全部 Issue

相似的 Issue

更多 C# Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。