C#类成员-事件

         类或对象可以通过事件向其他类或对象通知发生的相关事情。发送(或引发)事件的类称为“发行者”,接收(或处理)事件的类称为“订户”。

       在典型的 C# Windows 窗体或 Web 应用程序中,可订阅由控件(如按钮和列表框)引发的事件。可使用 Visual C# 集成开发环境 (IDE) 来浏览控件发布的事件,选择要处理的事件。IDE 会自动添加空事件处理程序方法和订阅事件的代码。

事件具有以下特点:

·发行者确定何时引发事件,订户确定执行何种操作来响应该事件。

·一个事件可以有多个订户。一个订户可处理来自多个发行者的多个事件。

      ·没有订户的事件永远也不会引发。

      ·事件通常用于通知用户操作,例如,图形用户界面中的按钮单击或菜单选择操作。

      ·如果一个事件有多个订户,当引发该事件时,会同步调用多个事件处理程序。要异步调用事件,请参见使用异步方式调用同步方法。

      ·可以利用事件同步线程。

·在 .NET Framework 类库中,事件是基于 EventHandler 委托和 EventArgs 基类的。

 

一、如何:订阅和取消订阅事件

如果您想编写引发事件时调用的自定义代码,则可以订阅由其他类发布的事件。例如,可以订阅某个按钮的 click 事件,以使应用程序在用户单击该按钮时执行一些有用的操作。

1、使用 Visual Studio IDE 订阅事件

·如果看不到“属性”窗口,请在“设计”视图中,右键单击要为其创建事件处理程序的窗体或控件,然后选择“属性”。

·在“属性”窗口的顶部,单击“事件”图标。

·双击要创建的事件,例如 Load 事件。

      ·Visual C# 会创建一个空事件处理程序方法,并将其添加到您的代码中。或者,您也可以在“代码”视图中手动添加代码。例如,下面的代码行声明了一个在 Form 类引发 Load 事件时调用的事件处理程序方法。

private void Form1_Load(object sender, System.EventArgs e)

{

    // Add your form load event handling code here.

}

订阅该事件所需的代码行也会在项目的 Form1.Designer.cs 文件的 InitializeComponent 方法中自动生成。该代码行类似于:

this.Load += new System.EventHandler(this.Form1_Load);

 

2、以编程方式订阅事件:

2.1、定义一个事件处理程序方法,其签名与该事件的委托签名匹配。例如,如果事件基于 EventHandler 委托类型,则下面的代码表示方法存根:

void HandleCustomEvent(object sender, CustomEventArgs a)

{

     // Do something useful here.

}

      2.2、使用加法赋值运算符 (+=) 来为事件附加事件处理程序。在下面的示例中,假设名为 publisher 的对象拥有一个名为 RaiseCustomEvent 的事件。请注意,订户类需要引用发行者类才能订阅其事件。

publisher.RaiseCustomEvent += HandleCustomEvent;

请注意,前面的语法是 C# 2.0 中的新语法。此语法完全等效于必须使用 new 关键字显式创建封装委托的 C# 1.0 语法:

publisher.RaiseCustomEvent += new CustomEventHandler(HandleCustomEvent);

还可以通过使用 lambda 表达式添加事件处理程序:

public Form1()

{

    InitializeComponent();

    // Use a lambda expression to define an event handler.

    this.Click += (s,e) => { MessageBox.Show(

       ((MouseEventArgs)e).Location.ToString());};

}

3、使用匿名方法订阅事件

如果以后不必取消订阅某个事件,则可以使用加法赋值运算符 (+=) 将匿名方法附加到此事件。在下面的示例中,假设名为 publisher 的对象拥有一个名为 RaiseCustomEvent 的事件,并且还定义了一个 CustomEventArgs 类以承载某些类型的专用事件信息。请注意,订户类需要引用 publisher 才能订阅其事件。

publisher.RaiseCustomEvent += delegate(object o, CustomEventArgs e)

{

    string s = o.ToString() + " " + e.ToString();

    Console.WriteLine(s);

};

请务必注意,如果使用匿名函数订阅事件,事件的取消订阅过程将比较麻烦。这种情况下若要取消订阅,必须返回到该事件的订阅代码,将该匿名方法存储在委托变量中,然后将此委托添加到该事件中。一般来说,如果必须在后面的代码中取消订阅某个事件,则建议您不要使用匿名函数订阅此事件。

4、取消订阅

