2018-05-01 10:49:47

委托是一种类型,而事件是一个类或结构的成员,如同字段,属性。必须在类或结构中声明。

引申 -  Observe 观察者模式  Publish/Subscribe 发布-订阅模式

一 基本形式

1. 如上图所示,声明委托,类中构建 事件 及 触发代码

 1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5 using System.Threading.Tasks;
6
7 namespace EventDemo
8 {
9 // Event 1 of 5 elements -- deletegate declare
10 delegate void Handler();
11
12 // Publisher
13 // 1. Event declare
14 // 2. fire event code
15 class Incrementer
16 {
17 public event Handler eventPub; // create event
18
19 // 触发事件的代码
20 public void DoIncrement()
21 {
22 for(int i = 0; i < 100; i++)
23 {
24 if (i % 12 == 0 && eventPub != null)
25 eventPub();
26 }
27 }
28
29 }
30 }

2. 订阅者中 创建事件处理程序 及 注册事件。

 1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5 using System.Threading.Tasks;
6
7 namespace EventDemo
8 {
9 /*
10 * 订阅者
11 * 1.事件处理程序
12 * 2.构造器中注入 Publisher,构造代码中给事件添加处理程序方法。订阅事件。
13 */
14 class subscribeCode
15 {
16 public int DozoneCount { get; private set; }
17
18 // 事件处理程序
19 void IncrementCount()
20 {
21 DozoneCount++;
22 }
23
24 // 构造器 订阅事件
25 public subscribeCode(Incrementer inc)
26 {
27 DozoneCount = 0;
28 inc.eventPub += IncrementCount;
29 }
30
31 }
32 }

二 EventHandler

public vodi EventHandler ( Object sender, EventArgs e)

(this, null ) 来匹配一致

三 需要传输数据时 自定义 EventArgs 派生类。

1. 首先定义 派生子 EventArgs 的类

 1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5 using System.Threading.Tasks;
6
7 namespace EventDemo.CustomerDefine
8 {
9 /* public void EventHandler(Object sender, EventArgs e)
10 * EventArgs 被设计为不能传递任何参数,
11 * 需要声明派生自 EventArgs 的类,自定义保存数据的字段。
12 *
13 * 命名规范 以 Args结尾
14 */
15 public class CustomerEventArgs : EventArgs
16 {
17 public int DestinateData { get; set; }
18 }
19 }

自定义 EventArgs 派生类

2. 其余步骤同上,注意定义 事件处理程序时

1)返回值为 void。

2) Publisher

3)Subscribe

 1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5 using System.Threading.Tasks;
6
7 namespace EventDemo.CustomerDefine
8 {
9 class SubscribeClass
10 {
11 public int transferData { get; set; }
12
13 public void AccountMethod(Object send, CustomerEventArgs e)
14 {
15 Console.WriteLine("{0} transfer data,/t {1} send",
16 e.DestinateData, send.ToString());
17 transferData++;
18 }
19 public SubscribeClass(PublisherClass pc)
20 {
21 transferData = 0;
22 pc.handler += AccountMethod;
23 }
24 }
25 }

注意 移除事件处理程序 是从最后的寻找第一个匹配的,类似于 委托的方法移除

高级用法,  事件访问器

将 +=   -= 改变。   让事件执行任何我们希望的自定义代码。

两个访问器 add  remove

声明事件访问器 类似于 声明一个属性  返回 void

public event  EventHandler  CountedADozen

{

  add

  { ... }   // 执行 += 运算符

  remove

  { ... }   // 执行 -= 运算符

}

声明之后,事件不包含任何内嵌委托对象。必须实现自己的机制来存储和移除时间注册的方法。

