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有些功能使 ...
随机推荐
- const 引起的BUG
今天白天出现了碰见了一个问题,隐藏得比较深,这里记录一下. 初衷很简单,就是要更改改一个数据库的链接名,这个链接名是放在数据层public const string connDB="conn ...
- 客户端Socket
导语 java.net.Socket类是JAVA完成客户端TCP操作的基础类.其他建立TCP网络连接的类(如URL,URLConnection和EditorPane)最终会调用这个类的方法.这个类本身 ...
- 跨域请求——WebClient通过get和post请求api
AJAX不可以实现跨域请求,经过特殊处理才行.一般后台可以通过WebClient实现跨域请求~ //get 请求 string url = string.Format("htt ...
- C /C++ 语言练习册
/************************************** 整数对应 32 bit 二进制数串中数字1的个数 2016-10-24 liukun ***************** ...
- 使用AccessibilityService执行开机自启动
res/xml/accessibility_service_config.xml <accessibility-service xmlns:android="http://schema ...
- JAVA+Maven+TestNG搭建接口测试框架及实例
1.配置JDK 见另一篇博客:http://www.cnblogs.com/testlurunxiu/p/5933912.html 2.安装Eclipse以及TestNG Eclipse下载地址:ht ...
- cocoapods安装
什么是cocoapods CocoaPods是一个用来帮助我们管理第三方依赖库的工具.它可以解决库与库之间的依赖关系,下载库的源代码,同时通过创建一个Xcode的workspace来将这些第三方库和我 ...
- weak和nonull
weak和nonull是相互排斥的,所以weak和null不能同时使用,如下图:
- fedora22,fedora24最简单的安装virtaulbox的方法
fedora为什么不好用呢? 1.因为很多软件没有预先安装,新手安装时,就无从下手了. 2.版本更新太快,有老手提供了解决方案,但是版本更新了,新手按照步骤来,就不能配置成功! 不抱怨了. 安装vir ...
- jsp上传图片实时显示
jsp代码 <div class="form-group" id="caseIma"> <label class="control- ...