要防止在引发事件时调用事件处理程序,请取消订阅该事件。要防止资源泄露,应在释放订户对象之前取消订阅事件。在取消订阅事件之前,在发布对象中作为该事件的基础的多路广播委托会引用封装了订户的事件处理程序的委托。只要发布对象保持该引用,垃圾回收功能就不会删除订户对象。

5、取消订阅事件

使用减法赋值运算符 (-=) 取消订阅事件:

publisher.RaiseCustomEvent -= HandleCustomEvent;

所有订户都取消订阅事件后,发行者类中的事件实例将设置为 null

 

二、如何:发布符合 .NET Framework 准则的事件

      下面的过程演示了如何将符合标准 .NET Framework 模式的事件添加到您自己的类和结构中。.NET Framework 类库中的所有事件均基于 EventHandler 委托,定义如下:

public delegate void EventHandler(object sender, EventArgs e);

      虽然您定义的类中的事件可基于任何有效委托类型(甚至是可返回值的委托),但是,通常建议您使用 EventHandler 让事件基于 .NET Framework 模式,如下面的示例所示。

1、采用 EventHandler 模式发布事件

1.1、(如果不需要与事件一起发送自定义数据,请跳过此步骤,进入步骤 3a。)在发行者类和订户类均可看见的范围中声明类,并添加保存自定义事件数据所需的成员。在此示例中,会返回一个简单字符串。

public class CustomEventArgs : EventArgs

{

    public CustomEventArgs(string s)

    {

        msg = s;

    }

    private string msg;

    public string Message

    {

        get { return msg; }

    }

}

      1.2、(如果您使用的是 EventHandler<(Of <(TEventArgs>)>) 的泛型版本,请跳过此步骤。)在发布类中声明一个委托。为它指定以 EventHandler 结尾的名称。第二个参数指定自定义 EventArgs 类型。

public delegate void CustomEventHandler(object sender, CustomEventArgs a);

      1.3、使用以下任一步骤,在发布类中声明事件。

1.3.1、如果没有自定义 EventArgs 类,事件类型就是非泛型 EventHandler 委托。它无需声明,因为它已在创建 C# 项目时包含的 System 命名空间中进行了声明:

public event EventHandler RaiseCustomEvent;

1.3.2、如果使用的是 EventHandler 的非泛型版本,并且您有一个由 EventArgs 派生的自定义类,请在发布类中声明您的事件,并且将您的委托用作类型:

           class Publisher

{

              public event CustomEventHandler RaiseCustomEvent;

}

1.3.3、如果使用的是泛型版本,则不需要自定义委托。相反,应将事件类型指定为 EventHandler<CustomEventArgs>,在尖括号内放置您自己的类的名称。

      public event EventHandler<CustomEventArgs> RaiseCustomEvent;

      1.4、示例

下面的示例通过将自定义 EventArgs 类和 EventHandler<(Of <(TEventArgs>)>) 用作事件类型来演示上述步骤。

namespace DotNetEvents

{

    using System;

    using System.Collections.Generic;

 

    // Define a class to hold custom event info

    public class CustomEventArgs : EventArgs

    {

        public CustomEventArgs(string s)

        {

            message = s;

        }

        private string message;

 

        public string Message

        {

            get { return message; }

            set { message = value; }

        }

    }

 

    // Class that publishes an event

    class Publisher

    {

 

        // Declare the event using EventHandler<T>

        public event EventHandler<CustomEventArgs> RaiseCustomEvent;

 

        public void DoSomething()

        {

            // Write some code that does something useful here

            // then raise the event. You can also raise an event

            // before you execute a block of code.

            OnRaiseCustomEvent(new CustomEventArgs("Did something"));

 

        }

 

        // Wrap event invocations inside a protected virtual method

        // to allow derived classes to override the event invocation behavior

        protected virtual void OnRaiseCustomEvent(CustomEventArgs e)

        {

            // Make a temporary copy of the event to avoid possibility of

            // a race condition if the last subscriber unsubscribes

            // immediately after the null check and before the event is raised.

            EventHandler<CustomEventArgs> handler = RaiseCustomEvent;

 

            // Event will be null if there are no subscribers

            if (handler != null)

            {

                // Format the string to send inside the CustomEventArgs parameter

                e.Message += String.Format(" at {0}", DateTime.Now.ToString());

 

                // Use the () operator to raise the event.

                handler(this, e);

            }

        }

    }

 

    //Class that subscribes to an event

    class Subscriber

    {

        private string id;

        public Subscriber(string ID, Publisher pub)

