Visual Studio EventHandler Delegate 和 EventArgs
EventHandler代理 用来表示处理一个没有事件数据(event data)的事件(event)的 方法。
无论何时事件发生时,事件代理就被调用来触发以前事件驱动的其他事件(监听当前事件TCurrentEvent += TListenerEvent)。
public delegate void EventHandler(
Object sender, //事件发起者
EventArgs e //处理事件数据的对象
)
EventArgs 类是事件变量的基类,提供事件数据类型给某个事件。
System.Object
System.EventArgs
More...
public class EventArgs
The following example shows an event named ThresholdReached that is associated with an EventHandler delegate. The method assigned to the EventHandler delegate is called in the OnThresholdReached method.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace delegrate
{
class Program
{
static void Main(string[] args)
{
Counter c = new Counter(new Random().Next(10));
c.ThresholdReached += c_ThresholdReached; //声明一个事件监听 Console.WriteLine("press 'a' key to increase total");
while (Console.ReadKey(true).KeyChar == 'a')
{
Console.WriteLine("adding one");
c.Add(1);
}
} static void c_ThresholdReached(object sender, ThresholdReachedEventArgs e)
{
Console.WriteLine("The threshold of {0} was reached at {1}.", e.Threshold, e.TimeReached);
Environment.Exit(0);
}
} 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); //传输一个 TEventArgs-->ThresholdReachedEventArgs
}
} protected virtual void OnThresholdReached(ThresholdReachedEventArgs e)
{
EventHandler<ThresholdReachedEventArgs> handler = ThresholdReached; //handle an Event
if (handler != null)
{
handler(this, e); //触发 c_ThresholdReached
}
} public event EventHandler<ThresholdReachedEventArgs> ThresholdReached;
} public class ThresholdReachedEventArgs : EventArgs
{
public int Threshold { get; set; }
public DateTime TimeReached { get; set; }
}
}
Visual Studio EventHandler Delegate 和 EventArgs的更多相关文章
- 使用Visual Studio SDK制作GLSL词法着色插件
使用Visual Studio SDK制作GLSL词法着色插件 我们在Visual Studio上开发OpenGL ES项目时,避免不了写Shader.这时在vs里直接编辑shader就会显得很方便. ...
- 分享10条Visual Studio 2012的开发使用技巧
使用Visual Studio 2012有一段时间了,并不是追赶潮流,而是被逼迫无可奈何.客户要求的ASP.NET MVC 4的项目,要用.NET 4.5来运行.经过一段时间的摸索,得到一点经验和体会 ...
- Visual Studio .NET项目转换器(ProjectConverter)修改
Visual Studio .NET 项目转换器非常类似于ASP.NET版本转换器,区别在于它用于转换 Visual Studio 项目文件的版本.尽管在 .NET 框架的 1.0 版和 1.1 版之 ...
- Visual Studio 2012的开发使用技巧
分享10条Visual Studio 2012的开发使用技巧 使用Visual Studio 2012有一段时间了,并不是追赶潮流,而是被逼迫无可奈何.客户要求的ASP.NET MVC 4的项目,要用 ...
- Visual studio之C#实现数字输入模拟键盘
背景 当前做的一个上位机需运行在工控机上,众所周知,工控机不可能配备键盘,只能是触屏,而我当前的上位机需要输入参数,于是之前的解决办法既是调用Windows自带的OSK.exe虚拟键盘,方法在我的另一 ...
- C# 4.0 新特性(.NET Framework 4.0 与 Visual Studio 2010 )
一.dynamic binding:动态绑定 在通过 dynamic 类型实现的操作中,该类型的作用是不在编译时类型检查,而是在运行时解析这些操作.dynamic 类型简化了对 COM API(例如 ...
- 在Visual Studio 2012中使用VMSDK开发领域特定语言(二)
本文为<在Visual Studio 2012中使用VMSDK开发领域特定语言>专题文章的第二部分,在这部分内容中,将以实际应用为例,介绍开发DSL的主要步骤,包括设计.定制.调试.发布以 ...
- Visual Studio 2010 插件之Resharper
这一系列不是对每个功能的中文帮助,而是我对开发中可能涉及的功能需求,利用Resharper来完成.有些是Visual Studio有的,但是做的不好,有些是没有而Resharper发明的.总的目的都只 ...
- 【译】Visual Studio 15 预览版更新说明
序:恰逢Build2016大会召开,微软发布了VS2015的update2更新包和VS2016预览版.本人正在提升英文水平中,于是在这里对VS2016预览版的官方文档进行了部分翻译.因为VS有些功能使 ...
随机推荐
- 国际化(Internationalization)
1:什么是国际化? 国际化(internationalization)是设计和制造容易适应不同区域要求的产品的一种方式.它要求从产品中抽离所有的与语言,国家/地区和文化相关的元素.换言之,应用程序的功 ...
- 《使用Hibernate开发租房系统》内部测试笔试题
笔试总结 1.在Hibernate中,以下关于主键生成器说法错误的是( C). A.increment可以用于类型为long.short或byte的主键 B.identity用于如SQL Server ...
- [No0000AC]全局鼠标键盘模拟器
之前网上下载的一位前辈写的工具,名叫:Dragon键盘鼠标模拟器,网址http://www.esc0.com/. 本软件能够录制键盘鼠标操作,并能按要求回放,对于重复的键盘鼠标操作,可以代替人去做,操 ...
- 商城项目:装nginx时碰到的各种问题
因为项目需要,我们要在linux上nginx.碰到了各种问题.在这里一一记录下来. 首先我要开启我的两个虚拟机,开起来之后.用主机的SeureCRT去连接.都是好的. 但是我在虚拟机机上去ping I ...
- Integrating SharePoint 2013 with ADFS and Shibboleth
Time again to attempt to implement that exciting technology, Federation Services (Web Single Sign On ...
- .NET MVC实现多图片上传并附带参数(ajaxfileupload)
做网站呢,都免不了要做图片上传. 还记得去年做微信的时候用WebAPI+ajaxfileupload.js做了一个能够附带参数上传的功能,博文地址:.NET WebAPI 实现图片上传(包括附带参数上 ...
- [原创]vscode初体验
这段时间,在网上看见很多从.net转java的,为什么会造成这样的情况,我感觉有几点 1. 微软在中国的生态不好,死要钱,很多公司都不想花这部分钱 2. 做.net开发人,工资普遍较低 前言 闲聊 ...
- 五.Jenkins安装plugin
http://blog.csdn.net/jmyue/article/details/9376237
- Javascript自执行匿名函数(function() { })()的原理分析
匿名函数指没有指定函数名或指针的函数,自执行匿名函数只是其中一种,下文中称这种函数为:自执行函数 下面是一个最常见的自执行函数: // 传统匿名函数 (function() { alert('hell ...
- Leetcode 15. 3Sum
Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find all un ...