简体   繁体   English

.Net事件-除onClick以外的事件在事件中使用什么(对象发送者,EventArgs e)参数

[英].Net Events - What use (object sender, EventArgs e) parameters give in Events other than onClick

I did not find .NET Events - What are object sender & EventArgs e? 我没有找到.NET事件-什么是对象发送者和EventArgs e? post much helpful. 发布很有帮助。

It primarily tells that how the parameters (object sender, EventArgs e) are used in case of onClick scenario, which turns out to be the obvious use. 它主要说明在onClick情况下如何使用参数(对象发送者,EventArgs e),这是很明显的用法。

My question is : 我的问题是:

Neglecting the onClick function 忽略onClick功能

ie In case of Page_Load, Init and other page events 即在Page_Load,Init和其他页面事件的情况下

What is the use of these (object sender, EventArgs e) parameters ? 这些(对象发送者,EventArgs e)参数的用途是什么?

Examples would be more helpful :-) 例子会更有用:-)

Well it's pretty simple: 好吧,这很简单:

object sender is the entity which emitted the event - in case of page_load i'm pretty sure it is the page itself, because the page is emitting the event (so this == sender should be true in the page). object sender是发出事件的实体-在page_load的情况下,我很确定它是页面本身,因为页面正在发出事件(所以this == sender在页面中应为true)。 EventArgs e is boring (it's a base class), but other events bring more interesting classes which tell something about the event, so a onMouseDown-Event for example will bring data about the mouse position on screen. EventArgs e很无聊(它是一个基类),但是其他事件带来了一些更有趣的类,这些类可以告诉您有关该事件的信息,因此,例如onMouseDown-Event将带来有关鼠标在屏幕上的位置的数据。 Other events might bring other data - but most times the Event itself and the sender are enough to get all data needed. 其他事件可能会带来其他数据-但大多数情况下,事件本身和发送方足以获取所需的所有数据。

Using standard(similar) signature such as (sender, eventargs) is beneficial because 使用标准的(类似)签名,例如(sender, eventargs)是有益的,因为

  • It's flexible and generic enough to server many scenarios. 它足够灵活且通用,可以处理多种情况。 Acts as a standard template even for novices 即使是新手也可以充当标准模板
  • Similar signature increases the familiarity with the patterns, helps in reducing learning time for beginners 相似的签名增加了对模式的熟悉度,有助于减少初学者的学习时间
  • Enables re-usability at both event delegate level as well as handler level. 在事件委托级别和处理程序级别均启用重用性。

Now, once you accept/agree some standards, you need to stick them (even though in certain cases, it may not make sense provided that there is no huge cost associated with). 现在,一旦您接受/同意某些标准,就需要遵守这些标准(即使在某些情况下,只要没有与此相关的巨额成本,这可能就没有意义了)。 .NET Fx developers has decided on this standard event template and that what's you will see everywhere. .NET Fx开发人员已决定使用此标准事件模板,并且到处都会看到什么。

Let's take an example of Page_Load - this event is actually declared at Control level . 让我们以Page_Load为例-该事件实际上是在Control级别声明的。 So its possible that some code may choose to handle multiple control's load event using the same event handler and use sender argument to apply specific things. 因此,某些代码可能选择使用同一事件处理程序来处理多个控件的load事件,并使用sender参数来应用特定内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM