我在类的INotifyPropertyChanged实现中使用了INotifyPropertyChanged属性,如下所示:
public event PropertyChangedEventHandler PropertyChanged;
// This method is called by the Set accessor of each property.
// The CallerMemberName attribute that is applied to the optional propertyName
// parameter causes the property name of the caller to be substituted as an argument.
private void NotifyPropertyChanged([CallerMemberName] String propertyName = "")
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}但是,使用默认参数并不符合CLS。但CallerMemberName只能用于具有默认值的参数.是否有一种常用的方法来解决这种不一致性,而不必使用硬编码的字符串参数调用notify方法?
发布于 2017-08-04 09:59:18
我只是简单地删除了CallerMemberName属性和默认参数值,这意味着该参数不再是可选的,因此方法签名变成:
private void NotifyPropertyChanged(String propertyName)然后通过提供字符串参数的nameof操作符调用它,这是一个很小(足够)的更改:
NotifyPropertyChanged(nameof(FooProperty));这似乎挺管用的。
不过,我将暂时搁置这个问题,因为其他人可能有更好的办法,或提出这个解决办法的问题。
https://stackoverflow.com/questions/45502525
复制相似问题