勇哥注:
事件很常见,但有重复的代码量。
如果你想简化一下事件的编程,而且是整个程序只使用一个像事件池一样的东西,可以尝试使用下文讲的ObservableCollection观察者集合来做到。
ObservableCollection<T>类表示一个动态数据集合,在添加项,移除项或刷新整个列表时,刺激和将提供通知
通知可以由下面的枚举知晓你对集合做了什么才触发的事件。
using System; using System.Collections.Generic; using System.Collections.ObjectModel; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ConsoleApplication1 { class Program { static void Main(string[] args) { var b = new ObservableCollection<int>(); b.CollectionChanged += new System.Collections.Specialized.NotifyCollectionChangedEventHandler(b_CollectionChanged); b.Add(100); b.Add(100); b.Add(100); b[0] = 200; ; b[1] = 200; } static void b_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e) { if (sender is ObservableCollection<int>) { ObservableCollection<int> t = sender as ObservableCollection<int>; if (e.Action == System.Collections.Specialized.NotifyCollectionChangedAction.Add) { } else if (e.Action == System.Collections.Specialized.NotifyCollectionChangedAction.Remove) { } else if (e.Action == System.Collections.Specialized.NotifyCollectionChangedAction.Move) { } else if (e.Action == System.Collections.Specialized.NotifyCollectionChangedAction.Replace) { foreach (var item in e.OldItems) { Console.WriteLine(item.ToString()); ; } foreach (var item in e.NewItems) { Console.WriteLine(item.ToString()); } } else if (e.Action == System.Collections.Specialized.NotifyCollectionChangedAction.Reset) { } else { } } } } }
这个集合可以理解为其任何一个元素做操作都会触发事件。
如果你想这样:
各元素有自己独的事件。
那该如何做呢?
答案是没这种功能。但是我们可以使用多个ObservableCollection<T>创造这种功能。
勇哥像下面这样实现这个需求:
public enum WindowEventEnum { 硬件初始化完成,急停,停止,运行 } public Dictionary<WindowEventEnum, ObservableCollection<bool>> Obc = new Dictionary<WindowEventEnum, ObservableCollection<bool>>() { { WindowEventEnum.硬件初始化完成, new ObservableCollection<bool>() { false} }, { WindowEventEnum.急停, new ObservableCollection<bool>() { false} }, { WindowEventEnum.停止, new ObservableCollection<bool>() { false} }, { WindowEventEnum.运行, new ObservableCollection<bool>() { false} } };
然后其它的类来注册我们想要的事件:
var res2 = BundleRuntime.Instance.GetService<WindowsEvents>(); if (res2.Count > 0) { winEvents = res2[0]; winEvents.Obc[WindowsEvents.WindowEventEnum.硬件初始化完成].CollectionChanged += Obc_CollectionChanged; winEvents.Obc[WindowsEvents.WindowEventEnum.急停].CollectionChanged += MotionDeviceForm_CollectionChanged; winEvents.Obc[WindowsEvents.WindowEventEnum.停止].CollectionChanged += MotionDeviceForm_CollectionChanged1; winEvents.Obc[WindowsEvents.WindowEventEnum.运行].CollectionChanged += MotionDeviceForm_CollectionChanged2; }
触发事件的代码:
private void button1_Click(object sender, EventArgs e) { winEvents.Obc[WindowsEvents.WindowEventEnum.急停][0] = true; winEvents.Obc[WindowsEvents.WindowEventEnum.停止][0] = false; winEvents.Obc[WindowsEvents.WindowEventEnum.运行][0] = false; }
事件函数:
private void MotionDeviceForm_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e) { if (winEvents.Obc[WindowsEvents.WindowEventEnum.急停][0]) { flowOperation.estop(); return; } }
这样的效果是像实现了一个事件池一样的功能。
好处是不是再自己写事件了。
因为ObservableCollection<T>可以是T类型,所以事件数据源可以是任何类型,足够灵活完成你想要的数据传递。
---------------------
作者:hackpig
来源:www.skcircle.com
版权声明:本文为博主原创文章,转载请附上博文链接!

