/// <summary> /// Unregisters a messenger recipient with a matching context completely. /// After this method is executed, the recipient will no longer receive any messages. /// </summary> /// <param name="recipient"></param> /// <param name="context"></param> public void Unregister <T>(object recipient, object context) { object action; var key = new MessengerKey(recipient, context, typeof(T)); Dictionary.TryRemove(key, out action); }
/// <summary> /// Determines if the specified MessengerKey is the same to the current messengerkey /// </summary> /// <param name="_other"></param> /// <returns></returns> public bool Equals(MessengerKey _other) { if (ReferenceEquals(null, _other)) { return(false); } if (ReferenceEquals(this, _other)) { return(true); } return(Equals(Recipient, _other.Recipient) && Equals(Context, _other.Context) && Equals(Context, _other.Type)); }
public void Unsubscribe(object subscriber, MessengerEvent msgEvent) { object action; MessengerKey key = GetMessengerKey(subscriber, msgEvent); if (subscriber != null) { if (key != null) { Dictionary.TryRemove(key, out action); } } }
/// <summary> /// Registers a recipient for a type of message T and a matching context. The action parameter will be executed /// when a corresponding message is sent. /// </summary> /// <typeparam name="T"></typeparam> /// <param name="recipient"></param> /// <param name="action"></param> /// <param name="context"></param> public void Register <T>(object recipient, Action <T> action, object context) { var key = new MessengerKey(recipient, context); if (!Dictionary.TryAdd(key, action)) { //Add Failed if (Dictionary.ContainsKey(key)) { throw new ArgumentException("Recipient '" + recipient + "' for context '" + context + "' is allready registered!"); } throw new ArgumentException("Unable to add recipient."); } }
public override bool Equals(object obj) { if (ReferenceEquals(null, obj)) { return(false); } if (ReferenceEquals(this, obj)) { return(true); } if (obj.GetType() != GetType()) { return(false); } MessengerKey messageKey = obj as MessengerKey; return(this.Key.Equals(messageKey.Key) && this.Recipient.Equals(messageKey.Recipient)); }
private MessengerKey GetMessengerKey(object subscriber, MessengerEvent msgEvent) { MessengerKey item = null; try { foreach (var key in Dictionary.Keys) { if (subscriber == key.Subscriber && msgEvent == (MessengerEvent)key.Context) { item = key; break; } } } catch (Exception ex) { } return(item); }
/// <summary> /// Registers a recipient for a type of message T and a matching context. /// The action parameter will be executed when a corresponding message is send /// </summary> /// <typeparam name="T"></typeparam> /// <param name="_recipient"></param> /// <param name="_action"></param> /// <param name="_context"></param> public void Register <T>(object _recipient, Action <T> _action, object _context) { var key = new MessengerKey(_recipient, _context, typeof(T)); dictionary.TryAdd(key, _action); }
/// <summary> /// Unregisters a messenger recipient with a matching context completely. After this method is executed, the recipient will /// no longer receive any messages. /// </summary> /// <param name="recipient"></param> /// <param name="context"></param> public void Unregister(object recipient, object context) { var key = new MessengerKey(recipient, context); _dictionary.TryRemove(key, out var action); }
public void Subscribe <T>(object subscriber, MessengerEvent mgsEvent, Action <T> handler) { var key = new MessengerKey(subscriber, mgsEvent, Dictionary.Count); Dictionary.TryAdd(key, handler); }
protected bool Equals(MessengerKey key) { return(Equals(Subscriber, key.Subscriber) && Equals(Context, key.Context)); }