事件概述
事件的訂閱和取消訂閱
發布標准事件
引發派生類中的基類事件
BaseClassEvents { System; System.Collections.Generic; ShapeEventArgs : EventArgs { newArea; ShapeEventArgs( d) { newArea = d; } NewArea { { newArea; } } } Shape { area; Area { { area; } { area = value; } } EventHandler<ShapeEventArgs> ShapeChanged; Draw(); OnShapeChanged(ShapeEventArgs e) { EventHandler<ShapeEventArgs> handler = ShapeChanged; (handler != ) { handler(, e); } } } Circle : Shape { radius; Circle( d) { radius = d; area = 3.14 * radius; } Update( d) { radius = d; area = 3.14 * radius; OnShapeChanged( ShapeEventArgs(area)); } OnShapeChanged(ShapeEventArgs e) { .OnShapeChanged(e); } Draw() { Console.WriteLine(); } } Rectangle : Shape { length; width; Rectangle( length, width) { .length = length; .width = width; area = length * width; } Update( length, width) { .length = length; .width = width; area = length * width; OnShapeChanged( ShapeEventArgs(area)); } OnShapeChanged(ShapeEventArgs e) { .OnShapeChanged(e); } Draw() { Console.WriteLine(); } } ShapeContainer { List<Shape> _list; ShapeContainer() { _list = List<Shape>(); } AddShape(Shape s) { _list.Add(s); s.ShapeChanged += HandleShapeChanged; } HandleShapeChanged( sender, ShapeEventArgs e) { Shape s = (Shape)sender; Console.WriteLine(, e.NewArea); s.Draw(); } } Test { Main([] args) { Circle c1 = Circle(54); Rectangle r1 = Rectangle(12, 9); ShapeContainer sc = ShapeContainer(); sc.AddShape(c1); sc.AddShape(r1); c1.Update(57); r1.Update(7, 7); Console.WriteLine(); Console.WriteLine(); Console.ReadLine(); } } }
實現接口事件
WrapTwoInterfaceEvents { System; IDrawingObject { EventHandler OnDraw; } IShape { EventHandler OnDraw; } Shape : IDrawingObject, IShape { EventHandler PreDrawEvent; EventHandler PostDrawEvent; EventHandler IDrawingObject.OnDraw { add { PreDrawEvent += value; } remove { PreDrawEvent -= value; } } EventHandler IShape.OnDraw { add { PostDrawEvent += value; } remove { PostDrawEvent -= value; } } Draw() { EventHandler handler = PreDrawEvent; (handler != ) { handler(, EventArgs()); } Console.WriteLine(); handler = PostDrawEvent; (handler != ) { handler(, EventArgs()); } } } Subscriber1 { Subscriber1(Shape shape) { IDrawingObject d = (IDrawingObject)shape; d.OnDraw += EventHandler(d_OnDraw); } d_OnDraw( sender, EventArgs e) { Console.WriteLine(); } } Subscriber2 { Subscriber2(Shape shape) { IShape d = (IShape)shape; d.OnDraw += EventHandler(d_OnDraw); } d_OnDraw( sender, EventArgs e) { Console.WriteLine(); } } Program { Main([] args) { Shape shape = Shape(); Subscriber1 sub = Subscriber1(shape); Subscriber2 sub2 = Subscriber2(shape); shape.Draw(); Console.WriteLine(); Console.ReadLine(); } } }
使用字典存儲事件實例
EventHandler1( i); EventHandler2( s); PropertyEventsSample { System.Collections.Generic.Dictionary<, System.Delegate> eventTable; PropertyEventsSample() { eventTable = System.Collections.Generic.Dictionary<, System.Delegate>(); eventTable.Add(, ); eventTable.Add(, ); } EventHandler1 Event1 { add { eventTable[] = (EventHandler1)eventTable[] + value; } remove { eventTable[] = (EventHandler1)eventTable[] - value; } } EventHandler2 Event2 { add { eventTable[] = (EventHandler2)eventTable[] + value; } remove { eventTable[] = (EventHandler2)eventTable[] - value; } } RaiseEvent1( i) { EventHandler1 handler1; ( != (handler1 = (EventHandler1)eventTable[])) { handler1(i); } } RaiseEvent2( s) { EventHandler2 handler2; ( != (handler2 = (EventHandler2)eventTable[])) { handler2(s); } } } TestClass { Delegate1Method( i) { System.Console.WriteLine(i); } Delegate2Method( s) { System.Console.WriteLine(s); } Main() { PropertyEventsSample p = PropertyEventsSample(); p.Event1 += EventHandler1(TestClass.Delegate1Method); p.Event1 += EventHandler1(TestClass.Delegate1Method); p.Event1 -= EventHandler1(TestClass.Delegate1Method); p.RaiseEvent1(2); p.Event2 += EventHandler2(TestClass.Delegate2Method); p.Event2 += EventHandler2(TestClass.Delegate2Method); p.Event2 -= EventHandler2(TestClass.Delegate2Method); p.RaiseEvent2(); } }
事件的異步模式