简体   繁体   English

ObservableCollection如何 <T> 实现INotifyPropertyChanged为受保护的吗?

[英]How does ObservableCollection<T> implement INotifyPropertyChanged as protected?

This is the definition for INotifyPropertyChanged 这是INotifyPropertyChanged的定义

public interface INotifyPropertyChanged
{
    event PropertyChangedEventHandler PropertyChanged;
}

ObservableCollection<T> implements this... ObservableCollection<T>实现了这个...

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

When I tested this... 当我测试这个...

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

I got the following error: 我收到以下错误:

Test does not implement interface member 'System.ComponentModel.INotifyPropertyChanged.PropertyChanged'. 测试未实现接口成员'System.ComponentModel.INotifyPropertyChanged.PropertyChanged'。 'Test.PropertyChanged' cannot implement an interface member because it is not public. 'Test.PropertyChanged'无法实现接口成员,因为它不是公共的。

How is this possible? 这怎么可能?

The interface itself is implemented explicitly, with a protected event for classes to override 接口本身是显式实现的,具有一个受保护的事件供类重写

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;
}

In addition to the protected PropertyChanged event , ObservableCollection<T> provides an explicit interface implementation for the INotifyPropertyChanged.PropertyChanged event , which is lacking in your class (and not shown in your code snippet from ObservableCollection<T> ). 除了受保护的PropertyChanged事件之外ObservableCollection<T>还为INotifyPropertyChanged.PropertyChanged事件提供了显式接口实现,该事件在您的类中缺少(并且未在ObservableCollection<T>代码段中显示)。

You can read about explicit interface implementation on MSDN . 您可以阅读有关MSDN上显式接口实现的信息

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM