在下面的代码中,我需要知道引发事件时传递两个字符串的语法。
[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;