        {

            id = ID;

            // Subscribe to the event using C# 2.0 syntax

            pub.RaiseCustomEvent += HandleCustomEvent;

        }

 

        // Define what actions to take when the event is raised.

        void HandleCustomEvent(object sender, CustomEventArgs e)

        {

            Console.WriteLine(id + " received this message: {0}", e.Message);

        }

    }

 

    class Program

    {

        static void Main(string[] args)

        {

            Publisher pub = new Publisher();

            Subscriber sub1 = new Subscriber("sub1", pub);

            Subscriber sub2 = new Subscriber("sub2", pub);

 

            // Call the method that raises the event.

            pub.DoSomething();

 

            // Keep the console window open

            Console.WriteLine("Press Enter to close this window.");

            Console.ReadLine();

 

        }

    }

}

 

三、如何:在派生类中引发基类事件

以下简单示例演示了在基类中声明可从派生类引发的事件的标准方法。此模式广泛应用于 .NET Framework 类库中的 Windows 窗体类。

在创建可用作其他类的基类的类时,应考虑如下事实:事件是特殊类型的委托,只可以从声明它们的类中调用。派生类无法直接调用基类中声明的事件。尽管有时需要事件仅由基类引发,但在大多数情形下,应该允许派生类调用基类事件。为此,您可以在包含该事件的基类中创建一个受保护的调用方法。通过调用或重写此调用方法,派生类便可以间接调用该事件。

      说明:

      不要在基类中声明虚拟事件,也不要在派生类中重写这些事件。在 Microsoft Visual Studio 2008 C# 编译器无法正确处理这些事件,并且无法预知派生事件的订户是否真正订阅了基类事件。

例子:

namespace BaseClassEvents

{

    using System;

    using System.Collections.Generic;

 

    // Special EventArgs class to hold info about Shapes.

    public class ShapeEventArgs : EventArgs

    {

        private double newArea;

 

        public ShapeEventArgs(double d)

        {

            newArea = d;

        }

        public double NewArea

        {

            get { return newArea; }

        }

    }

 

    // Base class event publisher

    public abstract class Shape

    {

        protected double area;

 

        public double Area

        {

            get { return area; }

            set { area = value; }

        }

        // The event. Note that by using the generic EventHandler<T> event type

        // we do not need to declare a separate delegate type.

        public event EventHandler<ShapeEventArgs> ShapeChanged;

 

        public abstract void Draw();

 

        //The event-invoking method that derived classes can override.

        protected virtual void OnShapeChanged(ShapeEventArgs e)

        {

            // Make a temporary copy of the event to avoid possibility of

            // a race condition if the last subscriber unsubscribes

            // immediately after the null check and before the event is raised.

            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)

        {

            // Do any circle-specific processing here.

 

            // Call the base class event invocation method.

            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)

        {

            // Do any rectangle-specific processing here.

 

            // Call the base class event invocation method.

            base.OnShapeChanged(e);

        }

        public override void Draw()

        {

            Console.WriteLine("Drawing a rectangle");

        }

 

    }

 

    // Represents the surface on which the shapes are drawn

    // Subscribes to shape events so that it knows

    // when to redraw a shape.

    public class ShapeContainer

    {

        List<Shape> _list;

 

        public ShapeContainer()

        {

            _list = new List<Shape>();

        }

 

        public void AddShape(Shape s)

        {

            _list.Add(s);

            // Subscribe to the base class event.

            s.ShapeChanged += HandleShapeChanged;

        }

 

        // ...Other methods to draw, resize, etc.

 

        private void HandleShapeChanged(object sender, ShapeEventArgs e)

        {

            Shape s = (Shape)sender;

 

            // Diagnostic message for demonstration purposes.

            Console.WriteLine("Received event. Shape area is now {0}", e.NewArea);

 

            // Redraw the shape here.

            s.Draw();

        }

    }

 

    class Test

    {

 

        static void Main(string[] args)

        {

            //Create the event publishers and subscriber

            Circle c1 = new Circle(54);

            Rectangle r1 = new Rectangle(12, 9);

            ShapeContainer sc = new ShapeContainer();

 

            // Add the shapes to the container.

            sc.AddShape(c1);

            sc.AddShape(r1);

 

            // Cause some events to be raised.

            c1.Update(57);

            r1.Update(7, 7);

 

            // Keep the console window open in debug mode.

            System.Console.WriteLine("Press any key to exit.");

            System.Console.ReadKey();

        }

    }

}

