时间:2016年05月23日 | 作者 : aaronyang | 分类 : C#开发 | 浏览: 3560次 | 评论 1 人
讲解: 观察(Observer)与通知(Notify)
两个角色的设计模式.WPF最多的就是观察者模式比如,IPropertyNotifyChanged接口
ay肯定用WPF方式来演示这个思想。
新建一个控件父类(被Notify对象,Observer,Update对象),和一个 桌面平台技术父类(Notify角色)
public interface Visual { string Name { get; set; } void Update(); } public interface WindowApplication { void Attach(Visual observer); void Detach(Visual observer); void Notify(); }
写一个通知技术
public class WPF : WindowApplication { List<Visual> visuals = new List<Visual>(); public void Attach(Visual observer) { visuals.Add(observer); } public void Detach(Visual observer) { visuals.Remove(observer); } public void Notify() { foreach (Visual item in visuals) { item.Update(); } } }
添加两个控件
public class Button : Visual { public Button(string name) { this.Name = name; } public string Name { get; set; } public void Update() { Console.WriteLine("Button类型:" + Name + "控件下的数据已经更新"); } } public class ToggleButton : Visual { public ToggleButton(string name) { this.Name = name; } public string Name { get; set; } public void Update() { Console.WriteLine("ToggleButton类型:" + Name + "控件下的数据已经更新"); } }
====================www.ayjs.net 杨洋 wpfui.com ayui ay aaronyang=======请不要转载谢谢了。=========
界面使用:
WPF wpf = new WPF(); Button btn1 = new Button("btn1"); wpf.Attach(btn1); Button btn2 = new Button("btn2"); wpf.Attach(btn2); Button btn3 = new Button("btn3"); wpf.Attach(btn3); ToggleButton btn4 = new ToggleButton("tbtn4"); wpf.Attach(btn4); //界面发生更新后,通知被绑定的控件 ayjs.net wpf.Notify(); Console.WriteLine(); wpf.Detach(btn3); wpf.Notify();
ay的这是第一种通知方式,下面说下第二种
当然这里推荐使用abstract类去写,关于在这个DEMO的WPF类,上方还可以再加一个父类,完成模板模式的思想,把 Attach和Detach方法提取出去,子类再继承WPF的子类。
好了,这里不多说了,先到这
====================www.ayjs.net 杨洋 wpfui.com ayui ay aaronyang=======请不要转载谢谢了。=========
抖音:wpfui 工作wpf,目前主maui
招聘合肥一枚WPF工程师,跟我一个开发组,10-15K,欢迎打扰
目前在合肥市企迈科技就职
AYUI8全源码 Github地址:前往获取
杨洋(AaronYang简称AY,安徽六安人)和AY交流
高中学历,2010年开始web开发,2015年1月17日开始学习WPF
声明:AYUI7个人与商用免费,源码可购买。部分DEMO不免费
不是从我处购买的ayui7源码,我不提供任何技术服务,如果你举报从哪里买的,我可以帮你转正为我的客户,并送demo
查看捐赠AYUI7.X MVC教程 更新如下:
第一课 第二课 程序加密教程
已有1位网友发表了看法:
IPropertyNotifyChanged=>INotifyPropertyChanged吧?
发表评论