ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

如何发布符合 .NET 准则的事件

2021-06-06 20:04:26  阅读:232  来源: 互联网

标签:EventHandler 自定义 CustomEventArgs 准则 event 事件 符合 NET public


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

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

尽管定义的类中的事件可基于任何有效委托类型,甚至是返回值的委托,但一般还是建议使用 EventHandler 使事件基于 .NET 模式,如下例中所示。

名称 EventHandler 可能导致一些混淆,因为它不会实际处理事件。 EventHandler 和泛型 EventHandler<TEventArgs> 均为委托类型。 其签名与委托定义匹配的方法或 Lambda 表达式是事件处理程序,并将在引发事件时调用。

发布基于 EventHandler 模式的事件

  1. (如果无需随事件一起发送自定义数据,请跳过此步骤转到步骤 3a。)将自定义数据的类声明为对发布服务器和订阅者类均可见的范围。 然后添加所需成员以保留自定义事件数据。 在此示例中,将返回一个简单的字符串。

    public class CustomEventArgs : EventArgs
    {
        public CustomEventArgs(string message)
        {
            Message = message;
        }
    
        public string Message { get; set; }
    }

     

  2. (如果使用的是泛型版本 EventHandler<TEventArgs>,请跳过此步骤。)声明发布类中的委托。 为其指定以 EventHandler 结尾的名称。 第二个参数指定自定义 EventArgs 类型。
    public delegate void CustomEventHandler(object sender, CustomEventArgs args);
  3. 使用下列步骤之一来声明发布类中的事件。

    1. 如果没有任何自定义 EventArgs 类,事件类型将为非泛型 EventHandler 委托。 你无需声明该委托,因为它已在创建 C# 项目时包括的 System 命名空间中声明。 将以下代码添加到发布服务器类。

      public event EventHandler RaiseCustomEvent;

       

    2. 如果使用非泛型版本 EventHandler 并且具有派生自 EventArgs 的自定义类,请声明发布类中的事件,并将步骤 2 中的委托用作类型。
      public event CustomEventHandler RaiseCustomEvent;

       

    3. 如果使用泛型版本,则无需自定义委托。 而是在发布类中,将事件类型指定为 EventHandler<CustomEventArgs>,替换尖括号中自定义类的名称。
      public event EventHandler<CustomEventArgs> RaiseCustomEvent;

      示例

      下例通过使用自定义 EventArgs 类和 EventHandler<TEventArgs> 作为事件类型来演示之前的步骤。

      using System;
      
      namespace DotNetEvents
      {
          // Define a class to hold custom event info
          public class CustomEventArgs : EventArgs
          {
              public CustomEventArgs(string message)
              {
                  Message = message;
              }
      
              public string Message { get; set; }
          }
      
          // 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("Event triggered"));
              }
      
              // 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> raiseEvent = RaiseCustomEvent;
      
                  // Event will be null if there are no subscribers
                  if (raiseEvent != null)
                  {
                      // Format the string to send inside the CustomEventArgs parameter
                      e.Message += $" at {DateTime.Now}";
      
                      // Call to raise the event.
                      raiseEvent(this, e);
                  }
              }
          }
      
          //Class that subscribes to an event
          class Subscriber
          {
              private readonly string _id;
      
              public Subscriber(string id, Publisher pub)
              {
                  _id = id;
      
                  // Subscribe to the event
                  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: {e.Message}");
              }
          }
      
          class Program
          {
              static void Main()
              {
                  var pub = new Publisher();
                  var sub1 = new Subscriber("sub1", pub);
                  var sub2 = new Subscriber("sub2", pub);
      
                  // Call the method that raises the event.
                  pub.DoSomething();
      
                  // Keep the console window open
                  Console.WriteLine("Press any key to continue...");
                  Console.ReadLine();
              }
          }
      }

       

标签:EventHandler,自定义,CustomEventArgs,准则,event,事件,符合,NET,public
来源: https://www.cnblogs.com/88223100/p/how-to-publish-events-that-conform-to-net-framework-gui

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有