| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189 |
- 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 行情消息单例
- /// <summary>
- /// 行情消息实例
- /// </summary>
- /// <value>The quotation messenger.</value>
- public static IMessenger QuotationMessenger
- {
- get
- {
- if (_quotationMessengerInstance == null)
- {
- lock (_createMessengerLock)
- {
- if (_quotationMessengerInstance == null)
- {
- _quotationMessengerInstance = new Messenger();
- }
- }
- }
- return _quotationMessengerInstance;
- }
- }
- #endregion
- #region 默认Messnenger
- /// <summary>
- /// 默认消息注册
- /// </summary>
- /// <typeparam name="TMessage">The type of the t message.</typeparam>
- /// <param name="recipient">The recipient.</param>
- /// <param name="token">The token.</param>
- /// <param name="action">The action.</param>
- /// <param name="isInDispatcher">if set to <c>true</c> [is in dispatcher].</param>
- public static void DefaultRegister<TMessage>(object recipient, object token, Action<TMessage> action, bool isInDispatcher = true)
- {
- lock (_defaultRegisterLock)
- {
- if (isInDispatcher)
- {
- Application.Current.Dispatcher.BeginInvoke(new Action(() =>
- {
- Messenger.Default.Register<TMessage>(recipient, token, action);
- }));
- }
- else
- {
- Messenger.Default.Register<TMessage>(recipient, token, action);
- }
- }
- }
- /// <summary>
- /// 取消默认消息注册
- /// </summary>
- /// <param name="recipient">The recipient.</param>
- public static void DefaultUnregister(object recipient)
- {
- lock (_defaultRegisterLock)
- {
- Messenger.Default.Unregister(recipient);
- }
- }
- /// <summary>
- /// 取消默认消息注册
- /// </summary>
- /// <typeparam name="TMessage">The type of the t message.</typeparam>
- /// <param name="recipient">The recipient.</param>
- /// <param name="token">The token.</param>
- public static void DefaultUnregister<TMessage>(object recipient, object token)
- {
- lock (_defaultRegisterLock)
- {
- Messenger.Default.Unregister<TMessage>(recipient, token);
- }
- }
- /// <summary>
- /// 默认消息发送
- /// </summary>
- /// <typeparam name="TMessage">The type of the t message.</typeparam>
- /// <param name="message">The message.</param>
- /// <param name="token">The token.</param>
- public static void DefaultSend<TMessage>(TMessage message, object token)
- {
- lock (_defaultRegisterLock)
- {
- Application.Current.Dispatcher.BeginInvoke(new Action(() =>
- {
- Messenger.Default.Send<TMessage>(message, token);
- }));
- }
- }
- #endregion
- #region 自定义行情Messenger
- /// <summary>
- /// 行情消息注册
- /// </summary>
- /// <typeparam name="TMessage">The type of the t message.</typeparam>
- /// <param name="recipient">The recipient.</param>
- /// <param name="token">The token.</param>
- /// <param name="action">The action.</param>
- /// <param name="isInDispatcher">if set to <c>true</c> [is in dispatcher].</param>
- public static void QuoteRegister<TMessage>(object recipient, object token, Action<TMessage> action, bool isInDispatcher = true)
- {
- lock (_quoteRegisterLock)
- {
- if (isInDispatcher)
- {
- Application.Current.Dispatcher.BeginInvoke(new Action(() =>
- {
- QuotationMessenger.Register<TMessage>(recipient, token, action);
- }));
- }
- else
- {
- QuotationMessenger.Register<TMessage>(recipient, token, action);
- }
- }
- }
- /// <summary>
- /// 取消行情消息注册
- /// </summary>
- /// <param name="recipient">The recipient.</param>
- public static void QuoteUnregister(object recipient)
- {
- lock (_quoteRegisterLock)
- {
- Application.Current.Dispatcher.BeginInvoke(new Action(() =>
- {
- QuotationMessenger.Unregister(recipient);
- }));
- }
- }
- /// <summary>
- /// 取消行情消息注册
- /// </summary>
- /// <typeparam name="TMessage">The type of the t message.</typeparam>
- /// <param name="recipient">The recipient.</param>
- /// <param name="token">The token.</param>
- public static void QuoteUnregister<TMessage>(object recipient, object token)
- {
- lock (_quoteRegisterLock)
- {
- Application.Current.Dispatcher.BeginInvoke(new Action(() =>
- {
- QuotationMessenger.Unregister<TMessage>(recipient, token);
- }));
- }
- }
- /// <summary>
- /// 行情消息发送
- /// </summary>
- /// <typeparam name="TMessage">The type of the t message.</typeparam>
- /// <param name="message">The message.</param>
- /// <param name="token">The token.</param>
- public static void QuoteSend<TMessage>(TMessage message, object token)
- {
- lock (_defaultRegisterLock)
- {
- Application.Current.Dispatcher.BeginInvoke(new Action(() =>
- {
- QuotationMessenger.Send(message, token);
- }));
- }
- }
- #endregion
- }
- }
|