MessengerHelper.cs 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. using GalaSoft.MvvmLight.Messaging;
  2. using System;
  3. using System.Windows;
  4. namespace Muchinfo.MTPClient.Infrastructure.Helpers
  5. {
  6. public class MessengerHelper
  7. {
  8. private static readonly object _defaultRegisterLock = new object();
  9. private static readonly object _quoteRegisterLock = new object();
  10. private static readonly object _createMessengerLock = new object();
  11. private static IMessenger _quotationMessengerInstance = null;
  12. #region 行情消息单例
  13. /// <summary>
  14. /// 行情消息实例
  15. /// </summary>
  16. /// <value>The quotation messenger.</value>
  17. public static IMessenger QuotationMessenger
  18. {
  19. get
  20. {
  21. if (_quotationMessengerInstance == null)
  22. {
  23. lock (_createMessengerLock)
  24. {
  25. if (_quotationMessengerInstance == null)
  26. {
  27. _quotationMessengerInstance = new Messenger();
  28. }
  29. }
  30. }
  31. return _quotationMessengerInstance;
  32. }
  33. }
  34. #endregion
  35. #region 默认Messnenger
  36. /// <summary>
  37. /// 默认消息注册
  38. /// </summary>
  39. /// <typeparam name="TMessage">The type of the t message.</typeparam>
  40. /// <param name="recipient">The recipient.</param>
  41. /// <param name="token">The token.</param>
  42. /// <param name="action">The action.</param>
  43. /// <param name="isInDispatcher">if set to <c>true</c> [is in dispatcher].</param>
  44. public static void DefaultRegister<TMessage>(object recipient, object token, Action<TMessage> action, bool isInDispatcher = true)
  45. {
  46. lock (_defaultRegisterLock)
  47. {
  48. if (isInDispatcher)
  49. {
  50. Application.Current.Dispatcher.BeginInvoke(new Action(() =>
  51. {
  52. Messenger.Default.Register<TMessage>(recipient, token, action);
  53. }));
  54. }
  55. else
  56. {
  57. Messenger.Default.Register<TMessage>(recipient, token, action);
  58. }
  59. }
  60. }
  61. /// <summary>
  62. /// 取消默认消息注册
  63. /// </summary>
  64. /// <param name="recipient">The recipient.</param>
  65. public static void DefaultUnregister(object recipient)
  66. {
  67. lock (_defaultRegisterLock)
  68. {
  69. Messenger.Default.Unregister(recipient);
  70. }
  71. }
  72. /// <summary>
  73. /// 取消默认消息注册
  74. /// </summary>
  75. /// <typeparam name="TMessage">The type of the t message.</typeparam>
  76. /// <param name="recipient">The recipient.</param>
  77. /// <param name="token">The token.</param>
  78. public static void DefaultUnregister<TMessage>(object recipient, object token)
  79. {
  80. lock (_defaultRegisterLock)
  81. {
  82. Messenger.Default.Unregister<TMessage>(recipient, token);
  83. }
  84. }
  85. /// <summary>
  86. /// 默认消息发送
  87. /// </summary>
  88. /// <typeparam name="TMessage">The type of the t message.</typeparam>
  89. /// <param name="message">The message.</param>
  90. /// <param name="token">The token.</param>
  91. public static void DefaultSend<TMessage>(TMessage message, object token)
  92. {
  93. lock (_defaultRegisterLock)
  94. {
  95. Application.Current.Dispatcher.BeginInvoke(new Action(() =>
  96. {
  97. Messenger.Default.Send<TMessage>(message, token);
  98. }));
  99. }
  100. }
  101. #endregion
  102. #region 自定义行情Messenger
  103. /// <summary>
  104. /// 行情消息注册
  105. /// </summary>
  106. /// <typeparam name="TMessage">The type of the t message.</typeparam>
  107. /// <param name="recipient">The recipient.</param>
  108. /// <param name="token">The token.</param>
  109. /// <param name="action">The action.</param>
  110. /// <param name="isInDispatcher">if set to <c>true</c> [is in dispatcher].</param>
  111. public static void QuoteRegister<TMessage>(object recipient, object token, Action<TMessage> action, bool isInDispatcher = true)
  112. {
  113. lock (_quoteRegisterLock)
  114. {
  115. if (isInDispatcher)
  116. {
  117. Application.Current.Dispatcher.BeginInvoke(new Action(() =>
  118. {
  119. QuotationMessenger.Register<TMessage>(recipient, token, action);
  120. }));
  121. }
  122. else
  123. {
  124. QuotationMessenger.Register<TMessage>(recipient, token, action);
  125. }
  126. }
  127. }
  128. /// <summary>
  129. /// 取消行情消息注册
  130. /// </summary>
  131. /// <param name="recipient">The recipient.</param>
  132. public static void QuoteUnregister(object recipient)
  133. {
  134. lock (_quoteRegisterLock)
  135. {
  136. Application.Current.Dispatcher.BeginInvoke(new Action(() =>
  137. {
  138. QuotationMessenger.Unregister(recipient);
  139. }));
  140. }
  141. }
  142. /// <summary>
  143. /// 取消行情消息注册
  144. /// </summary>
  145. /// <typeparam name="TMessage">The type of the t message.</typeparam>
  146. /// <param name="recipient">The recipient.</param>
  147. /// <param name="token">The token.</param>
  148. public static void QuoteUnregister<TMessage>(object recipient, object token)
  149. {
  150. lock (_quoteRegisterLock)
  151. {
  152. Application.Current.Dispatcher.BeginInvoke(new Action(() =>
  153. {
  154. QuotationMessenger.Unregister<TMessage>(recipient, token);
  155. }));
  156. }
  157. }
  158. /// <summary>
  159. /// 行情消息发送
  160. /// </summary>
  161. /// <typeparam name="TMessage">The type of the t message.</typeparam>
  162. /// <param name="message">The message.</param>
  163. /// <param name="token">The token.</param>
  164. public static void QuoteSend<TMessage>(TMessage message, object token)
  165. {
  166. lock (_defaultRegisterLock)
  167. {
  168. Application.Current.Dispatcher.BeginInvoke(new Action(() =>
  169. {
  170. QuotationMessenger.Send(message, token);
  171. }));
  172. }
  173. }
  174. #endregion
  175. }
  176. }