C# 是否可以在不创建派生类的情况下将数据传递给 EventArgs?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1942621/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
It is possible to pass data to EventArgs without creating derived class?
提问by Thomas
I am a bit confused. I know I can create class derived from EventArgs in order to have custom event data. But can I employ the base class EventArgs somehow? Like the mouse button click, in the subscriber method, there is always "EventArgs e" parameter. Can I create somehow the method that will pass data this way, I mean they will be passed in the base Eventargs?
我有点困惑。我知道我可以创建从 EventArgs 派生的类以获得自定义事件数据。但是我可以以某种方式使用基类 EventArgs 吗?就像鼠标按钮点击一样,在订阅者方法中,总是有“EventArgs e”参数。我能否以某种方式创建以这种方式传递数据的方法,我的意思是它们将在基本 Eventargs 中传递?
采纳答案by Chris Haas
Is it possible to just use the EventArgs
datatype raw? Absolutely. According to MSDN:
是否可以只使用EventArgs
原始数据类型?绝对地。根据 MSDN:
This class contains no event data; it is used by events that do not pass state information to an event handler when an event is raised. If the event handler requires state information, the application must derive a class from this class to hold the data.
此类不包含事件数据;它由在引发事件时不将状态信息传递给事件处理程序的事件使用。如果事件处理程序需要状态信息,应用程序必须从这个类派生一个类来保存数据。
http://msdn.microsoft.com/en-us/library/system.eventargs.aspx
http://msdn.microsoft.com/en-us/library/system.eventargs.aspx
private event TestEventEventHandler TestEvent;
private delegate void TestEventEventHandler(EventArgs e);
private void button1_Click(object sender, EventArgs e)
{
TestEvent += TestEventHandler;
if (TestEvent != null)
{
TestEvent(new EventArgs());
}
}
private void TestEventHandler(EventArgs e)
{
System.Diagnostics.Trace.WriteLine("hi");
}
Should you ever do it?Not for any reason I can think if. If you want to create your own clicks you can just instantiate the MouseEventArgs
on your own, too:
你应该这样做吗?不是出于任何原因我能想到如果。如果您想创建自己的点击,您也可以自己实例化MouseEventArgs
:
MouseEventArgs m = new MouseEventArgs(System.Windows.Forms.MouseButtons.Left, 1, 42, 42, 1);
回答by nitzmahone
Nope. The EventArgs base class is just a way to allow for some standard event delegate types. Ultimately, to pass data to a handler, you'll need to subclass EventArgs. You could use the sender arg instead, but that should really be the object that fired the event.
不。EventArgs 基类只是一种允许某些标准事件委托类型的方法。最终,要将数据传递给处理程序,您需要对 EventArgs 进行子类化。您可以改用 sender arg,但这应该是触发事件的对象。
回答by jupi
You can use the EventArgs class through the Generic Types approach. In this sample, i will use the Rect class with as return type:
您可以通过通用类型方法使用 EventArgs 类。在此示例中,我将使用 Rect 类作为返回类型:
public EventHandler<Rect> SizeRectChanged;
Raising the event:
引发事件:
if(SizeRectChanged != null){
Rect r = new Rect(0,0,0,0);
SizeRectChanged(this,r);
}
Listening the event:
监听事件:
anyElement.SizeRectChanged += OnSizeRectChanged;
public void OnSizeRectChanged(object sender, Rect e){
//TODO abything using the Rect class
e.Left = e.Top = e.Width = e.Height = 50;
}
So, don't need to create new events classes or delegates, simply create a EventHandler passing the specific type T.
因此,不需要创建新的事件类或委托,只需创建一个传递特定类型 T 的 EventHandler。