gpt4 book ai didi

c# - ObservableCollection 如何将 INotifyPropertyChanged 实现为 protected ?

转载 作者:行者123 更新时间:2023-12-02 21:19:19 26 4
gpt4 key购买 nike

这是 INotifyPropertyChanged 的定义

public interface INotifyPropertyChanged
{
event PropertyChangedEventHandler PropertyChanged;
}

ObservableCollection<T>实现这个...

public class ObservableCollection<T> : Collection<T>, INotifyCollectionChanged, INotifyPropertyChanged
{
protected virtual event PropertyChangedEventHandler PropertyChanged;
}

当我测试这个时...

public class Test : INotifyPropertyChanged
{
protected virtual event PropertyChangedEventHandler PropertyChanged;
}

我收到以下错误:

Test does not implement interface member 'System.ComponentModel.INotifyPropertyChanged.PropertyChanged'. 'Test.PropertyChanged' cannot implement an interface member because it is not public.

这怎么可能?

最佳答案

接口(interface)本身是显式实现的,具有供类覆盖的 protected 事件

public class Test : INotifyPropertyChanged
{
// explicit interface implementation
event PropertyChangedEventHandler INotifyPropertyChanged.PropertyChanged
{
add
{
PropertyChanged += value;
}
remove
{
PropertyChanged -= value;
}
}

// protected virtual (for derived classes to override)
protected virtual event PropertyChangedEventHandler PropertyChanged;
}

关于c# - ObservableCollection<T> 如何将 INotifyPropertyChanged 实现为 protected ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28606511/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com