/* Output:

        Received event. Shape area is now 178.98

        Drawing a circle

        Received event. Shape area is now 49

        Drawing a rectangle

 */

 

四、如何:实现接口事件

      接口可声明事件。下面的示例演示如何在类中实现接口事件。实现接口事件的规则与实现任何接口方法或属性的规则基本相同。

1、在类中实现接口事件

在类中声明事件,然后在适当的区域调用该事件。

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

    {

        // Raise this event before drawing

        // the object.

        event EventHandler OnDraw;

    }

    public interface IShape

    {

        // Raise this event after drawing

        // the shape.

        event EventHandler OnDraw;

    }

 

 

    // Base class event publisher inherits two

    // interfaces, each with an OnDraw event

    public class Shape : IDrawingObject, IShape

    {

        // Create an event for each interface event

        event EventHandler PreDrawEvent;

        event EventHandler PostDrawEvent;

 

        object objectLock = new Object();

 

        // Explicit interface implementation required.

        // Associate IDrawingObject's event with

        // PreDrawEvent

        event EventHandler IDrawingObject.OnDraw

        {

            add

            {

                lock (objectLock)

                {

                    PreDrawEvent += value;

                }

            }

            remove

            {

                lock (objectLock)

                {

                    PreDrawEvent -= value;

                }

            }

        }

        // Explicit interface implementation required.

        // Associate IShape's event with

        // PostDrawEvent

        event EventHandler IShape.OnDraw

        {

            add

            {

                lock (objectLock)

                {

                    PostDrawEvent += value;

                }

            }

            remove

            {

                lock (objectLock)

                {

                    PostDrawEvent -= value;

                }

            }

 

 

        }

 

        // For the sake of simplicity this one method

        // implements both interfaces.

        public void Draw()

        {

            // Raise IDrawingObject's event before the object is drawn.

            EventHandler handler = PreDrawEvent;

            if (handler != null)

            {

                handler(this, new EventArgs());

            }

            Console.WriteLine("Drawing a shape.");

 

            // RaiseIShape's event after the object is drawn.

            handler = PostDrawEvent;

            if (handler != null)

            {

                handler(this, new EventArgs());

            }

        }

    }

    public class Subscriber1

    {

        // References the shape object as an IDrawingObject

        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.");

        }

    }

    // References the shape object as an IShape

    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();

 

            // Keep the console window open in debug mode.

            System.Console.WriteLine("Press any key to exit.");

            System.Console.ReadKey();

        }

    }

 

}

/* Output:

    Sub1 receives the IDrawingObject event.

    Drawing a shape.

    Sub2 receives the IShape event.

*/

 

五、如何:使用字典存储事件实例

      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

        {

            lock (eventTable)

            {

                eventTable["Event1"] = (EventHandler1)eventTable["Event1"] + value;

            }

        }

        remove

        {

            lock (eventTable)

            {

                eventTable["Event1"] = (EventHandler1)eventTable["Event1"] - value;

            }

        }

    }

 

    public event EventHandler2 Event2

    {

        add

        {

            lock (eventTable)

            {

                eventTable["Event2"] = (EventHandler2)eventTable["Event2"] + value;

            }

        }

        remove

        {

            lock (eventTable)

            {              

                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");

 

        // Keep the console window open in debug mode.

        System.Console.WriteLine("Press any key to exit.");

        System.Console.ReadKey();

    }

}

/* Output:

    2

    TestString

*/

 

六、如何:实现自定义事件访问器

      事件是特殊的多路广播委托,仅能从声明该事件的类中调用。客户端代码通过提供对引发事件时要调用的方法的引用来订阅事件。这些方法通过事件访问器添加到委托的调用列表中,事件访问器类似于属性访问器,不同之处在于事件访问器被命名为 add remove。在大多数情况下都不需要提供自定义的事件访问器。如果您在代码中没有提供自定义的事件访问器,编译器会自动添加事件访问器。但在某些情况下,您可能需要提供自定义行为。

 

示例

下面的示例演示如何实现自定义的 add remove 事件访问器。虽然可以替换这些访问器内的任何代码,但建议您在添加或移除新的事件处理程序方法之前先锁定该事件。

event EventHandler IDrawingObject.OnDraw

{

     add

    {

         lock (PreDrawEvent)

      {

               PreDrawEvent += value;

         }

     }

     remove

     {

          lock (PreDrawEvent)

         {

               PreDrawEvent -= value;

         }

     }

}

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值