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

Error MVVMTK0015 when the desired NotifyPropertyChangedFor property is an explicit interface implementation

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

还没有人认领这个 Issue。

评估

难度
3/5
预计耗时
1-2 天
新手友好度
48/100
Issue 类型
缺陷
描述清晰度
描述清楚
活跃度
停滞
技术栈
csharp
领域
devtools

调研方向

首先使用 CommunityToolkit.Mvvm 8.4.0 重现提供的显式接口实现,并跟踪产生 MVVMTK0015 的 NotifyPropertyChangedFor 验证。确认示例的行为,然后更新验证,使显式实现的接口属性能够被接受,同时现有的无效目标仍然报告该诊断。

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

描述

bug :bug:
Describe the bug

A compilation error results for a situation that we think should be valid. Specifically:

error MVVMTK0015: The target(s) of [NotifyPropertyChangedFor] must be a (different) accessible property, but "(the notify-for property name)" has no (other) matches in type (my type) (https://aka.ms/mvvmtoolkit/errors/mvvmtk0015)

It appears that the toolkit's conditions for NotifiyPropertyChangedFor are not satisfied when the property exists only as an explicitly-implemented interface property.

Regression

No response

Steps to reproduce

Given this simplified reproduction:

public interface IMenuItem
{
    bool Enabled { get; }
}

public partial class MenuItem : ObservableObject, IMenuItem
{
    [ObservableProperty]
    [NotifyPropertyChangedFor(nameof(IMenuItem.Enabled))]
    public partial int ValueThatAffectsEnabled { get; set; }

    bool IMenuItem.Enabled => ValueThatAffectsEnabled > 0;
}

Compiling results in the following error:

error MVVMTK0015: The target(s) of [NotifyPropertyChangedFor] must be a (different) accessible property, but "Enabled" has no (other) matches in type MenuItem (https://aka.ms/mvvmtoolkit/errors/mvvmtk0015)

Expected behavior

We expect this to be allowed by the NotifyPropertyChangedFor attribute's validation because:

  1. There is a public property named Enabled, it's simply of lessened visibility (only visible through the interface)
  2. It is a very common for our views to work with their View Models as interfaces, not concrete types. This lets us swap in/out compatible view model types.

We can work around this:

  • We could make those properties not explicit interface implementations (we'd rather not, we design member visibility very intentionally).
  • We could add OnXXXXChanged() partial methods to manually call OnPropertyChanged("the desired property").

but it would be better if the library would allow this.

Screenshots

No response

IDE and version

VS 2022

IDE version

17.14.10

Nuget packages
  • CommunityToolkit.Common
  • CommunityToolkit.Diagnostics
  • CommunityToolkit.HighPerformance
  • CommunityToolkit.Mvvm (aka MVVM Toolkit)
Nuget package version(s)

8.4.0

Additional context

No response

Help us help you

No, just wanted to report 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 摘要。