/* * Step 4. Event source owned the event,so should define an event trigger method inside the event source, * So please don't forget to generate an action to trigger event * 第四步. 事件要能发送消息,请别忘了在事件拥有者中建一个方法用于事件触发 */ public void EventSourceAction4TriggerEvent() { // Before you do something make sure the event handler existed! if (this.delegateEventHandler != null) { // Pass event parameters,we will generate Event Args properties class at Step 7 // 传入事件参数,当然现在还没有Event args的属性类,我们将在第7步定义一个Event参数属性类 EventNameEventArgs e = new EventNameEventArgs(); e.FirstArgs = "You deal wiht 1st args"; e.SecondArgs = "You deal with 2nd args"; // Event handler invoke. // 利用事件处理器调用。 this.delegateEventHandler.Invoke(this, e); } }
public void Action(HereIsEventSource eventSource, EventNameEventArgs e) { System.Console.WriteLine("do something!"); }
// Due to subscribe event via delegate, so the method's signature must same as eventhandler delegate // - eventSource tells EventSubscriber do for which Event Source // - e tells EventSubscriber the action should base on which parameters public void SubscriberDoSomthing(HereIsEventSource eventSource, EventNameEventArgs e) { System.Console.WriteLine($"Event Suberscriber do something after Event source trigger the event!"); System.Console.WriteLine($"Do something with 1st agrs of EventNameEventArgs: {e.FirstArgs} "); System.Console.WriteLine($"Do something with 2nd agrs of EventNameEventArgs: {e.SecondArgs} "); }