Syntax to send two strings in eventArgs

拟墨画扇 提交于 2019-12-19 07:33:10

问题


In the following code I need to know the syntax of passing two strings when the event is raised.

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

Thanks, Saxon.


回答1:


The cleanest way is to create your own class that derives from 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; }
        }
    }

And use it like this:

public event EventHandler<MyEventArgs> MyEvent;

To raise the event, you can do something like this:

    protected virtual void OnMyEvent(string myFirstString, string mySecondString)
    {
        EventHandler<MyEventArgs> handler = MyEvent;
        if (handler != null)
            handler(this, new MyEventArgs(myFirstString, mySecondString));
    }



回答2:


Make your class and extend for EventArgs, and pass it

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

Now you have to provide your custom class like this

 public event EventHandler<YourCustomeEvent> MyEvent;


来源:https://stackoverflow.com/questions/12498551/syntax-to-send-two-strings-in-eventargs

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!