冒泡事件。 [英] Bubbling up events .

查看:176
本文介绍了冒泡事件。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个应用程序的多个层,我发现有冒泡事件做状态栏的变化等方面的GUI层自己。 。我发现自己不必编写重复编码,其中每一层简单的预订来自下层事件,然后在回调只是引发一个事件链向上。是否有这样做的更有效的方法?

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?

推荐答案

如果你正在做的是从另一个事件处理程序触发一个事件处理程序,你可以切出中间人和直接附加挂钩事件处理程序/删除块事件。

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.

例如,如果你有一个用户控件一个 SaveButtonClick事件,以及所有你想做的事时,调用事件处理程序时,上点击你的用户控件SaveButton,你可以这样做:

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:

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

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

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).

这篇关于冒泡事件。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