在silverlight中发布和订阅事件

时间:2017-08-04 08:00:02

标签: c# silverlight

我正在尝试理解传统silverlight应用程序中的一段代码。在此代码中,您可以看到用户可以发布事件并订阅该事件。

发布活动时

 Messenger.Publish<ErrorInformationData>(MessageConstant.ShowMessageTechnicalError, 
 new ErrorInformationData(ServiceOperationName.ClaimsService_GetClaim, ServiceOperationOccurence.DataServiceUI));

然后订阅该事件

Messenger.Subscribe<ErrorInformationData>(
                                this,
                                MessageConstant.ShowMessageTechnicalError,
                                 (result) =>
                                 {
                                     Messenger.Publish(MessageConstant.ShowMessage,
                                         new MessageData
                                         {
                                             Title = "Common$TechnicalError",
                                             MessageText = "Common$TechnicalErrorDetail",
                                             TextParameters = new object[] { result.OperationErrorCode.ToString() },
                                             MessageType = MessageData.MessageTypes.OK,
                                             OKAction = () =>
                                             {
                                                 HtmlPage.Window.Navigate(new Uri("", UriKind.Relative));
                                             },

                                             MessageLevel = MessageData.MessageLevels.Error
                                         }
                                     );
                                 }
                                );

问题是为什么我需要使用这个框架,而我可以简单地调用一个方法。也可以任何人指向有关此通信的任何文档/教程。

1 个答案:

答案 0 :(得分:1)

感谢@Nikosi的指针,经过更多的调查,我发现了幕后发生了什么。

所以有一个IMessanger接口,其中包含发布,订阅和取消订阅方法的签名。

public interface IMessanger : IDisposable
{
       void Subscribe(IReceiver receiver, int messageId);     
       void Publish<TEventArgs>(object sender, TEventArgs e, int messageId) 
           where TEventArgs : EventArgs;     
       void Unsubscribe(IReceiver receiver, int messageId);
}

现在我们定义一个实现接口的类Messanger

public sealed class Messanger : IMessanger
   {
       private readonly Dictionary<int, List<IReceiver>> messageIdToReceiver;

       public Messanger()
       {
           this.messageIdToReceiver = new Dictionary<int, List<IReceiver>>();
       }

       public void Subscribe(IReceiver receiver, int messageId)
       {
           List<IReceiver> receivers;

           if (this.messageIdToReceiver.TryGetValue(messageId, out receivers))
           {
               receivers.Add(receiver);
           }
           else
           {
               this.messageIdToReceiver.Add(messageId, new List<IReceiver>() { receiver });
           }
       }

       public void Publish<TEventArgs>(object sender, TEventArgs e, int messageId) 
           where TEventArgs : EventArgs
       {
           List<IReceiver> receivers;

           if (this.messageIdToReceiver.TryGetValue(messageId, out receivers))
           {
               foreach (IReceiver receiver in receivers)
               {
                   IReceiver<TEventArgs> receiverToReceive = receiver as IReceiver<TEventArgs>;

                   if (receiverToReceive != null)
                   {
                       receiverToReceive.Receive(sender, e, messageId);
                   }
               }
           }
       }

       public void Unsubscribe(IReceiver receiver, int messageId) 
       {
           List<IReceiver> receivers;

           if (this.messageIdToReceiver.TryGetValue(messageId, out receivers))
           {
               if (receivers.Count > 1)
               {
                   receivers.Remove(receiver);
               }
               else if(receivers.Count == 1)
               {
                   this.messageIdToReceiver.Remove(messageId);
               }
           }
       }

       public void Dispose()
       {
           this.messageIdToReceiver.Clear();
       }
   }

 public interface IReceiver<TEventArgs> : IReceiver
       where TEventArgs : EventArgs
   {
       void Receive(object sender, TEventArgs e, int messageId);
   }

   public interface IReceiver : IDisposable
   {

   }

现在我们可以看到上面的用法,定义了两个类,一个是发布事件,另一个是接收事件。

public class PresenterA : IReceiver<EventArgs>, IDisposable
   {
       readonly IMessanger messanger;

       public PresenterA(IMessanger messanger)
       {
           this.messanger = messanger;
           this.messanger.Subscribe(this, (int)PubSubMsg.AppInstl);
       }

       public void Receive(object sender, EventArgs e, int messageId)
       {
           if ((PubSubMsg)messageId == PubSubMsg.AppInstl)
           {
               //Now that you received the message, update the UI, etc...
           }
       }

       public void Dispose()
       {
           this.messanger.Unsubscribe(this, (int)PubSubMsg.AppInstl);
       }
   }

   public class PresenterB
   {
       readonly IMessanger messanger;

       public PresenterB(IMessanger messanger)
       {
           this.messanger = messanger;
       }

       public void btnApplicationRemove(object sender, EventArgs e)
       {
           //Do what you need to do and then publish the message
           this.messanger.Publish<EventArgs>(this, e, (int)PubSubMsg.AppInstl);
       }
   }

   public enum PubSubMsg
   {
       AppInstl = 1
   }