在实现
propertyName
时我一直不确定INotifyPropertyChanged
的含义。所以通常你将 INotifyPropertyChanged
实现为:
public class Data : INotifyPropertyChanged {
public event PropertyChangedEventHandler PropertyChanged;
private void NotifyPropertyChanged(string propertyName = "") {
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
private string itsID;
public string ID {
get { return itsID; }
set {
if (itsID != value) {
itsID = value;
NotifyPropertyChanged("ID");
}
}
}
我从来不确定
propertyName
对NotifyPropertyChanged(string propertyName)
的争论。
propertyName
与 Property
的名称不完全匹配,.NET 是否会将整个对象视为 changed
?这不是 .NET Framework 本身,而是几乎每个
PropertyChanged
订阅者(其中一些确实恰好作为框架的一部分进行分发)假设您通过发送属性名称按预期使用该接口。如果您发送属性 MyID
已更改的通知,则当另一个组件正在查看属性 ID
时,它通常会看到该通知,比较名称,并得出“此通知不适合我”的结论。
nameof
的新 C#6 功能。 nameof
功能可以回答您的所有问题,因为我们可以说使用 nameof
的主要优势一言以蔽之就是重构。 “重构”是您根据您的问题正在寻找的词:
NotifyPropertyChanged(nameof(ID));
作为示例重命名
ID
也会更改属性的名称,否则会破坏编译,但以下不会:
NotifyPropertyChanged("ID")
它必须与房产名称完全匹配。另外,您应该在调用 PropertyChanged 之前检查该值是否确实已更改,因为这是一个相对昂贵的调用(WPF 不会知道该属性尚未更改)。在VS2015中,还可以使用nameof操作符。
根据INotifyPropertyChanged Interface页面,您可以将
propertyName
参数保留为未定义。原因如下:
// 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 = "")
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
注意参数中的 [CallerMemberName] 属性。这将自动用属性的名称替换字符串,无需静态定义它。该文档还指定:
PropertyChanged 事件可以通过使用
或null
作为String.Empty
中的属性名称来指示对象上的所有属性已更改。请注意,在 UWP 应用程序中,必须使用PropertyChangedEventArgs
而不是 null。String.Empty
这只是为了方便您手动处理特定控件或整个表单的更改。
INotifyPropertyChanged
接口没有定义NotifyPropertyChanged
方法,因此必须事先了解CallerMemberName
属性。