/// <summary> /// 订阅消息 /// </summary> /// <param name="action"></param> /// <param name="threadOption"></param> /// <param name="keepSubscriberReferenceAlive"></param> public void Subscribe(string subscribeid, Action action, ThreadOption threadOption, bool keepSubscriberReferenceAlive) { IDelegateReference actionReference = new DelegateReference(action, keepSubscriberReferenceAlive); IEventSubscription subscription; switch (threadOption) { case ThreadOption.PublisherThread: subscription = new EventSubscription(actionReference); break; case ThreadOption.BackgroundThread: subscription = new BackgroundEventSubscription(actionReference); break; case ThreadOption.UIThread: subscription = new DispatcherEventSubscription(actionReference); break; default: subscription = new EventSubscription(actionReference); break; } AddSubscriprion(subscribeid, subscription); }
/// <summary> /// 支持过滤 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="subscribeid"></param> /// <param name="action"></param> /// <param name="threadOption"></param> /// <param name="keepSubscriberReferenceAlive"></param> /// <param name="filter"></param> public void Subscribe <T>(string subscribeid, Action <T> action, ThreadOption threadOption, bool keepSubscriberReferenceAlive, Predicate <T> filter) { IDelegateReference actionReference = new DelegateReference(action, keepSubscriberReferenceAlive); IDelegateReference filterReference; if (filter != null) { filterReference = new DelegateReference(filter, keepSubscriberReferenceAlive); } else { filterReference = new DelegateReference(new Predicate <T>(delegate { return(true); }), false); } IEventSubscription subscription; switch (threadOption) { case ThreadOption.PublisherThread: subscription = new EventSubscription <T>(actionReference, filterReference); break; case ThreadOption.BackgroundThread: subscription = new BackgroundEventSubscription <T>(actionReference, filterReference); break; case ThreadOption.UIThread: subscription = new DispatcherEventSubscription <T>(actionReference, filterReference); break; default: subscription = new EventSubscription <T>(actionReference, filterReference); break; } AddSubscriprion(subscribeid, subscription); }