Software--C#--grammer_Delegate--Event的更多相关文章

  1. How Google Tests Software - The Life of a TE

    By James WhittakerThe Test Engineer is a newerrole within Google than either SWEs or SETs. As such, ...

  2. PatentTips - Virtualizing performance counters

    BACKGROUND Generally, the concept of virtualization in information processing systems allows multipl ...

  3. PatentTips - Virtual machine management using processor state information

    BACKGROUND OF THE INVENTION The invention generally relates to virtual machine management, and more ...

  4. Let Start

      A free timing software with very small memory occupation. This tool is a pure green convenient off ...

  5. HDU-1171 Big Event in HDU

    Big Event in HDU Problem Description Nowadays, we all know that Computer College is the biggest depa ...

  6. Big Event in HDU

    Description Nowadays, we all know that Computer College is the biggest department in HDU. But, maybe ...

  7. 组合数学 - 母函数的变形 --- hdu 1171:Big Event in HDU

    Big Event in HDU Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others ...

  8. HDU 1171 Big Event in HDU 多重背包二进制优化

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1171 Big Event in HDU Time Limit: 10000/5000 MS (Jav ...

  9. Big Event in HDU[HDU1171]

    Big Event in HDU Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others ...

  10. java event

    What is an Event? Change in the state of an object is known as event i.e. event describes the change ...

随机推荐

  1. BC3-牛牛学说话之-整数

    题目描述 牛牛刚刚出生,嗷嗷待哺,一开始他只能学说简单的数字,你跟他说一个整数,他立刻就能学会.输入一个整数,输出这个整数. 输入描述 输入一个整数,范围在32位有符号整数范围内 输出描述 输出这个整 ...

  2. 基于windows系统使用GNVM进行node切换版本

    GNVM是什么? GNVM 是一个简单的 Windows 下 Node.js 多版本管理器,类似的 nvm nvmw nodist . 安装 进入官网,下载你所需要的包,直达链接 下载完成 放到我们的 ...

  3. 分布式协议与算法-Paxos算法

    1.Basic Paxos 假设我们要实现一个分布式集群,这个集群是由节点 A.B.C 组成,提供只读 KV 存储服务.你应该知道,创建只读变量的时候,必须要对它进行赋值,而且这个值后续没办法修改.因 ...

  4. java入门与进阶 P-3.2+P-3.3+P3.4

    数数字 例如:Scanner sc = new Scanner(System.in);int number = sc.nextInt();int count= 0;while(number>0) ...

  5. 创建型模式 - 抽象工厂模式AbstractFactory

    学习而来,代码是自己敲的.也有些自己的理解在里边,有问题希望大家指出. 模式的定义与特点 抽象工厂模式.是一种为访问类提供一个创建一组相关或相关一类对象的接口,且访问类无需指定所要的产品的具体类,就能 ...

  6. Node.js学习笔记----day03

    认真学习,认真记录,每天都要有进步呀!!! 加油叭!!! 一.Node中的模块系统 使用Node编写应用程序主要就是在使用 EcmaScript 和浏览器不一样的是,在Node中没有BOM.DOM 核 ...

  7. APP测试注意点-安装卸载与运行

    1.安装和卸载 应用在不同系统版本的ios和android是否可以正常安装(适配问题) 安装过程中是否可以取消 手机存储空间不足时安装应用是否有相应提示信息 安装后的应用是否可以正常卸载 卸载后是否会 ...

  8. Selenium 资源汇总

    created: 2022-8-4 驱动 资源名 网址 说明 chromedriver https://registry.npmmirror.com/binary.html?path=chromedr ...

  9. 前端-jQuery 查找标签 事件 Bootstrap页面框架

    1.查找标签 1.基本选择器: $('#d1'):id选择器 $('.c1'):class选择器 $('div'):标签选择器 2.组合选择器: $('div#d1'):查找id为d1的div标签 $ ...

  10. 非代码的贡献也能成为Committer,我与DolphinScheduler社区的故事

    点亮 ️ Star · 照亮开源之路 https://github.com/apache/dolphinscheduler   ​   // 每个人对于"开源社区"的定义都不一样, ...