问题描述
予理解事件的目的,特别是在创建用户界面的上下文中。我认为这是原型创建一个事件:
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)的输入参数。例如,如果我们创建一个委托无效MyDelegate(对象发件人,EventArgs五)
,它只能点到返回的方法无效
,并采取对象
和 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.
因此,了解如何创建引用的方法,让我们想想事件的目的:我们希望引起时,一些在其他地方发生在系统中的一些code到执行 - 或处理事件。要做到这一点,我们创造,我们要执行的code的具体方法。事件,也可以执行的方法之间的胶水是代表。该事件必须指针的名单内部存储的方法,当事件触发时调用。*。当然,要能够调用一个方法,我们需要知道传递给它什么论据!我们使用委托的事件和所有将被调用的具体方法之间的契约。
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.
那么默认的事件处理程序
(和许多喜欢它)重新presents一个的方法的具体形状的(同样,空/对象 - EventArgs的)。当你声明一个事件,你说的该方法的形状的(事件处理程序),该事件将调用,通过指定一个委托:
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中的事件和果皮的法宝 - 一个事件是真的,在幕后,同样的形的方法,只是一个清单列表存储在那里的活动活着。当事件被调高,它实际上只是办理方法这个名单,并呼吁各一台,使用这些值作为参数。分配的事件处理程序仅仅是一个prettier,增加了更简单的方法你被调用的方法的方法列表)。
(*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#中的事件和事件处理程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!