/// <summary> /// Unsubscribes from an event. /// </summary> /// <typeparam name="T">The type of the event arguments.</typeparam> /// <param name="target">The event source.</param> /// <param name="eventName">The name of the event.</param> /// <param name="subscriber">The subscriber.</param> public static void Unsubscribe <T>(object target, string eventName, IWeakSubscriber <T> subscriber) where T : EventArgs { if (SubscriptionTypeStorage <T> .Subscribers.TryGetValue(target, out var dic)) { if (dic.TryGetValue(eventName, out var sub)) { sub.Remove(subscriber); } } }
public static void Subscribe <T>(object target, string eventName, IWeakSubscriber <T> subscriber) { var dic = SubscriptionTypeStorage <T> .Subscribers.GetOrCreateValue(target); Subscription <T> sub; if (!dic.TryGetValue(eventName, out sub)) { dic[eventName] = sub = new Subscription <T>(dic, target, eventName); } sub.Add(new WeakReference <IWeakSubscriber <T> >(subscriber)); }
public void Remove(IWeakSubscriber <T> s) { var removed = false; for (int c = 0; c < _count; ++c) { var reference = _data[c]; IWeakSubscriber <T> instance; if (reference != null && reference.TryGetTarget(out instance) && instance == s) { _data[c] = null; removed = true; } } if (removed) { Compact(); } }
public static void SubscribeWeak(IWeakSubscriber <EventArgs> subscriber) => WeakSubscriptionManager.Subscribe(InternalEventManager, nameof(EventManager.SelectedThemeChanged), subscriber);
/// <summary> /// Subscribes to an event on an object using a weak subscription. /// </summary> /// <typeparam name="TTarget">The type of the target.</typeparam> /// <typeparam name="TEventArgs">The type of the event arguments.</typeparam> /// <param name="target">The event source.</param> /// <param name="eventName">The name of the event.</param> /// <param name="subscriber">The subscriber.</param> public static void Subscribe <TTarget, TEventArgs>(TTarget target, string eventName, IWeakSubscriber <TEventArgs> subscriber) where TEventArgs : EventArgs { var dic = SubscriptionTypeStorage <TEventArgs> .Subscribers.GetOrCreateValue(target); Subscription <TEventArgs> sub; if (!dic.TryGetValue(eventName, out sub)) { dic[eventName] = sub = new Subscription <TEventArgs>(dic, typeof(TTarget), target, eventName); } sub.Add(new WeakReference <IWeakSubscriber <TEventArgs> >(subscriber)); }