简体   繁体   English

从一个实现INotifyPropertyChanged的基类继承

[英]Inheriting from one base class that implements INotifyPropertyChanged

I've been using the following bit of code in a cookie cutter fashion, across dozens of classes 我一直在使用以下几个代码来完成几十个课程

public event PropertyChangedEventHandler PropertyChanged;

protected void NotifyPropertyChanged(string propertyName)
{
   if (PropertyChanged != null)
   {
      PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
   }
}

All of these classes implement INotifyPropertyChanged . 所有这些类都实现了INotifyPropertyChanged To silence my DRY alarms, lately I've been refactoring these classes to inherit my base class PropertyNotifier whose only purpose is to provide NotifyPropertyChanged for classes that inherit from it -- which are the dozens of ViewModel classes in my huge project. 为了使我的DRY警报静音,最近我一直在重构这些类来继承我的基类PropertyNotifier它的唯一目的是为从它继承的类提供NotifyPropertyChanged - 这是我庞大项目中的几十个ViewModel类。

It feels lazy and a bit dirty. 它感觉很懒,有点脏。 Am I hurting performance or breaking good design practices? 我是在损害性能还是破坏了良好的设计实践? I figure if change notification was supposed to be this easy, there would be a base class already in the WPF framework that does what my PropertyNotifier class does. 我认为如果改变通知应该很容易,那么WPF框架中就会有一个基类来执行我的PropertyNotifier类所做的事情。

Note that, for a lot of reasons, I've been having performance issues with my UI responsiveness -- mostly due to a large number of controls. 请注意,由于很多原因,我的UI响应性一直存在性能问题 - 主要是由于大量控件。 So I'm looking to trim the fat wherever I can. 所以我希望尽可能地减肥。 Any ideas? 有任何想法吗?

This is a very common base class to have in WPF or Silverlight development and won't noticeably affect performance. 这是WPF或Silverlight开发中非常常见的基类,不会显着影响性能。 The only problem I have with PropertyNotifier as a base class results from being restricted to single inheritance but that tends to be a rare issue with the type of class you'll need it for. 我将PropertyNotifier作为基类的唯一问题是限制为单一继承,但这对于您需要它的类的类型来说往往是一个罕见的问题。

Yes, that's very common practice. 是的,这是非常常见的做法。 For a large scale application it becomes necessary to have this kind of base class. 对于大规模的应用程序,有必要拥有这种基类。 We had also created a BaseViewModel for the same purpose; 我们还为同样的目的创建了一个BaseViewModel ; we also implemented a lot of common code(across ViewModels) in this base class like logging, displaying error messages, initializing WCF proxy objects etc. 我们还在这个基类中实现了许多公共代码(跨ViewModels),如日志记录,显示错误消息,初始化WCF代理对象等。

Another implementation is the Model class of the WPF Application Framework (WAF) . 另一个实现是WPF应用程序框架(WAF)Model类。 It does exactly the same. 它完全一样。

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

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