C#学习基本概念之事件(四)---EventHandler委托(事件处理)
2016-11-28 15:23
507 查看
Represents the method that will handle an event that has no event data.
命名空间:
System
程序集: mscorlib(位于 mscorlib.dll)
Type: System.Object
The source of the event.
e
Type: System.EventArgs
An object that contains no event data.
备注
The
event model in the .NET Framework is based on having an event delegate
that connects an event with its handler. To raise an event, two elements
are needed:
A delegate that identifies the method that provides the response to the event.
Optionally, a class that holds the event data, if the event provides data.
The
delegate is a type that defines a signature, that is, the return value
type and parameter list types for a method. You can use the delegate
type to declare a variable that can refer to any method with the same
signature as the delegate.
The standard signature of an event
handler delegate defines a method that does not return a value. This
method's first parameter is of type T:System.Object and refers to the
instance that raises the event. Its second parameter is derived from
type T:System.EventArgs and holds the event data. If the event does not
generate event data, the second parameter is simply the value of the
F:System.EventArgs.Empty field. Otherwise, the second parameter is a
type derived from T:System.EventArgs and supplies any fields or
properties needed to hold the event data.
The
T:System.EventHandler delegate is a predefined delegate that
specifically represents an event handler method for an event that does
not generate data. If your event does generate data, you must use the
generic T:System.EventHandler`1 delegate class.
To associate the
event with the method that will handle the event, add an instance of the
delegate to the event. The event handler is called whenever the event
occurs, unless you remove the delegate.
For more information about event handler delegates, see Handling and Raising Events.
示例
The
following example shows an event named ThresholdReached that is
associated with an T:System.EventHandler delegate. The method assigned
to the T:System.EventHandler delegate is called in the
OnThresholdReached method.
命名空间:
System
程序集: mscorlib(位于 mscorlib.dll)
[SerializableAttribute] [ComVisibleAttribute(true)] public delegate void EventHandler( object sender, EventArgs e)
参数
senderType: System.Object
The source of the event.
e
Type: System.EventArgs
An object that contains no event data.
备注
The
event model in the .NET Framework is based on having an event delegate
that connects an event with its handler. To raise an event, two elements
are needed:
A delegate that identifies the method that provides the response to the event.
Optionally, a class that holds the event data, if the event provides data.
The
delegate is a type that defines a signature, that is, the return value
type and parameter list types for a method. You can use the delegate
type to declare a variable that can refer to any method with the same
signature as the delegate.
The standard signature of an event
handler delegate defines a method that does not return a value. This
method's first parameter is of type T:System.Object and refers to the
instance that raises the event. Its second parameter is derived from
type T:System.EventArgs and holds the event data. If the event does not
generate event data, the second parameter is simply the value of the
F:System.EventArgs.Empty field. Otherwise, the second parameter is a
type derived from T:System.EventArgs and supplies any fields or
properties needed to hold the event data.
The
T:System.EventHandler delegate is a predefined delegate that
specifically represents an event handler method for an event that does
not generate data. If your event does generate data, you must use the
generic T:System.EventHandler`1 delegate class.
To associate the
event with the method that will handle the event, add an instance of the
delegate to the event. The event handler is called whenever the event
occurs, unless you remove the delegate.
For more information about event handler delegates, see Handling and Raising Events.
示例
The
following example shows an event named ThresholdReached that is
associated with an T:System.EventHandler delegate. The method assigned
to the T:System.EventHandler delegate is called in the
OnThresholdReached method.
using System;namespace ConsoleApplication1 { 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); } } 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; } } }备注:https://msdn.microsoft.com/zh-cn/library/system.eventhandler.aspx
相关文章推荐
- C# 委托学习(1) 基本概念学习
- C#学习基本概念之关键字---delegate(委托)
- C#学习基本概念之事件与关键字--event
- C#学习基本概念之事件(三)---发布符合.NET Framework准则的事件
- C#学习基本概念之事件(四)---ElapsedEventHandler委托
- C#学习基本概念之事件(二)---订阅和取消
- C#学习基本概念之事件(五)---EventArgs
- 学习笔记之---C#中的委托与事件
- C#及.NET2.0学习笔记之C#语言基本概念
- [C#] C#编写事件或委托时如果处理不好会造成内存泄漏
- 第二章-第四章:基本事件处理,映射模式和滚动视图-从”Hello World”着手学习AppWizard
- 一步一步学习C#(五 委托与事件)
- C#与.NET4 高级程序设计-----委托、事件 学习笔记
- C#基础学习系列之:委托和事件
- C#事件委托学习(一)
- C#学习笔记之三:C#接口与事件委托
- C#之委托概念认识学习笔记
- 复杂事件处理(Complex Event Processing)--1. 基本概念:事件,事件关系,事件处理的简单抽象理解
- C#中的委托和事件学习(续)
- 我是如何学习设计模式的一:.NET C# 委托定天下,学好委托事件,设计模式精髓可以理解三分之一,怎么说也是三分天下之势了