using System;
using System.Windows.Forms;
using System.Drawing;
public class FlashTrackBar : Control {
// The event does not have any data, so EventHandler is adequate
// as the event delegate.
private EventHandler onValueChanged;
// Define the event member using the event keyword.
// In this case, for efficiency, the event is defined
// using the event property construct.
public event EventHandler ValueChanged {
add {
onValueChanged += value;
}
remove {
onValueChanged -= value;
}
}
// The protected method that raises the ValueChanged
// event when the value has actually
// changed. Derived controls can override this method.
protected virtual void OnValueChanged(EventArgs e) {
if (ValueChanged != null) {
ValueChanged(this, e);
}
}
}
这段代码是MSDN上的,编译没通过,请问是不是应该把最后两行的ValueChanged改成onValueChanged?
using System.Windows.Forms;
using System.Drawing;
public class FlashTrackBar : Control {
// The event does not have any data, so EventHandler is adequate
// as the event delegate.
private EventHandler onValueChanged;
// Define the event member using the event keyword.
// In this case, for efficiency, the event is defined
// using the event property construct.
public event EventHandler ValueChanged {
add {
onValueChanged += value;
}
remove {
onValueChanged -= value;
}
}
// The protected method that raises the ValueChanged
// event when the value has actually
// changed. Derived controls can override this method.
protected virtual void OnValueChanged(EventArgs e) {
if (ValueChanged != null) {
ValueChanged(this, e);
}
}
}
这段代码是MSDN上的,编译没通过,请问是不是应该把最后两行的ValueChanged改成onValueChanged?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;namespace wanwan2
{
public class WarnEventArgs : EventArgs
{
private double _tempature = 0.0; public double Tempature
{
get { return _tempature; }
set { _tempature = value; }
} }
public class BoilWater
{ public delegate void WarnDelegate(WarnEventArgs e);
public event WarnDelegate WarnEvent; public void OnWarnEvent(WarnEventArgs e)
{
if (WarnEvent!= null)
{
WarnEvent(e);
} } public void Warn(double x)
{
WarnEventArgs e = new WarnEventArgs();
e.Tempature = x;
OnWarnEvent(e);
}
} class Program
{ public static void Alarm(WarnEventArgs e)
{
double tem = e.Tempature;
while (tem > 97 && tem < 101)
{
Console.WriteLine("是快开了,现在已经达到{0}摄氏度",tem);
tem++;
}
}
static void Main(string[] args)
{
BoilWater _warter = new BoilWater();
_warter.WarnEvent += Alarm;
_warter.Warn(98);
}
}
}
那究竟ValueChanged有什么存在价值,是不是没有办法直接通过Event属性调用EventHandler?
对于explict event,就是定义了add,remove的event,那么编译器就不会生成那个delegate了,所以你只能+=、-=,这时候调用的就是你自定义的add、remove方法了
存在的价值自然是有的,你可以在add、remove里做更多的控制
定义implict event之后,可以直接通过event调用那个自动生成的delegate,编译器是怎么把自动生成的delegate跟那个event关联起来的呢?我们自己也可以实现这个功能吗?
定义explicit event就是相当于这个功能呀,比如你的onValueChange就相当于编译器生成的那个delegate
{
onValueChanged(this, e);
}在微软自己的控件中,事件的设计模式是这样的:private static readonly EventHandler onValueChanged;public event EventHandler ValueChanged
{
add
{
base.Events.AddHandler(onValueChanged, value);
}
remove
{
base.Events.RemoveHandler(onValueChanged, value);
}
}protected virtual void OnValueChanged(EventArgs e)
{
EventHandler handler = (EventHandler) base.Events[onValueChanged];
if (handler != null)
{
handler(this, e);
}
}
也就是说,每个控件中有一个队列,保存所有各种类型的EventHandler。比如说一个控件有30个事件,比如说其中只有5个被使用并且注册了8个EvengHandler实例,那么此控件实例对象内部的这个事件队列中有5个Delegate,这5个Delegate(其中有的是MuticastDelegate)总共组合了8个具体的(单一的)Delegate。