问题描述
当我们要将数据传递给事件订阅者时,我们使用EventArgs(或CustomEventArgs)。
When we want to pass data to an event subscriber, we use EventArgs (or CustomEventArgs) for this.
.Net提供了一个类型为EventHandler的构建,它使用一个参数,也是构建的EventArgs类的一个实例。
.Net provides a build in type EventHandler that uses as a parameter an instance of EventArgs class that is build in as well.
当我需要通知用户某些操作已经结束的情况下,例如搜索是过度?我不想使用EventArgs,它不会包含任何东西。
What about cases, when I need to notify a subscriber that some action is over, for example search is over? I don't want to even use EventArgs, that won't contain anything.
是否有一个类型用于发信号通知另一个类,而不需要使用空的EventArgs?
Is there a build in type for signaling another class, without the need to use empty EventArgs?
推荐答案
你可以做几件事:
- 使用EventHandler和基本的EventArg类 - 确定它是空的,但是这是否受到伤害?
- 使用自己的代理,并使用
事件MyDelegateWithoutParams MyEvent;
- 使用具有IObservable的观察者模式
- 让客户端通过一个Action,并调用此操作
- youse your normal event with EventHandler and the basic EventArg class - sure it's empty but does this hurt?
- make your own delegate and use this with
event MyDelegateWithoutParams MyEvent;
- use the Observer-Pattern with IObservable instead
- let the clients pass a Action do you and call this action
我希望其中一个选择是您的喜好。我使用1和4这种情况(4个主要是只有一个听众。
I hope one of this options is to your liking. I use 1 and 4 for this kind of situation (4 mostly if there will be only one "listener".
PS:我猜2不符合 .net框架指南,所以也许这不是最好的主意;)
PS: I guess 2 won't conform to the .net framework guidelines so maybe this is not the best idea ;)
这篇关于EventHandler类型,没有事件args的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!