I don't understand how extending EventArgs is useful.
我不明白扩展EventArgs是如何有用的。
public class ClickedEventArgs : EventArgs {
int x;
int y;
public ClickedEventArgs (int x, int y) {
this.x = x;
this.y = y;
}
public int X { get { return x; } }
public int Y { get { return y; } }
}
Code is above. How can i use this inheritance?Also; i want to learn how can i call this code blog from default.aspx
代码在上面。我怎样才能使用这种继承呢?我想学习如何从default.aspx调用此代码博客
3 个解决方案
#1
Are you asking why it's useful to derive from EventArgs
in the first place? I have to say that with C# 1 it didn't make a lot of sense, because of the way delegate conversion worked - but as of C# 2 it's more sensible. It allows an event handler to be registered with an event even if it doesn't care about the details of the event arguments.
你是否在问为什么从一开始就从EventArgs派生出来是有用的?我不得不说,使用C#1它没有多大意义,因为委托转换的工作方式 - 但从C#2开始,它更为明智。它允许事件处理程序向事件注册,即使它不关心事件参数的细节。
For example:
void LogEvent(object sender, EventArgs e)
{
Console.WriteLine("Event sent from " + sender);
}
...
textArea.KeyPress += LogEvent;
This works even though Control.KeyPress
is an event of type KeyPressEventHandler
. C# and .NET don't mind that the signature of LogEvent
doesn't exactly match the signature of KeyPressEventHandler
- it's compatible enough.
即使Control.KeyPress是KeyPressEventHandler类型的事件,这仍然有效。 C#和.NET并不介意LogEvent的签名与KeyPressEventHandler的签名不完全匹配 - 它足够兼容。
Admittedly this would still be feasible if we didn't have EventArgs
at all (you could just use object
) but given the EventArgs
class and the pattern, it makes sense to derive your own event arguments from EventArgs
.
不可否认,如果我们根本没有EventArgs(你可以只使用对象),这仍然是可行的,但是给定EventArgs类和模式,从EventArgs派生自己的事件参数是有意义的。
#2
Here is a example of how you might use your code:
以下是如何使用代码的示例:
public class MyClass () {
public event EventHandler<ClickedEventArgs> ClickedEvent = delegate {}; //Register the event
protected void SomethingWasClicked(int x, int y) {
ClickedEvent(this, new ClickedEventArgs(x,y)); //Invoke the event that is subscribed to
}
}
public class AnotherClass () {
public AnotherClass () {
MyClass mClass = new MyClass();
mClass.ClickedEvent += new EventHandler(mClass_clickedEvent);
}
protected void mClass_clickedEvent(object sender, ClickedEventArgs e) {
//Retrieve the X parameter that was passed from the MyClass instance
int x = e.X;
}
}
#3
What is really important here is that you can easily UPGRADE your event later to have MORE details and don't break existing decoupled listeners.
这里真正重要的是,您可以在以后轻松升级您的事件以获得更多详细信息,并且不会破坏现有的解耦侦听器。
#1
Are you asking why it's useful to derive from EventArgs
in the first place? I have to say that with C# 1 it didn't make a lot of sense, because of the way delegate conversion worked - but as of C# 2 it's more sensible. It allows an event handler to be registered with an event even if it doesn't care about the details of the event arguments.
你是否在问为什么从一开始就从EventArgs派生出来是有用的?我不得不说,使用C#1它没有多大意义,因为委托转换的工作方式 - 但从C#2开始,它更为明智。它允许事件处理程序向事件注册,即使它不关心事件参数的细节。
For example:
void LogEvent(object sender, EventArgs e)
{
Console.WriteLine("Event sent from " + sender);
}
...
textArea.KeyPress += LogEvent;
This works even though Control.KeyPress
is an event of type KeyPressEventHandler
. C# and .NET don't mind that the signature of LogEvent
doesn't exactly match the signature of KeyPressEventHandler
- it's compatible enough.
即使Control.KeyPress是KeyPressEventHandler类型的事件,这仍然有效。 C#和.NET并不介意LogEvent的签名与KeyPressEventHandler的签名不完全匹配 - 它足够兼容。
Admittedly this would still be feasible if we didn't have EventArgs
at all (you could just use object
) but given the EventArgs
class and the pattern, it makes sense to derive your own event arguments from EventArgs
.
不可否认,如果我们根本没有EventArgs(你可以只使用对象),这仍然是可行的,但是给定EventArgs类和模式,从EventArgs派生自己的事件参数是有意义的。
#2
Here is a example of how you might use your code:
以下是如何使用代码的示例:
public class MyClass () {
public event EventHandler<ClickedEventArgs> ClickedEvent = delegate {}; //Register the event
protected void SomethingWasClicked(int x, int y) {
ClickedEvent(this, new ClickedEventArgs(x,y)); //Invoke the event that is subscribed to
}
}
public class AnotherClass () {
public AnotherClass () {
MyClass mClass = new MyClass();
mClass.ClickedEvent += new EventHandler(mClass_clickedEvent);
}
protected void mClass_clickedEvent(object sender, ClickedEventArgs e) {
//Retrieve the X parameter that was passed from the MyClass instance
int x = e.X;
}
}
#3
What is really important here is that you can easily UPGRADE your event later to have MORE details and don't break existing decoupled listeners.
这里真正重要的是,您可以在以后轻松升级您的事件以获得更多详细信息,并且不会破坏现有的解耦侦听器。