为什么我们需要从eventarg派生 [英] why do we need to derive from eventarg

查看:131
本文介绍了为什么我们需要从eventarg派生的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好

i花了这三天试图真正了解事件及其工作原理,现在我知道它们有什么好处以及如何使用它们但我不能理解的是技术时间一个类派生自eventargs,正如我在aricles中可以看到的那样,我试图解释这个,我们总是可以工作并得到相同的结果而不从这个类派生.MSDN没有帮助。



你能否发现这个Eventargs背后的真相或建议文章提及这个课程的好处?



谢谢你

hi everyone
i spent this three days trying to really understand events and how they work,now i know are they benefits and how to use them but what i can''t understand is the technic when a class derive from eventargs,as i can see in the aricles i could find that trying to explain this,we can always work and have the same result without deriving from this class.MSDN wasn''t helpful.

can you please uncover the truth behind this Eventargs or suggests articles to mention the benefit of this class?

thanks any way

推荐答案

对于那些可以添加和理解文档的人来说,MSDN对.NET非常有用。



当你得到相同的结果时,你对这种情况的考虑是明显的虚假证据。谁告诉你,你应该总是从 System.EventArgs 类派生出来?对于许多事件,实际上并不需要。当事件被调用的代码(只能在声明事件的类中可能)传递给事件处理程序时,这些事件就是事件。



没有传递信息时事件的例子抛出 EventArgs (如你所见,我不算数事件处理程序的第一个参数,对象发送者总是传递并且很少使用),是像 Button.Click 这样的事件。对于事件处理程序,除了单击某个按钮这一事实外,没有什么是重要的。对于按钮功能的逻辑,通过空格键的鼠标实际点击,或者通过选择按钮然后按Enter键,它是如何点击并不重要;如果使用鼠标,鼠标指针在按钮的客户端内的位置并不重要。这样的方法非常好,因为它有助于统一各种点击以同样的方式处理。



要查找派生 EventArgs 类的案例非常重要,那就是足以看看许多可用的事件。例如,让我们看看事件 System.Windows.Control.MouseDown

http://msdn.microsoft.com/en-us/library/system.windows.forms.control.mousedown .aspx [ ^ ]。



如您所见,处理程序的签名由类型 MouseHandler ,可以定义为 System.EventArgs< System.Windows.Forms.MouseEventArgs>

http://msdn.microsoft.com/en-us/library/system.windows.forms.mouseeventargs。 aspx [ ^ ]。



正如你所看到的,这个继承自 System.EventArgs 的类型提供了其他属性,以传递有关单击鼠标按钮,鼠标指针位置等信息。反过来,此类型用作其他事件参数类型的基类型,用于更专业的事件,例如树节点等的事件。当然,也会传递一些特定于控件的信息。



根据事件声明,事件处理程序的签名包含<$ c $类型的第二个参数c> System.Windows.Forms.MouseEventArgs




MSDN is perfectly helpful in case of .NET, for those who can add and understand documentation.

Your considerations about the case when you "have the same result" is the bright case of false proof. Who told you that you should always derive from System.EventArgs class? For many events, it is really not needed. Those are the events when no information need to be passed from the code where the event is invoked (which is possible only in the class declaring the event), to the event handler.

The example of the events when no information is passed throw EventArgs (as you can see, I don''t count the first parameter of the event handler, Object sender which is always passed and rarely used), is the event like Button.Click. For the event handler, nothing is important except the fact that some button is just clicked. For the logic on the button functionality, it is not important, how it was "clicked", by a real click of a mouse of by blank space key, or, maybe, by selecting the button and then hitting Enter; if a mouse was used, the position of mouse pointer inside the button''s client are is not important. Such approach is very good, because it helps to unify all kinds of "click" to handle then in the same way.

To find an example of the cases when deriving of the EventArgs class is really important, it''s enough to take a look at on of the many available events. Let''s, for example, look at the event System.Windows.Control.MouseDown:
http://msdn.microsoft.com/en-us/library/system.windows.forms.control.mousedown.aspx[^].

As you can see, the signature of the handler is defined by the type MouseHandler, which can be defined as System.EventArgs<System.Windows.Forms.MouseEventArgs>:
http://msdn.microsoft.com/en-us/library/system.windows.forms.mouseeventargs.aspx[^].

As you can see, this type inherited from System.EventArgs provides additional properties to pass information on the mouse button which was clicked, mouse pointer location, and so on. In turn, this type is used as a base type to other event arguments types used for more specialized events, such as the ones for a tree node, etc.; naturally, some control-specific information is also passed.

According to the event declaration, the signature of the event handler includes the second parameter of the type System.Windows.Forms.MouseEventArgs:

myControl.MouseDown += delegate(object sender, System.Windows.Forms.MouseEventArgs eventArgs) {
   // use eventArgs.Button, if required
   // use eventArgs.Location, if required
   // and so on...
};





我故意不使用事件的lambda形式上面代码示例中的处理程序,用于显式地演示处理程序的签名。使用lambda,可以使用类型推断(使用C#v.3及更高版本):





I intentionally did not use lambda form of the event handler in the above code sample, to demonstrate the signature of the handler explicitly. With lambda, type inference can be used (with C# v.3 and above):

myControl.MouseDown += (sender, eventArgs) => {
   // use eventArgs.Button, if required
   // use eventArgs.Location, if required
   // and so on...
};





当您需要在自己的类中创建自定义事件时,可以使用其中一个可用的事件参数类。但是,如果需要通过传递特定于类的某些参数来处理事件,则需要声明自己的事件参数类并向其添加一些新属性。基类可以是 System.EventArgs ,但并非总是如此,有时你可以为你的基类使用一些更多派生的事件参数类。

< br $> b $ b



了解使用委托类型 System.EventArgs 或通用委托类型 System.EventArgs<> 只是推荐的做法,而不是CLR和语法正式要求的。但是,这是强烈推荐的事件模式(但不能用于其他委托实例,可以使用任何委托类型)。



请参阅下面我的评论答案。



-SA



When you need to create a custom event in your own class, you can use one of the available event argument classes. However, if you need to handle your event with passing some parameters specific to your class, you would need to declare your own event argument class and add some new properties to it. The base class can be System.EventArgs, but not always, sometime you can use some more derived event argument classes for your base class.



It''s important to understand that the use of the delegate type System.EventArgs or the generic delegate type System.EventArgs<> is just a recommended practice and not formally required by CLR and syntax. However, this is the highly recommended pattern for events (but not for other delegate instances, which can use any delegate types).

Please see my comments below to the answer.

—SA


这篇关于为什么我们需要从eventarg派生的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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