为每个侦听器将不同的 EventArgs 注入到每个事件中?

发布于 2025-01-16 05:27:17 字数 599 浏览 2 评论 0原文

所以我有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文