了解C#中的事件和事件处理程序 [英] Understanding events and event handlers in C#

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

问题描述

我了解事件的目的,特别是在创建用户界面的上下文中。我认为这是创建事件的原型:

I understand the purpose of events, especially within the context of creating user interfaces. I think this is the prototype for creating an event:

public void EventName(object sender, EventArgs e);

事件处理程序做什么,为什么需要它,我如何创建一个? p>

What do event handlers do, why are they needed, and how do I to create one?

推荐答案

要理解事件处理程序,您需要了解代表。在 C#中,您可以将代理视为指针(或引用)一种方法。这是有用的,因为指针可以作为值传递。

To understand event handlers, you need to understand delegates. In C#, you can think of a delegate as a pointer (or a reference) to a method. This is useful because the pointer can be passed around as a value.

委托的中心概念是其签名或形状。那就是(1)返回类型和(2)输入参数。例如,如果我们创建一个委托 void MyDelegate(object sender,EventArgs e),它只能指向返回 void ,并采取对象 EventArgs 。像一个方形的洞和一个方形的钉子。所以我们说这些方法与代理具有相同的签名或形状。

The central concept of a delegate is its signature, or shape. That is (1) the return type and (2) the input arguments. For example, if we create a delegate void MyDelegate(object sender, EventArgs e), it can only point to methods which return void, and take an object and EventArgs. Kind of like a square hole and a square peg. So we say these methods have the same signature, or shape, as the delegate.

所以知道如何创建对方法的引用,让我们考虑事件的目的:当系统中其他地方出现某些情况时,我们希望导致执行某些代码 - 或者处理事件。为此,我们为要执行的代码创建特定的方法。事件和要执行的方法之间的粘合是代表。事件必须内部存储一个列表的指针,以便在事件发生时调用方法。*当然,为了能够调用一个方法,我们需要知道要传递什么参数!我们使用委托作为事件与所有具体方法之间的合同。

So knowing how to create a reference to a method, let's think about the purpose of events: we want to cause some code to be executed when something happens elsewhere in the system - or "handle the event". To do this, we create specific methods for the code we want to be executed. The glue between the event and the methods to be executed are the delegates. The event must internally store a "list" of pointers to the methods to call when the event is raised.* Of course, to be able to call a method, we need to know what arguments to pass to it! We use the delegate as the "contract" between the event and all the specific methods that will be called.

所以默认的 EventHandler (和许多喜欢它)代表一个具体的方法形式(再次,void / object-EventArgs)。当你宣布一个事件时,你就是通过指定一个委托来说明这个事件将调用什么形式的方法(EventHandler):

So the default EventHandler (and many like it) represents a specific shape of method (again, void/object-EventArgs). When you declare an event, you are saying which shape of method (EventHandler) that event will invoke, by specifying a delegate:

//This delegate can be used to point to methods
//which return void and take a string.
public delegate void MyEventHandler(string foo);

//This event can cause any method which conforms
//to MyEventHandler to be called.
public event MyEventHandler SomethingHappened;

//Here is some code I want to be executed
//when SomethingHappened fires.
void HandleSomethingHappened(string foo)
{
    //Do some stuff
}

//I am creating a delegate (pointer) to HandleSomethingHappened
//and adding it to SomethingHappened's list of "Event Handlers".
myObj.SomethingHappened += new MyEventHandler(HandleSomethingHappened);

(*这是.NET中事件的关键,并剥离了魔法 - 一个事件实际上,在封面下,只是一个相同形状的方法的列表,列表存储在事件的存在位置,当事件提出时,它只是通过这个方法列表并调用每个,使用这些值作为参数。分配事件处理程序只是将方法添加到要调用的方法列表中的更简单的方法。)

(*This is the key to events in .NET and peels away the "magic" - an event is really, under the covers, just a list of methods of the same "shape". The list is stored where the event lives. When the event is "raised", it's really just "go through this list of methods and call each one, using these values as the parameters". Assigning an event handler is just a prettier, easier way of adding your method to this list of methods to be called).

这篇关于了解C#中的事件和事件处理程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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