为每个侦听器将不同的 EventArgs 注入到每个事件中?
所以我有一个 EventHandler:
public event EventHandler<CustomEventArgs> Confirmed;
protected void RaiseConfirmed()
{
Confirmed?.Invoke(this, new CustomEventArgs(Color.red));
}
并且我在不同的类中有几个事件侦听器,例如。
public void SetColor(object sender, CustomEventArgs e)
{
//do something with e.Color
}
但这并不完全是我想要的。
是否可以引发事件,但将不同的 EventArgs 发送到不同的侦听器?
因此,当单击“确认”按钮并触发“确认”事件时,我想将一个侦听器设置为红色,将另一个侦听器设置为蓝色,将另一个侦听器设置为绿色。也许我还有另一个监听器,它不接受颜色,而是接受布尔值。
因此本质上,发送者并不真正了解该参数。参数在接收者接收之前被注入。
这有道理吗?我应该实现自己的事件处理来支持每个侦听器的不同参数吗?
So I have an EventHandler:
public event EventHandler<CustomEventArgs> Confirmed;
protected void RaiseConfirmed()
{
Confirmed?.Invoke(this, new CustomEventArgs(Color.red));
}
And I have a couple of event listeners in different classes, such as.
public void SetColor(object sender, CustomEventArgs e)
{
//do something with e.Color
}
But that's not exactly what I want.
Is it possible to raise the event, but send different EventArgs to the different listeners?
So when the Confirm button is clicked, and the Confirmed event is fired, I want to set one listener to Color red, the other to Color blue, and yet another listener to Color green. And perhaps I have yet another listener that does not take a color, but a bool value.
So essentially, the sender does not really know about the argument. The argument is injected before the receiver receives it.
Does that make sense? Should I implement my own event handling to support different arguments per listener?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论