单列模式下的对象中事件在注册的时候,若被注册的地方进行多次重复注册(主要是初始化对象时造成)。尤其是通知到窗口的消息。不用系统给的事件,而是自定义委托后的事件
public delegate void OnSendPrintBarcodeReplyToFormDelegate(object sende, FlowDataEventArgs<BatchPrintBarcodesControlServerReply> e); private OnSendPrintBarcodeReplyToFormDelegate _onSendPrintBarcodeReplyToFormDelegateEventHandler; /// <summary> /// 通知界面事件 /// </summary> public event OnSendPrintBarcodeReplyToFormDelegate OnSendPrintBarcodeReplyToFormDelegateEventHandler { add { //关键部分,如果已经注册过事件,则能防止重复注册 if (_onSendPrintBarcodeReplyToFormDelegateEventHandler == null) _onSendPrintBarcodeReplyToFormDelegateEventHandler += value; else { //先清空后赋值,要不然消息传递到窗体时对界面的相应更改不起作用(可能失去句柄) _onSendPrintBarcodeReplyToFormDelegateEventHandler = null; _onSendPrintBarcodeReplyToFormDelegateEventHandler += value; } } remove { _onSendPrintBarcodeReplyToFormDelegateEventHandler -= value; } }