.Net事件
.NET 事件
事件概述
在發生其他類或對象關注的事情時,類或對象可通過事件通知它們。發送(或引發)事件的類稱為“發行者”,接收(或處理)事件的類稱為“訂戶”。
- 特點
- 發行者確定何時引發事件,訂戶確定執行何種操作來響應該事件。
- 一個事件可以有多個訂戶。一個訂戶可處理來自多個發行者的多個事件。
- 沒有訂戶的事件永遠不會被調用。
- 事件通常用于通知用戶操作
- 如果一個事件有多個訂戶,當引發該事件時,會同步調用多個事件處理程序,也可以設置異步調用事件。
- 可以利用事件同步線程。
- 事件是基于 EventHandler 委托和 EventArgs 基類的。
事件的訂閱和取消
- 訂閱事件
- VS IDE 訂閱事件
- 如果“屬性”窗口不可見,請在“設計”視圖中,右擊要創建事件處理程序的窗體或控件,然后選擇“屬性”。
- 在“屬性”窗口的頂部,單擊“事件”圖標。
- 雙擊要創建的事件,Visual C# 會創建一個空事件處理程序方法,并將其添加到您的代碼中。或者,您也可以在“代碼”視圖中手動添加代碼。
- VS IDE 訂閱事件
- 編程方式訂閱事件
-
定義一個事件處理程序方法,其簽名與該事件的委托簽名匹配。例如,如果事件基于 EventHandler 委托類型,則下面的代碼表示方法存根
-
- oid HandleCustomEvent(object sender, CustomEventArgs a){ }
-
使用加法賦值運算符 (+=) 來為事件附加事件處理程序。在下面的示例中,假設名為 publisher 的對象擁有一個名為 RaiseCustomEvent 的事件。請注意,訂戶類需要引用發行者類才能訂閱其事件。
-
- publisher.RaiseCustomEvent += HandleCustomEvent;
- publisher.RaiseCustomEvent += new CustomEventHandler(HandleCustomEvent);
- 匿名方法訂閱事件
- 使用加法賦值運算符 (+=) 來為事件附加匿名方法。在下面的示例中,假設名為 publisher 的對象擁有一個名為 RaiseCustomEvent 的事件,并且還定義了一個 CustomEventArgs 類以承載某些類型的專用事件信息。請注意,訂戶類需要引用 publisher 才能訂閱其事件。
- publisher.RaiseCustomEvent += delegate(object o, CustomEventArgs e)
- {
- string s = o.ToString() + " " + e.ToString();
- Console.WriteLine(s);
- };
- 取消訂閱
要防止在引發事件時調用事件處理程序,您只需取消訂閱該事件。要防止資源泄露,請在釋放訂戶對象之前取消訂閱事件,這一點很重要。在取消訂閱事件之前,在發布對象中作為該事件的基礎的多路廣播委托會引用封裝了訂戶的事件處理程序的委托。只要發布對象包含該引用,就不會對訂戶對象執行垃圾回收。
使用減法賦值運算符 (-=) 取消訂閱事件。所有訂戶都取消訂閱某事件后,發行者類中的事件實例會設置為 null。
- publisher.RaiseCustomEvent -= HandleCustomEvent;
發布標準事件
下面的過程演示了如何將符合標準 .NET Framework 模式的事件添加到您自己的類和結構中。.NET Framework 類庫中的所有事件均基于 EventHandler 委托,定義如下。
- public delegate void EventHandler(object sender, EventArgs e);
- 采用 EventHandler 模式發布事件
- (如果不需要發送含事件的自定義數據,請跳過此步驟,直接進入步驟 3。)在發行者類和訂戶類均可看見的范圍中聲明類,并添加保留自定義事件數據所需的成員。在此示例中,會返回一個簡單字符串。
- public class CustomEventArgs : EventArgs
- {
- public CustomEventArgs(string s)
- {
- msg = s;
- }
- private string msg;
- public string Message
- {
- get { return msg; }
- }
- }
- (如果您使用的是 EventHandler 的泛型版本,請跳過此步驟。)在發布類中聲明一個委托。為它指定以 EventHandler 結尾的名稱。第二個參數指定自定義 EventArgs 類型。
- public delegate void CustomEventHandler(object sender, CustomEventArgs a);
- 如果沒有自定義 EventArgs 類,事件類型就是非泛型 EventHandler 委托。它無需聲明,因為它已在 C# 項目默認包含的 System 命名空間中進行了聲明
- public event EventHandler RaiseCustomEvent;
- class Publisher
- {
- public event CustomEventHandler RaiseCustomEvent;
- }
- public event EventHandler<CustomEventArgs> RaiseCustomEvent;
#p#
引發派生類中的基類事件
以下簡單示例演示了在基類中聲明可從派生類引發的事件的標準方法。此模式廣泛應用于 .NET Framework 基類庫中的 Windows 窗體類。
在創建可用作其他類的基類的類時,必須考慮如下事實:事件是特殊類型的委托,只可以從聲明它們的類中調用。派生類無法直接調用基類中聲明的事件。盡管有時您可能希望某個事件只能通過基類引發,但在大多數情形下,您應該允許派生類調用基類事件。為此,您可以在包含該事件的基類中創建一個受保護的調用方法。通過調用或重寫此調用方法,派生類便可以間接調用該事件。
- namespace BaseClassEvents
- {
- using System;
- using System.Collections.Generic;
- public class ShapeEventArgs : EventArgs
- {
- private double newArea;
- public ShapeEventArgs(double d)
- {
- newArea = d;
- }
- public double NewArea
- {
- get { return newArea; }
- }
- }
- public abstract class Shape
- {
- protected double area;
- public double Area
- {
- get { return area; }
- set { area = value; }
- }
- public event EventHandler<ShapeEventArgs> ShapeChanged;
- public abstract void Draw();
- protected virtual void OnShapeChanged(ShapeEventArgs e)
- {
- EventHandler<ShapeEventArgs> handler = ShapeChanged;
- if (handler != null)
- {
- handler(this, e);
- }
- }
- }
- public class Circle : Shape
- {
- private double radius;
- public Circle(double d)
- {
- radius = d;
- area = 3.14 * radius;
- }
- public void Update(double d)
- {
- radius = d;
- area = 3.14 * radius;
- OnShapeChanged(new ShapeEventArgs(area));
- }
- protected override void OnShapeChanged(ShapeEventArgs e)
- {
- base.OnShapeChanged(e);
- }
- public override void Draw()
- {
- Console.WriteLine("Drawing a circle");
- }
- }
- public class Rectangle : Shape
- {
- private double length;
- private double width;
- public Rectangle(double length, double width)
- {
- this.length = length;
- this.width = width;
- area = length * width;
- }
- public void Update(double length, double width)
- {
- this.length = length;
- this.width = width;
- area = length * width;
- OnShapeChanged(new ShapeEventArgs(area));
- }
- protected override void OnShapeChanged(ShapeEventArgs e)
- {
- base.OnShapeChanged(e);
- }
- public override void Draw()
- {
- Console.WriteLine("Drawing a rectangle");
- }
- }
- public class ShapeContainer
- {
- List<Shape> _list;
- public ShapeContainer()
- {
- _list = new List<Shape>();
- }
- public void AddShape(Shape s)
- {
- _list.Add(s);
- s.ShapeChanged += HandleShapeChanged;
- }
- private void HandleShapeChanged(object sender, ShapeEventArgs e)
- {
- Shape s = (Shape)sender;
- Console.WriteLine("Received event. Shape area is now {0}", e.NewArea);
- s.Draw();
- }
- }
- class Test
- {
- static void Main(string[] args)
- {
- Circle c1 = new Circle(54);
- Rectangle r1 = new Rectangle(12, 9);
- ShapeContainer sc = new ShapeContainer();
- sc.AddShape(c1);
- sc.AddShape(r1);
- c1.Update(57);
- r1.Update(7, 7);
- Console.WriteLine();
- Console.WriteLine("Press Enter to exit");
- Console.ReadLine();
- }
- }
- }
#p#
實現接口事件
接口可聲明事件。下面的示例演示如何在類中實現接口事件。接口事件的實現規則與任何接口方法或屬性的實現規則基本相同。
- 在類中實現接口事件
在類中聲明事件,然后在適當的位置調用該事件。
- public interface IDrawingObject
- {
- event EventHandler ShapeChanged;
- }
- public class MyEventArgs : EventArgs {…}
- public class Shape : IDrawingObject
- {
- event EventHandler ShapeChanged;
- void ChangeShape()
- {
- // Do something before the event…
- OnShapeChanged(new MyEventsArgs(…));
- // or do something after the event.
- }
- protected virtual void OnShapeChanged(MyEventArgs e)
- {
- if(ShapeChanged != null)
- {
- ShapeChanged(this, e);
- }
- }
- }
下面的示例演示如何處理以下的不常見情況:您的類是從兩個以上的接口繼承的,每個接口都含有同名事件)。在這種情況下,您至少要為其中一個事件提供顯式接口實現。為事件編寫顯式接口實現時,必須編寫 add 和 remove 事件訪問器。這兩個事件訪問器通常由編譯器提供,但在這種情況下編譯器不能提供。
您可以提供自己的訪問器,以便指定這兩個事件是由您的類中的同一事件表示,還是由不同事件表示。例如,根據接口規范,如果事件應在不同時間引發,則可以將每個事件與類中的一個單獨實現關聯。在下面的示例中,訂戶將形狀引用強制轉換為 IShape 或 IDrawingObject,從而確定自己將會接收哪個 OnDraw 事件。
- namespace WrapTwoInterfaceEvents
- {
- using System;
- public interface IDrawingObject
- {
- event EventHandler OnDraw;
- }
- public interface IShape
- {
- event EventHandler OnDraw;
- }
- public class Shape : IDrawingObject, IShape
- {
- event EventHandler PreDrawEvent;
- event EventHandler PostDrawEvent;
- event EventHandler IDrawingObject.OnDraw
- {
- add { PreDrawEvent += value; }
- remove { PreDrawEvent -= value; }
- }
- event EventHandler IShape.OnDraw
- {
- add { PostDrawEvent += value; }
- remove { PostDrawEvent -= value; }
- }
- public void Draw()
- {
- EventHandler handler = PreDrawEvent;
- if (handler != null)
- {
- handler(this, new EventArgs());
- }
- Console.WriteLine("Drawing a shape.");
- handler = PostDrawEvent;
- if (handler != null)
- {
- handler(this, new EventArgs());
- }
- }
- }
- public class Subscriber1
- {
- public Subscriber1(Shape shape)
- {
- IDrawingObject d = (IDrawingObject)shape;
- d.OnDraw += new EventHandler(d_OnDraw);
- }
- void d_OnDraw(object sender, EventArgs e)
- {
- Console.WriteLine("Sub1 receives the IDrawingObject event.");
- }
- }
- public class Subscriber2
- {
- public Subscriber2(Shape shape)
- {
- IShape d = (IShape)shape;
- d.OnDraw += new EventHandler(d_OnDraw);
- }
- void d_OnDraw(object sender, EventArgs e)
- {
- Console.WriteLine("Sub2 receives the IShape event.");
- }
- }
- public class Program
- {
- static void Main(string[] args)
- {
- Shape shape = new Shape();
- Subscriber1 sub = new Subscriber1(shape);
- Subscriber2 sub2 = new Subscriber2(shape);
- shape.Draw();
- Console.WriteLine("Press Enter to close this window.");
- Console.ReadLine();
- }
- }
- }
使用字典存儲事件實例
accessor-declarations 的一種用法是公開大量的事件但不為每個事件分配字段,而是使用字典來存儲這些事件實例。這只有在具有非常多的事件、但您預計大部分事件都不會實現時才有用。
- public delegate void EventHandler1(int i);
- public delegate void EventHandler2(string s);
- public class PropertyEventsSample
- {
- private System.Collections.Generic.Dictionary<string, System.Delegate> eventTable;
- public PropertyEventsSample()
- {
- eventTable = new System.Collections.Generic.Dictionary<string, System.Delegate>();
- eventTable.Add("Event1", null);
- eventTable.Add("Event2", null);
- }
- public event EventHandler1 Event1
- {
- add
- {
- eventTable["Event1"] = (EventHandler1)eventTable["Event1"] + value;
- }
- remove
- {
- eventTable["Event1"] = (EventHandler1)eventTable["Event1"] - value;
- }
- }
- public event EventHandler2 Event2
- {
- add
- {
- eventTable["Event2"] = (EventHandler2)eventTable["Event2"] + value;
- }
- remove
- {
- eventTable["Event2"] = (EventHandler2)eventTable["Event2"] - value;
- }
- }
- internal void RaiseEvent1(int i)
- {
- EventHandler1 handler1;
- if (null != (handler1 = (EventHandler1)eventTable["Event1"]))
- {
- handler1(i);
- }
- }
- internal void RaiseEvent2(string s)
- {
- EventHandler2 handler2;
- if (null != (handler2 = (EventHandler2)eventTable["Event2"]))
- {
- handler2(s);
- }
- }
- }
- public class TestClass
- {
- public static void Delegate1Method(int i)
- {
- System.Console.WriteLine(i);
- }
- public static void Delegate2Method(string s)
- {
- System.Console.WriteLine(s);
- }
- static void Main()
- {
- PropertyEventsSample p = new PropertyEventsSample();
- p.Event1 += new EventHandler1(TestClass.Delegate1Method);
- p.Event1 += new EventHandler1(TestClass.Delegate1Method);
- p.Event1 -= new EventHandler1(TestClass.Delegate1Method);
- p.RaiseEvent1(2);
- p.Event2 += new EventHandler2(TestClass.Delegate2Method);
- p.Event2 += new EventHandler2(TestClass.Delegate2Method);
- p.Event2 -= new EventHandler2(TestClass.Delegate2Method);
- p.RaiseEvent2("TestString");
- }
- }
事件的異步模式
有多種方式可向客戶端代碼公開異步功能?;谑录漠惒侥J綖轭愐幎擞糜陲@示異步行為的建議方式。對于相對簡單的多線程應用程序,BackgroundWorker 組件提供了一個簡單的解決方案。對于更復雜的異步應用程序,請考慮實現一個符合基于事件的異步模式的類。
- “在后臺”執行耗時任務(例如下載和數據庫操作),但不會中斷您的應用程序。
- 同時執行多個操作,每個操作完成時都會接到通知。
- 等待資源變得可用,但不會停止(“掛起”)您的應用程序。
- 使用熟悉的事件和委托模型與掛起的異步操作通信。