调用 INotifyPropertyChanged 的 PropertyChanged 事件的最佳方法是什么?
当您实现 INotifyPropertyChanged 接口时,您负责在类中每次更新属性时调用 PropertyChanged 事件。
这通常会产生以下代码:
public class MyClass: INotifyPropertyChanged
private bool myfield;
public bool MyField
{
get { return myfield; }
set
{
if (myfield == value)
return;
myfield = value;
OnPropertyChanged(new PropertyChangedEventArgs("MyField"));
}
}
public event PropertyChangedEventHandler PropertyChanged;
private void OnPropertyChanged(PropertyChangedEventArgs e)
{
PropertyChangedEventHandler h = PropertyChanged;
if (h != null)
h(this, e);
}
}
即每个属性 12 行。
如果能够像这样装饰自动属性,那就简单多了:
[INotifyProperty]
public double MyField{ get; set; }
但不幸的是,这是不可能的(请参阅 msdn 上的这篇文章)
如何减少每个属性所需的代码量?
When you implement the INotifyPropertyChanged interface, you're responsible for calling the PropertyChanged event each and everytime a property is updated in the class.
This typically leads to the following code :
public class MyClass: INotifyPropertyChanged
private bool myfield;
public bool MyField
{
get { return myfield; }
set
{
if (myfield == value)
return;
myfield = value;
OnPropertyChanged(new PropertyChangedEventArgs("MyField"));
}
}
public event PropertyChangedEventHandler PropertyChanged;
private void OnPropertyChanged(PropertyChangedEventArgs e)
{
PropertyChangedEventHandler h = PropertyChanged;
if (h != null)
h(this, e);
}
}
That is 12 lines per property.
It would be so much simpler if one was able to decorate automatic properties like this :
[INotifyProperty]
public double MyField{ get; set; }
But unfortunately this is not possible (see this post on msdn for example)
How can I reduce the amount of code needed per property?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
实际上,每个属性只有 3-4 行; 其他行在所有“通知”属性上摊销:
您可以尝试类似以下的操作,它分担更多负载:
Actually, it's only 3-4 lines per property; the other lines are amortized over all "notifying" properties:
You could try something like the following, which shares some more of the load:
我现在要做的就是在我的类中编写这个:
我编写了一个小工具,可以在部分类中生成所有需要的属性代码。
这绝不是一个优雅的解决方案,但它确实有效:)
What I do for now is that I write this in my class :
And I wrote a small tool which generate all the needed property code in a partial class.
This is by no way an elegant solution, but it works :)
外包是最有意义的。
使用以下代码编写一个类(ObservableObject):
所有从该类派生的类都可以访问该方法,这要归功于 protected。
例子:
It makes most sense to outsource.
Write a class ( ObservableObject) with the following code:
All classes derived from this class can access the method, thanks to protected.
Example: