using GalaSoft.MvvmLight.Messaging; using System; using System.Windows; namespace Muchinfo.MTPClient.Infrastructure.Helpers { public class MessengerHelper { private static readonly object _defaultRegisterLock = new object(); private static readonly object _quoteRegisterLock = new object(); private static readonly object _createMessengerLock = new object(); private static IMessenger _quotationMessengerInstance = null; #region 行情消息单例 /// /// 行情消息实例 /// /// The quotation messenger. public static IMessenger QuotationMessenger { get { if (_quotationMessengerInstance == null) { lock (_createMessengerLock) { if (_quotationMessengerInstance == null) { _quotationMessengerInstance = new Messenger(); } } } return _quotationMessengerInstance; } } #endregion #region 默认Messnenger /// /// 默认消息注册 /// /// The type of the t message. /// The recipient. /// The token. /// The action. /// if set to true [is in dispatcher]. public static void DefaultRegister(object recipient, object token, Action action, bool isInDispatcher = true) { lock (_defaultRegisterLock) { if (isInDispatcher) { Application.Current.Dispatcher.BeginInvoke(new Action(() => { Messenger.Default.Register(recipient, token, action); })); } else { Messenger.Default.Register(recipient, token, action); } } } /// /// 取消默认消息注册 /// /// The recipient. public static void DefaultUnregister(object recipient) { lock (_defaultRegisterLock) { Messenger.Default.Unregister(recipient); } } /// /// 取消默认消息注册 /// /// The type of the t message. /// The recipient. /// The token. public static void DefaultUnregister(object recipient, object token) { lock (_defaultRegisterLock) { Messenger.Default.Unregister(recipient, token); } } /// /// 默认消息发送 /// /// The type of the t message. /// The message. /// The token. public static void DefaultSend(TMessage message, object token) { lock (_defaultRegisterLock) { Application.Current.Dispatcher.BeginInvoke(new Action(() => { Messenger.Default.Send(message, token); })); } } #endregion #region 自定义行情Messenger /// /// 行情消息注册 /// /// The type of the t message. /// The recipient. /// The token. /// The action. /// if set to true [is in dispatcher]. public static void QuoteRegister(object recipient, object token, Action action, bool isInDispatcher = true) { lock (_quoteRegisterLock) { if (isInDispatcher) { Application.Current.Dispatcher.BeginInvoke(new Action(() => { QuotationMessenger.Register(recipient, token, action); })); } else { QuotationMessenger.Register(recipient, token, action); } } } /// /// 取消行情消息注册 /// /// The recipient. public static void QuoteUnregister(object recipient) { lock (_quoteRegisterLock) { Application.Current.Dispatcher.BeginInvoke(new Action(() => { QuotationMessenger.Unregister(recipient); })); } } /// /// 取消行情消息注册 /// /// The type of the t message. /// The recipient. /// The token. public static void QuoteUnregister(object recipient, object token) { lock (_quoteRegisterLock) { Application.Current.Dispatcher.BeginInvoke(new Action(() => { QuotationMessenger.Unregister(recipient, token); })); } } /// /// 行情消息发送 /// /// The type of the t message. /// The message. /// The token. public static void QuoteSend(TMessage message, object token) { lock (_defaultRegisterLock) { Application.Current.Dispatcher.BeginInvoke(new Action(() => { QuotationMessenger.Send(message, token); })); } } #endregion } }