C#委托,事件理解入门 (译稿) 收藏
目录
l
导论
l
什么是委托
l
事件的理解
l
事件
关键字
l
最后
导论
在学习
C#
中的委托和事件过程中,我读了许多文章来理解他们二者究竟是怎么一回事,以及如何使用他们,现在我将
整个的理解过程陈述以下,我学到的每一方面,恐怕也是你们需要掌握的
:
-
)。
什么是委托?
委托和事件这两个概念是完全配合的。委托仅仅是函数指针,那就是说,它能够引用函数,通过传递地址的机制完成。委托是一个类,当你对它实例
化时,要提供一个引用函数,将其作为它构造函数的参数。
每一个委托都有自己的签名,例如:
Delegate int SomeDelegate(string s, bool b);
是一个委托申明,在这里,提及的签名,就是说
SomeDelegate
这个委托
有
string
和
bool
类型
的形参,返回一个
int
类型。
上面提及的:当你对委托实例化时,要提供一个引用函数,将其作为它构造函数的参数。这里要注意了:被
引用的这个函数必须和委托有相同的签名。
看下面的函数:
private int SomeFunction(string str, bool
bln){...}
你可以把这个函数传给
SomeDelegate
的构造函数,因为他们有相似的签名(
in other words
,他们都有相同的形参类型和个数,并且返回相同的数据类型)。
SomeDelegate sd = new
SomeDelegate(SomeFunction);
sd
引用了
SomeFunction
,也就是说,
SomeFunction
已被
sd
所登记注册,如果你调用
sd
,
SomeFunction
这个函数也会被调用,记住:我所说
SomeFunction
的含义,后面,我们会用到它。
现在,你应该知道如何使用委托了,让我们继续理解事件之旅
……
事件的理解
我们知道,在
C#
中:
l
按钮(
Button
)就是
一个类,当我们单击它时,就触发一次
click
事件。
l
时钟(
Timer
)也是一
个类,每过一毫秒,就触发一次
tick
事件。
让我们通过一个例子来
学习,假定有这样的情节:
现在有一个
Counter
的类,它有一个方法
CountTo(int countTo, int reachableNum)
,该方法表示:在指定的时间段内(
0~~countTo
),当到达指定的时间点
reachableNum
时,就触发一次
NumberReached
事件。
它还有一个事件:
NumberReached
,事件是委托类型的变量。意思是:如果给事件命名,用
event
关键字和要使用的委托类型申明它即可,如下所示:
public
event NumberReachedEventHandler NumberReached;
在上面的申明中,
NumberReachedEventHandle
仅是一个委托,更确切的表示应该是:
NumberReachedDelegate
。但是微软
从不这样认为
MouseDelegate
或者
PaintDelegate
,,而是称谓:
MouseEventHandler
或者
PaintEventHandler
。所以
NumberReachedEventHandler
比
NumberReachedDelegate
听起来更方便一些,
OK?
好了,让我们继续,现在你知道了,在我们声明事件之前,需要象下面这样的形式来定义委托:
public delegate void
NumberReachedEventHandler(object sender, NumberReachedEventArgs e);
现在声明的委托
NumberReachedEventHandle
,它有一个
void
返回值,和
object
,
NumberReachedEventArgs
两个形参。就像我们在第一节中强调的那样,当实例化委托时,作为实参传入的函数也必须拥有和委托同样
的签名。
在你的代码中,
你是否用过
PaintEventArgs
或者
MouseEventArgs
来确定鼠标的移动位置?是否在触发
Paint
事件的对象中用过
Graphics
属
性?实际上,为用户提供数据的类都是继承于
System.EventArgs
类,就是我们常说的事件参数类,如果事件不提供参数,就不定义该类。在我们的例子中,我们通过下面
的类提供预期的时间点。
public class NumberReachedEventArgs :
EventArgs
{
private int _reached;
public
NumberReachedEventArgs(int num)
{
this._reached = num;
}
public int ReachedNumber
{
get
{
return _reached;
}
}
}
好,有了前面的介绍,让我们到
Counter
类里
面看看:
namespace Events
{
public delegate void
NumberReachedEventHandler(object sender,
NumberReachedEventArgs e);
/// <summary>
/// Summary description for
Counter.
/// </summary>
public class Counter
{
public event
NumberReachedEventHandler NumberReached;
public Counter()
{
//
// TODO: Add constructor
logic here
//
}
public void CountTo(int
countTo, int reachableNum)
{
if(countTo <
reachableNum)
throw new
ArgumentException(
"reachableNum
should be less than countTo");
for(int
ctr=0;ctr<=countTo;ctr++)
{
if(ctr ==
reachableNum)
{
NumberReachedEventArgs e = new NumberReachedEventArgs(
reachableNum);
OnNumberReached(e);
return;//don't
count any more
}
}
}
protected virtual void
OnNumberReached(NumberReachedEventArgs e)
{
if(NumberReached !=
null)
{
NumberReached(this,
e);//Raise the event
}
}
}
在
Counter
中,
如果到达指定的时间点,就触发一次事件,有以下几个方面需要注意:
l
通过调用
NumberReached
(它是
NumberReachedEventHandler
委托的实例)来完成一次触发事件。
NumberReached(this,
e);
通过这种方式,可以调用所有的注册函数。
l
通过
NumberReachedEventArgs e = new
NumberReachedEventArgs(reachableNum);
为所有的注册函数提供事件数据。
l
看了上面的代码,你可能要问了:为什么我们直接用
OnNumberReached(NumberReachedEventArgs e)
方法来调用
NumberReached
(
this
,
e
),而不用下面的代码呢?
if(ctr == reachableNum)
{
NumberReachedEventArgs e = new NumberReachedEventArgs(reachableNum);
//OnNumberReached(e);
if(NumberReached != null)
{
NumberReached(this, e);//Raise the event
}
return;//don't count any more
}
这个问题问得
很好,那就让我们再看一下
OnNumberReached
签名:
protected
virtual void OnNumberReached(NumberReachedEventArgs e)
①
你也明白
关键字
protected
限
定了
只有从该类继承的类才能调用该类中的所有方法。
②
关键字
virtual
表明了
在继承类中可以重写该方法。
这两点非常有
用,假设你在写一个从
Counter
继承而来的类,通过重写
OnNumberReached
方法,你可以在事件触发
之前,进行一次其他的工作。
protected
override void OnNumberReached(NumberReachedEventArgs e)
{
//Do additional work
base.OnNumberReached(e);
}
注意:如果你
没有调用
base.OnNumberReached(e),
那么从不会触发这个事件!在你继承该类而想剔出它的一些其他事件时,使用该方式是非常有用的。
l
还要注意到:委托
NumberReachedEventHandler
是在类定义的外部,命名空间内定义的,对所有类来说是可见的。
好,该我们来
实际操作使用
Counter
类了。
在我们简单的
应用程序中,我们有两个文本框,分别是:
txtCountTo
和
txtReachable
:
下面是 btnRun 的 click 事件:
private
void
btnRun_Click(object
sender, System.EventArgs e)
{
if
(txtCountTo.Text == "" ||
txtReachable.Text=="")
return
;
oCounter.CountTo(Convert.ToInt32(txtCountTo.Text),
Convert.ToInt32(txtReachable.Text));
}
private
void
oCounter_NumberReached(object
sender, NumberReachedEventArgs e)
{
MessageBox.Show("Reached: " + e.ReachedNumber.ToString());
}
初始化事件处
理的语法如下:
oCounter
= new Counter();
oCounter.NumberReached += new
NumberReachedEventHandler(oCounter_NumberReached);
现在你明白了
你刚才所做的一切,仅仅初始化 NumberReachedEventHandler 委托类型的对象(就像你实例化其他对象一样),注意到
oCounter_NumberReached 方法的签名与我前面提到的相似。
还要注意我们
用的是+= 而不是=;这是因为委托是特殊的对象,它可以引用多个对象(在这里是指它可以引用多个函数)。For example 如果有另外一个
和oCounter_NumberReached一样具有相同签名的函数
oCounter_NumberReached2,这两个函数都可以被引用:
oCounter = new Counter();
oCounter.NumberReached +=
new NumberReachedEventHandler(oCounter_NumberReached);
oCounter.NumberReached +=
new NumberReachedEventHandler(oCounter_NumberReached2);
现在,触发一个事件后,上面两个函数被依次调用。
视情况而定,如果你想让oCounter_NumberReached2在
NumberReached事件发生后不再被调用,可以简单地这样写:oCounter.NumberReached -= new
NumberReachedEventHandler(oCounter_NumberReached2);
最后
让我们看一下完整的源代码,以供参考:
Form1.cs
Counter.cs
using
System;
namespace Events
{
public delegate void NumberReachedEventHandler( object sender, NumberReachedEventArgs e);
/// <summary>
/// Summary description for Counter.
/// </summary>
public class Counter
{
public event NumberReachedEventHandler NumberReached;
public Counter()
{
//
// TODO: Add constructor logic here
//
}
public void CountTo( int countTo, int reachableNum)
{
if (countTo < reachableNum)
throw new ArgumentException( " reachableNum should be less than countTo " );
for ( int ctr = 0 ;ctr <= countTo;ctr ++ )
{
if (ctr == reachableNum)
{
NumberReachedEventArgs e = new NumberReachedEventArgs(reachableNum);
OnNumberReached(e);
return ; // don't count any more
}
}
}
protected virtual void OnNumberReached(NumberReachedEventArgs e)
{
if (NumberReached != null )
{
NumberReached( this , e);
}
}
}
public class NumberReachedEventArgs : EventArgs
{
private int _reached;
public NumberReachedEventArgs( int num)
{
this ._reached = num;
}
public int ReachedNumber
{
get
{
return _reached;
}
}
}
}
namespace Events
{
public delegate void NumberReachedEventHandler( object sender, NumberReachedEventArgs e);
/// <summary>
/// Summary description for Counter.
/// </summary>
public class Counter
{
public event NumberReachedEventHandler NumberReached;
public Counter()
{
//
// TODO: Add constructor logic here
//
}
public void CountTo( int countTo, int reachableNum)
{
if (countTo < reachableNum)
throw new ArgumentException( " reachableNum should be less than countTo " );
for ( int ctr = 0 ;ctr <= countTo;ctr ++ )
{
if (ctr == reachableNum)
{
NumberReachedEventArgs e = new NumberReachedEventArgs(reachableNum);
OnNumberReached(e);
return ; // don't count any more
}
}
}
protected virtual void OnNumberReached(NumberReachedEventArgs e)
{
if (NumberReached != null )
{
NumberReached( this , e);
}
}
}
public class NumberReachedEventArgs : EventArgs
{
private int _reached;
public NumberReachedEventArgs( int num)
{
this ._reached = num;
}
public int ReachedNumber
{
get
{
return _reached;
}
}
}
}
using
System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
namespace Events
{
/// <summary>
/// Summary description for Form1.
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
Counter oCounter = null ;
private System.Windows.Forms.Button cmdRun;
private System.Windows.Forms.TextBox txtReachable;
private System.Windows.Forms.TextBox txtCountTo;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.Button btnRemoveDelegate;
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null ;
public Form1()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
//
// TODO: Add any constructor code after InitializeComponent call
//
oCounter = new Counter();
oCounter.NumberReached += new NumberReachedEventHandler(oCounter_NumberReached);
oCounter.NumberReached += new NumberReachedEventHandler(oCounter_NumberReached2);
}
/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if ( disposing )
{
if (components != null )
{
components.Dispose();
}
}
base .Dispose( disposing );
}
Windows Form Designer generated code
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.Run( new Form1());
}
private void btnRun_Click( object sender, System.EventArgs e)
{
if (txtCountTo.Text == "" || txtReachable.Text == "" )
return ;
oCounter.CountTo(Convert.ToInt32(txtCountTo.Text), Convert.ToInt32(txtReachable.Text));
}
private void oCounter_NumberReached( object sender, NumberReachedEventArgs e)
{
MessageBox.Show( " Reached: " + e.ReachedNumber.ToString());
}
private void oCounter_NumberReached2( object sender, NumberReachedEventArgs e)
{
MessageBox.Show( " Reached2: " + e.ReachedNumber.ToString());
}
private void btnRemoveDelegate_Click( object sender, System.EventArgs e)
{
oCounter.NumberReached -= new NumberReachedEventHandler(oCounter_NumberReached2);
oCounter.CountTo(Convert.ToInt32(txtCountTo.Text), Convert.ToInt32(txtReachable.Text));
}
}
}
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
namespace Events
{
/// <summary>
/// Summary description for Form1.
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
Counter oCounter = null ;
private System.Windows.Forms.Button cmdRun;
private System.Windows.Forms.TextBox txtReachable;
private System.Windows.Forms.TextBox txtCountTo;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.Button btnRemoveDelegate;
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null ;
public Form1()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
//
// TODO: Add any constructor code after InitializeComponent call
//
oCounter = new Counter();
oCounter.NumberReached += new NumberReachedEventHandler(oCounter_NumberReached);
oCounter.NumberReached += new NumberReachedEventHandler(oCounter_NumberReached2);
}
/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if ( disposing )
{
if (components != null )
{
components.Dispose();
}
}
base .Dispose( disposing );
}
Windows Form Designer generated code
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.Run( new Form1());
}
private void btnRun_Click( object sender, System.EventArgs e)
{
if (txtCountTo.Text == "" || txtReachable.Text == "" )
return ;
oCounter.CountTo(Convert.ToInt32(txtCountTo.Text), Convert.ToInt32(txtReachable.Text));
}
private void oCounter_NumberReached( object sender, NumberReachedEventArgs e)
{
MessageBox.Show( " Reached: " + e.ReachedNumber.ToString());
}
private void oCounter_NumberReached2( object sender, NumberReachedEventArgs e)
{
MessageBox.Show( " Reached2: " + e.ReachedNumber.ToString());
}
private void btnRemoveDelegate_Click( object sender, System.EventArgs e)
{
oCounter.NumberReached -= new NumberReachedEventHandler(oCounter_NumberReached2);
oCounter.CountTo(Convert.ToInt32(txtCountTo.Text), Convert.ToInt32(txtReachable.Text));
}
}
}
作者:Angelo Lee
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利.