public void Detach <TViewModelEventArgs>(string dispatcher, ViewModelEventHandler <TViewModelEventArgs> callback) where TViewModelEventArgs : ViewModelEventArgs { // [WARNING] Reflection used. May be slow. dispatcher = dispatcher ?? throw new ArgumentNullException("Dispatcher cannot be null."); callback = callback ?? throw new ArgumentNullException("Callback cannot be null."); Type ty = GetType(); EventInfo e = ty.GetEvent(dispatcher, BindingFlags.Instance | BindingFlags.Public); if (e.MemberType != MemberTypes.Event) { throw new SystemException("\'" + dispatcher + "\' is not a valid dispatcher."); } if (e == null) { throw new DispatcherNotFoundException("Could not find dispatcher \'" + dispatcher + "\"."); } if (callback.GetType() != e.EventHandlerType) { throw new DispatcherTypeMismatchException("Dispatcher type does not match with the callback type."); } e.RemoveEventHandler(this, callback); }
/// <summary> /// Subscribes to an <see cref="IViewModelEvent{TPayload}"/> event /// </summary> protected virtual void SubscribeToEvent <TEvent, TArgs>(ViewModelEventHandler <TArgs> handler) where TEvent : class, IViewModelEvent <TArgs>, new() { var targetEvent = _eventDispatcher.GetEvent <TEvent>(); targetEvent.Subscribe(this, handler); }
/// <summary> /// Add a handler to the event. /// </summary> /// <param name="group">The ViewModel group.</param> /// <param name="handler">The event handler.</param> public virtual void Subscribe(string group, ViewModelEventHandler <TPayload> handler) { lock (_handlers) { var list = _handlers.ContainsKey(group) ? _handlers[group] : (_handlers[group] = new List <ViewModelEventHandler <TPayload> >()); list.Add(handler); } }
/// <summary> /// Subscribes to an <see cref="ViewModelsGroupEvent{TPayload}"/> event /// </summary> protected virtual void SubscribeToGroupEvent <TEvent, TArgs>(string viewModelGroup, ViewModelEventHandler <TArgs> handler) where TEvent : ViewModelsGroupEvent <TArgs>, new() { var targetEvent = _eventDispatcher.GetEvent <TEvent>(); targetEvent.Subscribe(viewModelGroup, handler); }
/// <summary> /// Subscribes to an <see cref="ViewModelsGroupEvent{TPayload}"/> event /// </summary> protected virtual void SubscribeToGroupEvent <TEvent, TArgs>(ViewModelEventHandler <TArgs> handler) where TEvent : ViewModelsGroupEvent <TArgs>, new() { if (string.IsNullOrEmpty(ViewModelGroup)) { throw new InvalidOperationException("ViewModelGroup is not set."); } SubscribeToGroupEvent <TEvent, TArgs>(ViewModelGroup, handler); }
/// <summary> /// Remove a handler from the event. /// </summary> /// <param name="group">The ViewModel group.</param> /// <param name="handler">The event handler.</param> public virtual void Unsubscribe(string group, ViewModelEventHandler <TPayload> handler) { lock (_handlers) { if (_handlers.ContainsKey(group)) { _handlers[group].Remove(handler); } } }