5

在下面的代码中,我需要知道在引发事件时传递两个字符串的语法。

 [PublishEvent("Click")]
 public event EventHandler<EventArgs<string>> MyEvent;

谢谢,撒克逊人。

4

2 回答 2

19

最干净的方法是创建自己的类,该类派生自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));
    }
于 2012-09-19T16:03:40.033 回答
2

为 EventArgs 创建你的类并扩展,并通过它

public class YourCustomeEvent : EventArgs
{
   public string yourVariable {get; }
}

现在你必须像这样提供你的自定义类

 public event EventHandler<YourCustomeEvent> MyEvent;
于 2012-09-19T16:04:29.943 回答