重新实现相同的事件调用者 [英] Re-implementing the same event invoker

查看:110
本文介绍了重新实现相同的事件调用者的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在写几个类,我想使它们都符合数据绑定(对于WPF,甚至是很少的WinForms),通过实现 INotifyPropertyChanged



问题是重复的代码。我实际上复制粘贴相同的方法(我不是开玩笑)。

  protected void OnPropertyChanged([CallerMemberName ] String propertyName = null)
{
if(PropertyChanged!= null)
PropertyChanged(this,new PropertyChangedEventArgs(propertyName));
}

我有一段时间的这个问题,但是今天特别是它再次发生,再次,所以希望你能帮我解决一个问题。我有几十个类有这个方法,我真的很讨厌重复这段代码。



我想到创建一个将实现它的基类( NotifyPropertyChangedObject 的名字也许),但这可能是一个坏主意,将真正限制我的类没有多重继承。
我也想到了一个扩展方法,但我想范围它作为一个受保护的方法,这样也不会奏效。



可以做些什么来解决这个问题?

解决方案

有一个基类是即使MVVM Helper库也是这样的方法。没有任何缺点。



是的,您只能为c#类只有一个基类,但具有实现多个接口。对于你的情况,所有你需要做的就是说有一个基类实现INPC,并调用它 ViewModelBase



现在如果目前你有Class X 继承自 A ,只需让A继承自 ViewModelBase



因此,您因此使您当前的基类继承自此新的INPC提供类,并且您没有任何代码重复的INPC实现任何派生类别



更新



在您特殊情况下无论什么原因被绑定到已经有另一个基类,并且限制没有像INPC的公共实现一样传递给这个对象作为成员变量,



你可以尝试看看这个:



Fody 特别是它的插件 P ropertyChanged - Addon



这将有助于您自己注入INPC实现,因此不需要您复制粘贴代码,它也允许您从任何自定义基类派生(仍然需要指定INPC,但这只是一个接口)


I'm writing a few classes and I want to make them all "data-binding compliant" (for WPF, or even the probably rarer WinForms) by implementing INotifyPropertyChanged.

The issue is the repeated code. I actually copy-paste the same method over and over again (I'm not joking).

protected void OnPropertyChanged([CallerMemberName] String propertyName = null)
{
    if (PropertyChanged != null)
        PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}

I had this issue for a while but today specifically it just keeps happening again and again, so I hope you could help me with a solution. I have almost a dozen classes that have this method and I really hate to repeat that piece of code.

I thought of creating a base class that will implement it (NotifyPropertyChangedObject for the name maybe) but that's probably a bad idea that will really limit my classes without multiple inheritance.
I also thought of an extension method but I would like to scope it as a protected method, so that won't work either.

What could be done to fix this issue?

解决方案

Having a base class is the method even MVVM Helper libraries do. There is no drawback to that.

Yes you can have only one base class for a c# class but have it implement multiple interfaces. For your case all you would have to do is say have a base class implement INPC and call it ViewModelBase

Now if currently you have Class X inherit from Class A, Just make A inherit from ViewModelBase.

You hence thus make your current base classes inherit from this new INPC providing class and you don't have any code duplication for INPC implementation in any of your derived classes

Update

In your special case where you are for whatever reason tied into already having another base class and with the restriction of not having say something like a public implementation of INPC passed to this object as a member variable,

You can try having a look at this:

Fody and in particular it's addon PropertyChanged - Addon

This will hopefully help you since it injects the INPC implementations itself, thus not requiring you to Copy Paste code and it also then allows you to derive from any custom base class(still need to specify INPC but thats just an interface here)

这篇关于重新实现相同的事件调用者的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