代表和活动的关系 [英] Delegates and events relationship

查看:45
本文介绍了代表和活动的关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好专家!!



我对代表和活动很困惑。委托与事件之间的关系是什么?



我理解的是Delegate只是一种方法指针,用于调用具有相同参数和返回类型的委托的事件处理程序方法。 br />


请从头开始精心解释它杀了我。

提前谢谢

Hello Experts!!

I am quite confused with delegates and events. What is the relationship between delegate and event?

What I understand is " Delegate is just a method pointer sort of stuff which is used to invoke an event handler method having same parameters and return type that of delegate.

Please explain elaborately from the scratch as its killing me.
Thanks in advance

推荐答案

好的:从另一个方面来看......



这是一个构建和使用的事件:

OK: look at it the other way...

Here is an event, constructed and used:
/// <summary>
/// Event to indicate My Test Event has occurred
/// </summary>
public event EventHandler MyTest;
/// <summary>
/// Called to signal to subscribers that My Test Event has occurred
/// </summary>
/// <param name="e"></param>
protected virtual void OnMyTest(EventArgs e)
    {
    EventHandler eh = MyTest;
    if (eh != null)
        {
        eh(this, e);
        }
    }
private void frmMain_Load(object sender, EventArgs e)
    {
    MyTest += new EventHandler(frmMain_MyTest);
    }

void frmMain_MyTest(object sender, EventArgs e)
    {
    //Handle the event
    }

private void myTestButton_Click(object sender, EventArgs e)
    {
    OnMyTest(null);
    }





事件是发生某事并且应用程序需要处理它的信号。

当用户按下测试!时按钮事件通过使用null EventArgs调用OnMyTest方法来发出信号。



在方法中,检索事件的委托 - 这是一个列表函数指针如果你喜欢并检查它是否至少分配了一个处理程序。

如果有,则调用Delegate,系统查看列表并调用每个处理程序list,依次将Form实例和EventArgs传递给每个。然后运行处理程序方法。



这就是为什么处理程序需要具有与委托相同的返回类型和参数 - 或者系统不知道传递它们的内容。



因此,Event是一种特殊类型的Delegate,具有特定的方法签名和一些额外的处理。它使用Delegate来操作事件。



这有帮助吗?



An Event is a signal that something has happened and the application needs to process it.
When the user presses the "Test!" button the event is signalled, by calling the OnMyTest method with a null EventArgs.

In the method, the Delegate for the event is retrieved - this is a list of "function pointers" if you like and checked to see it there is a minimum of one handler assigned.
If there is, then the Delegate is called and the system looks at the list and calls each handler on the list, passing the Form instance and the EventArgs to each in turn. The handler method then runs.

This is why Handlers need to have the same return type and parameters as the Delegate - or the system would not know what to pass them.

So an Event is a special type of Delegate with a specific method signature and a little extra processing. It uses the Delegate to action the event.

Does that help at all?


检查此链接以获取解释

http://msdn.microsoft.com/en -us / library / 17sde2xt%28v = vs.71%29.aspx [ ^ ]



例如查看此链接:

http://msdn.microsoft.com/en-us/ library / wkzf914z%28v = vs.71%29.aspx [ ^ ]
check this link for explanation
http://msdn.microsoft.com/en-us/library/17sde2xt%28v=vs.71%29.aspx[^]

for example look this link:
http://msdn.microsoft.com/en-us/library/wkzf914z%28v=vs.71%29.aspx[^]


查看这些链接



< a href =http://www.codeproject.com/Articles/834/Delegates-And-Events-The-Uncensored-Story-Part-1>代表和事件 - 未经审查的故事 - 第1部分 [ ^ ]







http://stackoverflow.com/questions/9487053/what-is-the-relationship-between-delegates-and-events [ ^ ]
check these Links

Delegates And Events - The Uncensored Story - Part 1[^]

and

http://stackoverflow.com/questions/9487053/what-is-the-relationship-between-delegates-and-events[^]


这篇关于代表和活动的关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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