C# 冒泡事件。

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/217233/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-03 18:30:53  来源:igfitidea点击:

Bubbling up events .

c#winformsevents

提问by leora

I have multiple layers in an application and i find myself having to bubble up events to the GUI layer for doing status bar changes, etc . . I find myself having to write repeated coded where each layer simply subscribes to events from the lower layer and then in the call back simply raise an event up the chain. Is there a more efficient way of doing this?

我在应用程序中有多个层,我发现自己必须将事件冒泡到 GUI 层以进行状态栏更改等。. 我发现自己不得不编写重复的代码,其中每一层只是简单地订阅来自较低层的事件,然后在回调中简单地在链上引发一个事件。有没有更有效的方法来做到这一点?

采纳答案by Matt Hamilton

If all you're doing is firing an event handler from another event handler, you can cut out the middle man and hook the event handlers directly in the add/remove blocks for the event.

如果您所做的只是从另一个事件处理程序中触发事件处理程序,您可以省去中间人,直接在事件的添加/删除块中挂钩事件处理程序。

For example, if you have a UserControl with a "SaveButtonClick" event, and all you want to do when is call the event handler when the "SaveButton" on your UserControl is clicked, you can do this:

例如,如果您有一个带有“SaveButtonClick”事件的 UserControl,并且您想要做的就是在单击 UserControl 上的“SaveButton”时调用事件处理程序,您可以这样做:

public event EventHandler SaveButtonClick
{
    add { this.SaveButton.Click += value; }
    remove { this.SaveButton.Click -= value; }
}

Now you don't need any code to fire the SaveButtonClick event - it will automatically be fired when the SaveButton.Click event is raised (ie when someone clicks that button).

现在您不需要任何代码来触发 SaveButtonClick 事件 - 当 SaveButton.Click 事件被引发时(即当有人单击该按钮时)它会自动被触发。

回答by Hapkido

You can have a central channel that only support events. This channel must be independent so the layer only publish or subscribe to it.

您可以拥有一个仅支持事件的中央频道。该通道必须是独立的,因此该层只能发布或订阅它。

回答by Gishu

Unless I see a bit more of the design.. it'll be hard to give a good answer.

除非我看到更多的设计......很难给出一个好的答案。

WPF does bubble events up (automatically) the UI Component/Control tree... this has now been built into the framework. So I guess that's the recommended way :)

WPF 确实会(自动)在 UI 组件/控件树中冒泡事件……这现在已内置到框架中。所以我想这是推荐的方式:)

The trouble with bypassing the middle man Layer2 is that Layer1 and Layer3 now know each other... are coupled. So its a tradeoff.. if you are okay with the coupling.. eliminate the middle man / invent a specialized component with this responsibility. However if you expect Layer 3 to be hot-swappable (low coupling), I'd say continue bubbling.

绕过中间人 Layer2 的麻烦在于,Layer1 和 Layer3 现在相互认识……是耦合的。所以这是一个权衡……如果你对耦合没问题……消除中间人/发明一个有这个责任的专门组件。但是,如果您希望第 3 层可热插拔(低耦合),我会说继续冒泡。

回答by Michael L Perry

Take a look at Update Controls .NET. These controls discover the parts of your data model that they depend upon even through layers of business logic. You don't have to write anycode to notify them.

看看更新控件 .NET。这些控件甚至可以通过业务逻辑层发现它们所依赖的数据模型部分。您不必编写任何代码来通知他们。

回答by Bevan

Have a read of Jeremy Miller's blog "The Shade Tree Developer", especially his Write Your Own CABseries- the command pattern stuff he talks about there is probably what you need.

阅读 Jeremy Miller 的博客“The Shade Tree Developer”,尤其是他的Write Your Own CAB系列- 他谈到的命令模式内容可能正是您所需要的。

回答by Ben

Peter Rilling has posted a way of simulating Event Bubbling/Broadcasting in winforms. It is simple and effective.

Peter Rilling 发布了一种在 winforms 中模拟事件冒泡/广播的方法。它简单而有效。

http://www.codeproject.com/KB/cs/event_broadcast.aspx

http://www.codeproject.com/KB/cs/event_broadcast.aspx