通过 eventargs 传回数据的问题或最佳实践?

发布于 2024-08-13 13:07:14 字数 392 浏览 4 评论 0原文

我有一个非 GUI 类,它生成有关其正在执行的操作的事件(表单又使用这些事件向用户显示进度)。

其中一个事件是 AboutToDoSomethingDestructiveEvent。现在,我们希望表单在引发 AboutToDoSomethingDestructiveEvent 时向用户显示一个对话框,询问他们是否希望发生 SomethingDestructive。如果他们选择“否”,那么我们将在客户 EventArgs 上设置一个值,原始表单将读取该值,然后跳过执行 SomethingDestructive

这是事件和 EventArgs 的正确使用吗?这种方法有问题吗?是否有做此类事情的最佳实践?

I've got a non-GUI class that generates events as to what it is doing (which are in turn used by a Form to display to the user the progress).

One of the events is a AboutToDoSomethingDestructiveEvent. Now we want to have the Form display a dialog to the user when AboutToDoSomethingDestructiveEvent is raised, asking them if they would like SomethingDestructive to happen. If they select no, then we would set a value on the customer EventArgs and the original form would read that value and then skip doing SomethingDestructive.

Is this a proper use of Events and EventArgs? Are there problems with this approach? Are there any best practices for doing this sort of thing?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

一场春暖 2024-08-20 13:07:14

这种方法非常好,.NET Framework 中甚至有一个类: 取消事件参数

The approach is so good there's even a class in the .NET Framework for this: CancelEventArgs

○闲身 2024-08-20 13:07:14

你的想法就是正确的做法。 Console.CancelKeyPress 事件本质上是相同的事情。

Console.CancelKeyPress

The way you are thinking is the proper way to do it. The Console.CancelKeyPress event is essentially the same thing.

Console.CancelKeyPress

谁与争疯 2024-08-20 13:07:14

这是一个正确的方法,只要您有自己的 EventArgs,它继承自 System.EventArgs。这很常见,我能想到的最好的例子是 PostSharp 中的 FlowBehavior。

This is a proper approach, as long as you have your own EventArgs, which are inheriting from System.EventArgs. It is very common, the best example I can think of is in PostSharp with the FlowBehavior.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文