public IDisposable AddClassHandler( Type targetType, EventHandler <RoutedEventArgs> handler, RoutingStrategies routes = RoutingStrategies.Direct | RoutingStrategies.Bubble, bool handledEventsToo = false) { return(Raised.Subscribe(args => { var sender = args.Item1; var e = args.Item2; if (targetType.GetTypeInfo().IsAssignableFrom(sender.GetType().GetTypeInfo()) && ((e.Route == RoutingStrategies.Direct) || (e.Route & routes) != 0) && (!e.Handled || handledEventsToo)) { try { handler.DynamicInvoke(sender, e); } catch (TargetInvocationException ex) { // Unwrap the inner exception. ExceptionDispatchInfo.Capture(ex.InnerException).Throw(); } } })); }
/// <summary> /// Adds a handler for the specified routed event. /// </summary> /// <param name="routedEvent">The routed event.</param> /// <param name="handler">The handler.</param> /// <param name="routes">The routing strategies to listen to.</param> /// <param name="handledEventsToo">Whether handled events should also be listened for.</param> /// <returns>A disposable that terminates the event subscription.</returns> public IDisposable AddHandler( RoutedEvent routedEvent, Delegate handler, RoutingStrategies routes = RoutingStrategies.Direct | RoutingStrategies.Bubble, bool handledEventsToo = false) { Contract.Requires <ArgumentNullException>(routedEvent != null); Contract.Requires <ArgumentNullException>(handler != null); List <EventSubscription> subscriptions; if (!_eventHandlers.TryGetValue(routedEvent, out subscriptions)) { subscriptions = new List <EventSubscription>(); _eventHandlers.Add(routedEvent, subscriptions); } var sub = new EventSubscription { Handler = handler, Routes = routes, AlsoIfHandled = handledEventsToo, }; subscriptions.Add(sub); return(Disposable.Create(() => subscriptions.Remove(sub))); }
/// <summary> /// Adds a handler for the specified routed event. /// </summary> /// <typeparam name="TEventArgs">The type of the event's args.</typeparam> /// <param name="routedEvent">The routed event.</param> /// <param name="handler">The handler.</param> /// <param name="routes">The routing strategies to listen to.</param> /// <param name="handledEventsToo">Whether handled events should also be listened for.</param> /// <returns>A disposable that terminates the event subscription.</returns> public IDisposable AddHandler <TEventArgs>( RoutedEvent <TEventArgs> routedEvent, EventHandler <TEventArgs> handler, RoutingStrategies routes = RoutingStrategies.Direct | RoutingStrategies.Bubble, bool handledEventsToo = false) where TEventArgs : RoutedEventArgs { return(AddHandler(routedEvent, (Delegate)handler, routes, handledEventsToo)); }
/// <summary> /// Adds a handler for the specified routed event. /// </summary> /// <typeparam name="TEventArgs">The type of the event's args.</typeparam> /// <param name="routedEvent">The routed event.</param> /// <param name="handler">The handler.</param> /// <param name="routes">The routing strategies to listen to.</param> /// <param name="handledEventsToo">Whether handled events should also be listened for.</param> public void AddHandler <TEventArgs>( RoutedEvent <TEventArgs> routedEvent, EventHandler <TEventArgs> handler, RoutingStrategies routes = RoutingStrategies.Direct | RoutingStrategies.Bubble, bool handledEventsToo = false) where TEventArgs : RoutedEventArgs { routedEvent = routedEvent ?? throw new ArgumentNullException(nameof(routedEvent)); handler = handler ?? throw new ArgumentNullException(nameof(handler));
public EventChainLink(object handler, bool handled, RoutingStrategies route) { Contract.Requires <ArgumentNullException>(handler != null); this.Handler = handler; this.Handled = handled; this.Route = route; }
/// <summary> /// Registers a RoutedEvent with the given details /// </summary> /// <typeparam name="TOwner"></typeparam> /// <typeparam name="TEventArgs"></typeparam> /// <param name="name"></param> /// <param name="routingStrategy"></param> /// <returns></returns> public static RoutedEvent <TEventArgs> Register <TOwner, TEventArgs>( string name, RoutingStrategies routingStrategy) where TEventArgs : RoutedEventArgs { Contract.Requires <ArgumentNullException>(name != null); return(new RoutedEvent <TEventArgs>(name, routingStrategy, typeof(TOwner))); }
public static RoutedEvent <TEventArgs> Register <TEventArgs>( string name, RoutingStrategies routingStrategy, Type ownerType) where TEventArgs : RoutedEventArgs { Contract.Requires <NullReferenceException>(name != null); return(new RoutedEvent <TEventArgs>(name, routingStrategy, ownerType)); }
/// <summary> /// Adds a handler for the specified routed event and returns a disposable that can terminate the event subscription. /// </summary> /// <typeparam name="TEventArgs">The type of the event's args.</typeparam> /// <param name="o">Target for adding given event handler.</param> /// <param name="routedEvent">The routed event.</param> /// <param name="handler">The handler.</param> /// <param name="routes">The routing strategies to listen to.</param> /// <param name="handledEventsToo">Whether handled events should also be listened for.</param> /// <returns>A disposable that terminates the event subscription.</returns> public static IDisposable AddDisposableHandler <TEventArgs>(this IInteractive o, RoutedEvent <TEventArgs> routedEvent, EventHandler <TEventArgs> handler, RoutingStrategies routes = RoutingStrategies.Direct | RoutingStrategies.Bubble, bool handledEventsToo = false) where TEventArgs : RoutedEventArgs { o.AddHandler(routedEvent, handler, routes, handledEventsToo); return(Disposable.Create( (instance: o, handler, routedEvent), state => state.instance.RemoveHandler(state.routedEvent, state.handler))); }
/// <summary> /// Adds a handler to the route. /// </summary> /// <param name="target">The target on which the event should be raised.</param> /// <param name="handler">The handler for the event.</param> /// <param name="routes">The routing strategies to listen to.</param> /// <param name="handledEventsToo"> /// If true the handler will be raised even when the routed event is marked as handled. /// </param> /// <param name="adapter"> /// An optional adapter which if supplied, will be called with <paramref name="handler"/> /// and the parameters for the event. This adapter can be used to avoid calling /// `DynamicInvoke` on the handler. /// </param> public void Add( IInteractive target, Delegate handler, RoutingStrategies routes, bool handledEventsToo = false, Action <Delegate, object, RoutedEventArgs>?adapter = null) { target = target ?? throw new ArgumentNullException(nameof(target)); handler = handler ?? throw new ArgumentNullException(nameof(handler)); _route ??= new PooledList <RouteItem>(16); _route.Add(new RouteItem(target, handler, adapter, routes, handledEventsToo)); }
public RouteItem( IInteractive target, Delegate?handler, Action <Delegate, object, RoutedEventArgs>?adapter, RoutingStrategies routes, bool handledEventsToo) { Target = target; Handler = handler; Adapter = adapter; Routes = routes; HandledEventsToo = handledEventsToo; }
/// <summary> /// Gets an observable for a <see cref="RoutedEvent{TEventArgs}"/>. /// </summary> /// <param name="o">The object to listen for events on.</param> /// <param name="routedEvent">The routed event.</param> /// <param name="routes">The routing strategies to listen to.</param> /// <param name="handledEventsToo">Whether handled events should also be listened for.</param> /// <returns> /// An observable which fires each time the event is raised. /// </returns> public static IObservable <TEventArgs> GetObservable <TEventArgs>( this IInteractive o, RoutedEvent <TEventArgs> routedEvent, RoutingStrategies routes = RoutingStrategies.Direct | RoutingStrategies.Bubble, bool handledEventsToo = false) where TEventArgs : RoutedEventArgs { return(Observable.Create <TEventArgs>(x => o.AddHandler( routedEvent, (_, e) => x.OnNext(e), routes, handledEventsToo))); }
public static RoutedEvent <TEventArgs> Register <TEventArgs>( string name, RoutingStrategies routingStrategy, Type ownerType) where TEventArgs : RoutedEventArgs { Contract.Requires <ArgumentNullException>(name != null); var routedEvent = new RoutedEvent <TEventArgs>(name, routingStrategy, ownerType); RoutedEventRegistry.Instance.Register(ownerType, routedEvent); return(routedEvent); }
/// <summary> /// Adds a handler for the specified routed event. /// </summary> /// <param name="routedEvent">The routed event.</param> /// <param name="handler">The handler.</param> /// <param name="routes">The routing strategies to listen to.</param> /// <param name="handledEventsToo">Whether handled events should also be listened for.</param> public void AddHandler( RoutedEvent routedEvent, Delegate handler, RoutingStrategies routes = RoutingStrategies.Direct | RoutingStrategies.Bubble, bool handledEventsToo = false) { routedEvent = routedEvent ?? throw new ArgumentNullException(nameof(routedEvent)); handler = handler ?? throw new ArgumentNullException(nameof(handler)); var subscription = new EventSubscription(handler, routes, handledEventsToo); AddEventSubscription(routedEvent, subscription); }
public IDisposable AddClassHandler <TTarget>( Action <TTarget, TEventArgs> handler, RoutingStrategies routes = RoutingStrategies.Direct | RoutingStrategies.Bubble, bool handledEventsToo = false) where TTarget : class, IInteractive { void Adapter(object?sender, RoutedEventArgs e) { if (sender is TTarget target && e is TEventArgs args) { handler(target, args); } } return(AddClassHandler(typeof(TTarget), Adapter, routes, handledEventsToo)); }
public RoutedEvent( string name, RoutingStrategies routingStrategies, Type eventArgsType, Type ownerType) { Contract.Requires <ArgumentNullException>(name != null); Contract.Requires <ArgumentNullException>(eventArgsType != null); Contract.Requires <ArgumentNullException>(ownerType != null); Contract.Requires <InvalidCastException>(typeof(RoutedEventArgs).GetTypeInfo().IsAssignableFrom(eventArgsType.GetTypeInfo())); EventArgsType = eventArgsType; Name = name; OwnerType = ownerType; RoutingStrategies = routingStrategies; }
/// <summary> /// Gets an observable for a <see cref="RoutedEvent{TEventArgs}"/>. /// </summary> /// <param name="o">The object to listen for events on.</param> /// <param name="routedEvent">The routed event.</param> /// <param name="routes">The routing strategies to listen to.</param> /// <param name="handledEventsToo">Whether handled events should also be listened for.</param> /// <returns> /// An observable which fires each time the event is raised. /// </returns> public static IObservable <TEventArgs> GetObservable <TEventArgs>( this IInteractive o, RoutedEvent <TEventArgs> routedEvent, RoutingStrategies routes = RoutingStrategies.Direct | RoutingStrategies.Bubble, bool handledEventsToo = false) where TEventArgs : RoutedEventArgs { o = o ?? throw new ArgumentNullException(nameof(o)); routedEvent = routedEvent ?? throw new ArgumentNullException(nameof(routedEvent)); return(Observable.Create <TEventArgs>(x => o.AddDisposableHandler( routedEvent, (_, e) => x.OnNext(e), routes, handledEventsToo))); }
/// <summary> /// Adds a handler for the specified routed event. /// </summary> /// <param name="routedEvent">The routed event.</param> /// <param name="handler">The handler.</param> /// <param name="routes">The routing strategies to listen to.</param> /// <param name="handledEventsToo">Whether handled events should also be listened for.</param> /// <returns>A disposable that terminates the event subscription.</returns> public IDisposable AddHandler( RoutedEvent routedEvent, Delegate handler, RoutingStrategies routes = RoutingStrategies.Direct | RoutingStrategies.Bubble, bool handledEventsToo = false) { Contract.Requires <ArgumentNullException>(routedEvent != null); Contract.Requires <ArgumentNullException>(handler != null); var subscription = new EventSubscription { Handler = handler, Routes = routes, AlsoIfHandled = handledEventsToo, }; return(AddEventSubscription(routedEvent, subscription)); }
public IDisposable AddClassHandler <TTarget>( Func <TTarget, Action <TEventArgs> > handler, RoutingStrategies routes = RoutingStrategies.Direct | RoutingStrategies.Bubble, bool handledEventsToo = false) where TTarget : class, IInteractive { EventHandler <RoutedEventArgs> adapter = (sender, e) => { var target = sender as TTarget; var args = e as TEventArgs; if (target != null && args != null) { handler(target)(args); } }; return(AddClassHandler(typeof(TTarget), adapter, routes, handledEventsToo)); }
private TestInteractive CreateTree( RoutedEvent ev, EventHandler <RoutedEventArgs> handler, RoutingStrategies handlerRoutes, bool handledEventsToo = false) { TestInteractive target; var tree = new TestInteractive { Name = "1", Children = new[] { new TestInteractive { Name = "2a", }, (target = new TestInteractive { Name = "2b", Children = new[] { new TestInteractive { Name = "3", }, }, }), } }; if (handler != null) { foreach (var i in tree.GetSelfAndVisualDescendents().Cast <Interactive>()) { i.AddHandler(ev, handler, handlerRoutes, handledEventsToo); } } return(target); }
/// <summary> /// Adds a handler for the specified routed event. /// </summary> /// <typeparam name="TEventArgs">The type of the event's args.</typeparam> /// <param name="routedEvent">The routed event.</param> /// <param name="handler">The handler.</param> /// <param name="routes">The routing strategies to listen to.</param> /// <param name="handledEventsToo">Whether handled events should also be listened for.</param> /// <returns>A disposable that terminates the event subscription.</returns> public IDisposable AddHandler <TEventArgs>( RoutedEvent <TEventArgs> routedEvent, EventHandler <TEventArgs> handler, RoutingStrategies routes = RoutingStrategies.Direct | RoutingStrategies.Bubble, bool handledEventsToo = false) where TEventArgs : RoutedEventArgs { Contract.Requires <ArgumentNullException>(routedEvent != null); Contract.Requires <ArgumentNullException>(handler != null); // EventHandler delegate is not covariant, this forces us to create small wrapper // that will cast our type erased instance and invoke it. Type eventArgsType = routedEvent.EventArgsType; if (!s_invokeHandlerCache.TryGetValue(eventArgsType, out var invokeAdapter)) { void InvokeAdapter(Delegate baseHandler, object sender, RoutedEventArgs args) { var typedHandler = (EventHandler <TEventArgs>)baseHandler; var typedArgs = (TEventArgs)args; typedHandler(sender, typedArgs); } invokeAdapter = InvokeAdapter; s_invokeHandlerCache.Add(eventArgsType, invokeAdapter); } var subscription = new EventSubscription { InvokeAdapter = invokeAdapter, Handler = handler, Routes = routes, AlsoIfHandled = handledEventsToo, }; return(AddEventSubscription(routedEvent, subscription)); }
public void AddToChain(object handler, bool handled, RoutingStrategies route) { AddToChain(new EventChainLink(handler, handled, route)); }
public MetadataTestClass(string name, RoutingStrategies routingStrategies, Type eventArgsType, Type ownerType) : base(name, routingStrategies, eventArgsType, ownerType) { }
static public void Register(this Control item, RoutedEvent routed_event, FunctionSyncro function, RoutingStrategies routes = RoutingStrategies.Bubble) { item.AddHandler(routed_event, function.GetRoutedEventHandler <RoutedEventArgs>(), routes); }
public RoutedEvent(string name, RoutingStrategies routingStrategies, Type ownerType) : base(name, routingStrategies, typeof(TEventArgs), ownerType) { }
public void AddClassHandler(Type type, EventHandler <RoutedEventArgs> handler, RoutingStrategies routes) { _subscriptions.Add(new ClassEventSubscription { TargetType = type, Handler = handler, Routes = routes, }); }
public RoutedEvent(string name, RoutingStrategies routingStrategies, Type ownerType) : base(name, routingStrategies, typeof(TEventArgs), ownerType) { Contract.Requires <ArgumentNullException>(name != null); Contract.Requires <ArgumentNullException>(ownerType != null); }
public EventChainLink(object handler, bool handled, RoutingStrategies route) { Handler = handler ?? throw new ArgumentNullException(nameof(handler)); Handled = handled; Route = route; }
private TestInteractive CreateTree( RoutedEvent ev, EventHandler<RoutedEventArgs> handler, RoutingStrategies handlerRoutes, bool handledEventsToo = false) { TestInteractive target; var tree = new TestInteractive { Name = "1", Children = new[] { new TestInteractive { Name = "2a", }, (target = new TestInteractive { Name = "2b", Children = new[] { new TestInteractive { Name = "3", }, }, }), } }; if (handler != null) { foreach (var i in tree.GetSelfAndVisualDescendents().Cast<Interactive>()) { i.AddHandler(ev, handler, handlerRoutes, handledEventsToo); } } return target; }
public static RoutedEvent Register <TOwner, TEventArgs>(string name, RoutingStrategies routing) where TEventArgs : RoutedEventArgs { return(RoutedEvent.Register <TOwner, TEventArgs>(name, routing)); }