在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
using System; namespace ConsoleApplication7 { class Program { static void Main(string[] args) { people p = new people("Name1"); p.PropertyChanged += new EventHandler(p_PropertyChanged); //注册事件处理函数 p.Name = "Name2"; Console.ReadKey(); } static void p_PropertyChanged(object sender, EventArgs e) //事件的处理函数 { Console.WriteLine("NamePropertyChanged:"+(sender as people).Name); } } class people { public event EventHandler PropertyChanged; private string name=String .Empty; public people(string name) { this.name = name; } public string Name { get { return name; } set { name = value; this.OnPropertyChanged(new EventArgs()); //每次改变Name值调用方法; } } private void OnPropertyChanged(EventArgs eventArgs) { if (this.PropertyChanged != null) //判断事件是否有处理函数 { this.PropertyChanged(this, eventArgs); } } } } //请参考 |
请发表评论