https://docs.microsoft.com/zh-cn/dotnet/standard/events/how-to-raise-and-consume-events

第一个示例演示如何引发和使用一个没有数据的事件。 它包含一个名为 Counter 类,该类具有一个名为 ThresholdReached 的事件。 当计数器值等于或者超过阈值时,将引发此事件。 EventHandler 委托与此事件关联,因为没有提供任何事件数据。

using System;

namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
Counter c = new Counter(new Random().Next());
c.ThresholdReached += c_ThresholdReached; Console.WriteLine("press 'a' key to increase total");
while (Console.ReadKey(true).KeyChar == 'a')
{
Console.WriteLine("adding one");
c.Add();
}
} static void c_ThresholdReached(object sender, EventArgs e)
{
Console.WriteLine("The threshold was reached.");
Environment.Exit();
}
} class Counter
{
private int threshold;
private int total; public Counter(int passedThreshold)
{
threshold = passedThreshold;
} public void Add(int x)
{
total += x;
if (total >= threshold)
{
OnThresholdReached(EventArgs.Empty);
}
} protected virtual void OnThresholdReached(EventArgs e)
{
EventHandler handler = ThresholdReached;
if (handler != null)
{
handler(this, e);
}
} public event EventHandler ThresholdReached;
}
}

下一个示例演示如何引发和使用提供数据的事件。 EventHandler<TEventArgs> 委托与此事件关联,示例还提供了一个自定义事件数据对象的实例。

using System;

namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
Counter c = new Counter(new Random().Next());
c.ThresholdReached += c_ThresholdReached; Console.WriteLine("press 'a' key to increase total");
while (Console.ReadKey(true).KeyChar == 'a')
{
Console.WriteLine("adding one");
c.Add();
}
} static void c_ThresholdReached(object sender, ThresholdReachedEventArgs e)
{
Console.WriteLine("The threshold of {0} was reached at {1}.", e.Threshold, e.TimeReached);
Environment.Exit();
}
} class Counter
{
private int threshold;
private int total; public Counter(int passedThreshold)
{
threshold = passedThreshold;
} public void Add(int x)
{
total += x;
if (total >= threshold)
{
ThresholdReachedEventArgs args = new ThresholdReachedEventArgs();
args.Threshold = threshold;
args.TimeReached = DateTime.Now;
OnThresholdReached(args);
}
} protected virtual void OnThresholdReached(ThresholdReachedEventArgs e)
{
EventHandler<ThresholdReachedEventArgs> handler = ThresholdReached;
if (handler != null)
{
handler(this, e);
}
} public event EventHandler<ThresholdReachedEventArgs> ThresholdReached;
} public class ThresholdReachedEventArgs : EventArgs
{
public int Threshold { get; set; }
public DateTime TimeReached { get; set; }
}
}

下一个示例演示如何声明事件的委托。 该委托名为 ThresholdReachedEventHandler。 这只是一个示例。 通常不需要为事件声名委托,因为可以使用 EventHandler 或者 EventHandler<TEventArgs> 委托。 只有在极少数情况下才应声明委托,例如,在向无法使用泛型的旧代码提供类时,就需要如此。

using System;

namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
Counter c = new Counter(new Random().Next());
c.ThresholdReached += c_ThresholdReached; Console.WriteLine("press 'a' key to increase total");
while (Console.ReadKey(true).KeyChar == 'a')
{
Console.WriteLine("adding one");
c.Add();
}
} static void c_ThresholdReached(Object sender, ThresholdReachedEventArgs e)
{
Console.WriteLine("The threshold of {0} was reached at {1}.", e.Threshold, e.TimeReached);
Environment.Exit();
}
} class Counter
{
private int threshold;
private int total; public Counter(int passedThreshold)
{
threshold = passedThreshold;
} public void Add(int x)
{
total += x;
if (total >= threshold)
{
ThresholdReachedEventArgs args = new ThresholdReachedEventArgs();
args.Threshold = threshold;
args.TimeReached = DateTime.Now;
OnThresholdReached(args);
}
} protected virtual void OnThresholdReached(ThresholdReachedEventArgs e)
{
ThresholdReachedEventHandler handler = ThresholdReached;
if (handler != null)
{
handler(this, e);
}
} public event ThresholdReachedEventHandler ThresholdReached;
} public class ThresholdReachedEventArgs : EventArgs
{
public int Threshold { get; set; }
public DateTime TimeReached { get; set; }
} public delegate void ThresholdReachedEventHandler(Object sender, ThresholdReachedEventArgs e);
}

