1.先定义要触发的事件对象 public class MyEventArgs: System.EventArgs { private int newValue; public int NewValue { get{return newValue;}set{newValue = value;} } } 2、定义事件代理 public delegate void PropertyChangedHandler(object sender,MyEventArgs e); 3、定义类,并在类中定义事件 public class dog { public event PropertyChangedHandler PropertyChanged; //定义事件处理方法 protected virtual void OnPropertyChanged(MyEventArgs e) { if (PropertyChanged != null) PropertyChanged(this,e); } private int age; public int Age { get{return age;} set{ age = value; MyEventArgs e = new MyEventArgs(); e.NewValue = age; OnPropertyChanged(e); } } } 4、使用实例 public class Test { public static void main() { Dog aDog = new Dog(); aDog.PropertyChanged += new PropertyChantedHandler(aDog_AgeChanged); aDog.Age = 10; } private static void aDog_AgeChanged(object sender, MyEventArgs e) { Console.WriteLine(e.NewValue); } }
|
请发表评论