当前位置:网站首页>C# EventHandler观察者模式
C# EventHandler观察者模式
2022-07-23 04:29:00 【biyusr】
C# 和 java 比较:java 中使用的是接口。C# 使用委托机制,可以用时 + 运算符进行注册,直接多播。而 java 中是一般是使用一个集合来保存观察者。
发布者(Publisher)= 被观察者(Observable) = 事件源(java中的EventObject,C#中的sender)
订阅者(Subscriber)=观察者(Observer)= 接收者(java中继承EventLister,接口, 或Observer接口, C#由于委托机制,不需要继承接口,直接按EventHandler实现回调方法)
在发生其他类或对象关注的事情时,类或对象可通过事件通知它们。发送(或引发)事件的类称为“发行者”,接收(或处理)事件的类称为“订户”。在典型的 C# Windows 窗体或 Web 应用程序中,可订阅由控件(如按钮和列表框)引发的事件。可使用 Visual C# 集成开发环境 (IDE) 来浏览控件发布的事件,选择要处理的事件。IDE 会自动添加空事件处理程序方法和订阅事件的代码。
EventHandler 为 C# 中的预定义委托,专用于表示不生成数据的事件的事件的处理程序方法。
public delegate void EventHandler(Object sender, EventArgs e)事件具有以下特点:
1. 发行者确定何时引发事件,订户确定执行何种操作来响应该事件。
2. 一个事件可以有多个订户。一个订户可处理来自多个发行者的多个事件。
3. 没有订户的事件永远不会被调用。
4. 事件通常用于通知用户操作(如:图形用户界面中的按钮单击或菜单选择操作)。
5. 如果一个事件有多个订户,当引发该事件时,会同步调用多个事件处理程序。要异步调用事件,请参见使用异步方式调用同步方法。
6. 可以利用事件同步线程。
7. 在 .NET Framework 类库中,事件是基于 EventHandler 委托和 EventArgs 基类的。
下面的示例演示了上述步骤,它将自定义 EventArgs 类和 EventHandler 用作事件类型。
namespace ConsoleApplication2{using System;using System.Collections.Generic;// 自定义一个事件类来保存事件信息public class CustomEventArgs : EventArgs{public CustomEventArgs(string s){message = s;}private string message;public string Message{get { return message; }set { message = value; }}}// 广播事件的类class Publisher{// 使用 EventHandler<T> 声明一个事件public event EventHandler<CustomEventArgs> RaiseCustomEvent;//这个方法是做了一些事情。然后触发一个事件。public void DoSomething(){//DoSomething…………// 你也可以再触发事件前,执行一些其他的代码OnRaiseCustomEvent(new CustomEventArgs("Did something,hi 这是事件消息"));}//使用虚方法,让子类可以重写。to allow derived classes to override the event invocation behaviorprotected virtual void OnRaiseCustomEvent(CustomEventArgs e){// 定义一个局部变量,已防止最后一个订阅者刚好在检查null后取消订阅EventHandler<CustomEventArgs> handler = RaiseCustomEvent;// 如果没有 订阅者(观察者), 委托对象将为nullif (handler != null){// 格式化事件消息中 的字符串e.Message += String.Format(" at {0}", DateTime.Now.ToString());// 这是最重要的一句。// 此时执行的 handler已经是一个多播委托(如果有多个订阅者或观察者注册)。// 既然是多播委托,就可以依次调用各个 回调函数 (既然是回调函数,实际的执行就由订阅者类决定)。//这里面传入一个this, 就代表 事件源(或发布者 或 被观察者 都一个意思)handler(this, e);}}}//用来注册事件的类class Subscriber{private string id;public Subscriber(string ID, Publisher pub){id = ID;//注册这个动作,应该有订阅者主动进行,并且可以再后期取消注册pub.RaiseCustomEvent += HandleCustomEvent;}// 实现回调函数。在事件发生后,执行什么样的操作。这里只是简单的打印信息。void HandleCustomEvent(object sender, CustomEventArgs e){//这就是实际的操作。Console.WriteLine(id + " received this message: {0}", e.Message);}}class Class1{static void Main(string[] args){Publisher pub = new Publisher();Subscriber sub1 = new Subscriber("sub1", pub);Subscriber sub2 = new Subscriber("sub2", pub);// 调用这个方法来产生事件pub.DoSomething();Console.WriteLine("Press Enter to close this window.");Console.ReadLine();}}}
边栏推荐
- PyQt5_ Pyqtgraph mouse draws line segments on the line graph
- 数仓:工作流的设计以及优化实践
- Interest rate in installment payment
- Richview textbox items textbox
- redis 复制集群搭建
- RTC 性能自动化工具在内存优化场景下的实践
- Self operation and maintenance: a new sample of it operation and maintenance services in Colleges and Universities
- 2022/7/20
- What is the core essence of smart parks?
- Customer first | domestic Bi leader, smart software completes round C financing
猜你喜欢

智慧园区的核心本质是什么?

SQLZOO——SELECT from WORLD Tutorial
![[c #] IEnumerable enumerable type interface analysis yield](/img/08/8c346ce257b4adc0bea80bf05b6f52.png)
[c #] IEnumerable enumerable type interface analysis yield

Kingbasees SQL language reference manual of Jincang database (8. Function (7))

NFT数字藏品版权如何保护?

Jmeter-记一次自动化造数引发的BeanShell写入excel实例

SPR:SUPERVISED PERSONALIZED RANKING BASED ON PRIOR KNOWLEDGE FOR RECOMMENDATION

What is instant messaging? Development of instant messaging

Cloudcompare & PCL point cloud point matching (based on point to face distance)

添加信任列表
随机推荐
第一章概述-------第一节--1.2互联网概述
kex_exchange_identification: read: Connection reset by peer 不完美解决办法(之一)
32.< tag-数组和位运算>补充: lt.剑指 Offer 56 - I. 数组中数字出现的次数
千亿营收之后,阿里云生态有了新打法
After 100 billion of revenue, Alibaba cloud ecosystem has a new way to play
元宇宙浪潮震撼来袭,抓住时机,齐心协力
Chapter2 Standard Output
CloudCompare&PCL 点云点匹配(基于点到面的距离)
7. < tag dynamic programming and stock trading Collection> lt.121. The best time to buy and sell stocks + lt.122. The best time to buy and sell stocks II + lt.123. The best time to buy and sell stocks
【Warning】YOLOV5训练时的ignoring corrupt image/label: [Errno 2].....,无法全部训练数据集,快速带你解决它
Richview textbox items textbox
Seektiger's okaleido has a big move. Will the STI of ecological pass break out?
数据湖:Delta Lake介绍
Response对象
PowerBI入门指南
Kingbasees SQL language reference manual of Jincang database (8. Function (I))
[c #] IEnumerable enumerable type interface analysis yield
Kingbasees SQL language reference manual of Jincang database (8. Function (6))
Self operation and maintenance: a new sample of it operation and maintenance services in Colleges and Universities
CV (3)- CNNs