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

查看:33
本文介绍了了解 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);

事件处理程序的作用是什么,为什么需要它们,我该如何创建?

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的方法,并取一个objectEventArgs.有点像方孔和方钉.所以我们说这些方法与委托具有相同的签名或形状.

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

//To raise the event within a method.
SomethingHappened("bar");

(*这是 .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天全站免登陆