C# 给类做事件的一般做法的更多相关文章

  1. 用block做事件回调来简化代码,提高开发效率

       我们在自定义view的时候,通常要考虑view的封装复用,所以如何把view的事件回调给Controller就是个需要好好考虑的问题, 一般来说,可选的方式主要有target-action和de ...

  2. VBA中自定义类和事件的(伪)注册

    想了解一下VBA中自定义类和事件,以及注册事件处理程序的方法. 折腾了大半天,觉得这样的方式实在称不上“注册”,所以加一个“伪”字.纯粹是瞎试,原理也还没有摸透.先留着,有时间再接着摸. 做以下尝试: ...

  3. QT_8_Qt中的事件处理_定时器事件_定时器类_事件分发器_事件过滤器_绘图事件_高级绘图事件_绘图设备_QFile 文件读写_QFileInfo文件信息

    Qt中的事件处理 1.1. 捕获QLabel中是鼠标事件 1.2. enterevent 鼠标进入 1.3. leaveevent 鼠标离开 1.4. 鼠标按下MyLabel::mousePressE ...

  4. 前端(十八)—— jQuery高级操作:选择器、文本属性与类、事件、文档操作、动画、结构关系

    JQ选择器.文本属性与类.事件.文档操作.动画.结构关系 可参考jQuery的API文档 一.选择器 1.css语法匹配 标签 | 类 | id | 交集 群组 | 后代 | 兄弟 伪类 | 属性 $ ...

  5. django使用类做业务逻辑

    在django中一般定义一个带有request参数的函数用来处理url,但是更推荐用类做 从django.views.generic.base 导入的views有get,post等各种函数,用来处理对 ...

  6. springboot+redis做事件过期通知业务

    springboot+redis做事件过期通知 博主也是初次体验,不足之处多多指教 我的业务场景 系统管理员要给维护员分配巡查路口设施的工作,由于路口比较多,管理员不知道哪些路口已经被分配了,况且过了 ...

  7. C++ 友元 (全局函数做友元) (类做友元) (成员函数做友元)

    1 //友元 全局函数做友元 2 /* 3 #include <iostream> 4 #include <string> 5 using namespace std; 6 7 ...

  8. 微信浏览器或各种移动浏览器上:active伪类做的触觉反馈失效

    在做移动端页面的时候,会发现PC上那种:hover的效果是不管用了的,但又要给用户一个点击反馈怎么办呢?我管它叫触觉反馈. 细心点就会发现浏览器有自带了一点触觉反馈,在点击a.button.input ...

  9. C#基础-事件 继承类无法直接引发基类的事件

    An event can be raised only from the declaration space in which it is declared. Therefore, a class c ...

随机推荐

  1. caffe的运行create_data.sh前对VOC2007图片格式的更改

    运用caffe进行深度学习之前需要对图片进行预处理,将图片的大小,格式等进行修改 将300*300的图片改为256*256格式 则将以下参数改为: min_dim=256 max_dim=256 wi ...

  2. would you please...could you please...两句区别是什么?

    Could you please 是can you please 更为礼貌.委婉的说法,并不是过去式,是“能否麻烦你……”.“请你……”的意思,更侧重“能否”及客观情况:回答时要注意,eg:A:“Co ...

  3. [2]传奇3服务器源码分析一 LoginServer

    留存 服务端下载地址: 点击这里

  4. 【Alpha版本】冲刺阶段——Day7

    [Alpha版本]冲刺阶段--Day7 阅读目录 今日进展 今日贡献量 贡献量汇总 TODOList 及项目燃尽图 [今日进展] 将项目源文件打成jar包,并运行测试 完成答辩ppt 项目运行情况 主 ...

  5. CRM 权限设置 ss

    表结构的设计 权限表 url -url地址的正则表达式 ^$ title - 标题 角色表 name - 角色名称 permissions 多对多关联权限表 (权限和角色的关系表) 用户表 name ...

  6. .net core创建项目(指令方式)

    所谓的指令创建项目,就是不用再已安装的VS2015的环境下或者VS Core下创建,直接通过DOS指令创建也是OK的. 1.找到你所准备保存项目的项目文件夹(你也可以到某个目录用指令创建项目文件夹[  ...

  7. yocto doc

    Yocto Project Overview and Concepts Manual https://www.yoctoproject.org/docs/2.6.1/overview-manual/o ...

  8. EDK II之驱动程序与硬件平台的初始化简介

    本文旨在简单介绍一下UEFI中驱动程序的加载方式(这里涉及的模块指的是符合UEFI Driver Model的模块): 在UEFI中,当一个驱动模块被加载时,在模块入口点只会安装EFI_DRIVER_ ...

  9. P4172 [WC2006]水管局长(LCT)

    P4172 [WC2006]水管局长 LCT维护最小生成树,边权化点权.类似 P2387 [NOI2014]魔法森林(LCT) 离线存储询问,倒序处理,删边改加边. #include<iostr ...

  10. Markdown语法学习(Github/git.oschina.net上README.md书写规范)(转)

    晚上在更新git.oschina.net项目时,突然想知道README.md后缀的来源,于是搜了下,发现README.md使用了一种小标记语言Markdown的语法,于是简单的看了一个,特转载如下,为 ...