在下面的代码中,我需要知道在引发事件时传递两个字符串的语法。
[PublishEvent("Click")]
public event EventHandler<EventArgs<string>> MyEvent;
谢谢,撒克逊人。
在下面的代码中,我需要知道在引发事件时传递两个字符串的语法。
[PublishEvent("Click")]
public event EventHandler<EventArgs<string>> MyEvent;
谢谢,撒克逊人。
最干净的方法是创建自己的类,该类派生自EventArgs
:
public class MyEventArgs : EventArgs
{
private readonly string _myFirstString;
private readonly string _mySecondString;
public MyEventArgs(string myFirstString, string mySecondString)
{
_myFirstString = myFirstString;
_mySecondString = mySecondString;
}
public string MyFirstString
{
get { return _myFirstString; }
}
public string MySecondString
{
get { return _mySecondString; }
}
}
并像这样使用它:
public event EventHandler<MyEventArgs> MyEvent;
要引发事件,您可以执行以下操作:
protected virtual void OnMyEvent(string myFirstString, string mySecondString)
{
EventHandler<MyEventArgs> handler = MyEvent;
if (handler != null)
handler(this, new MyEventArgs(myFirstString, mySecondString));
}
为 EventArgs 创建你的类并扩展,并通过它
public class YourCustomeEvent : EventArgs
{
public string yourVariable {get; }
}
现在你必须像这样提供你的自定义类
public event EventHandler<YourCustomeEvent> MyEvent;