diff --git a/src/Avalonia.Base/Interactivity/Interactive.cs b/src/Avalonia.Base/Interactivity/Interactive.cs index 821e00d784..3bdaa60d2e 100644 --- a/src/Avalonia.Base/Interactivity/Interactive.cs +++ b/src/Avalonia.Base/Interactivity/Interactive.cs @@ -1,7 +1,6 @@ using System; using System.Collections.Generic; using Avalonia.Layout; -using Avalonia.VisualTree; #nullable enable @@ -67,7 +66,38 @@ namespace Avalonia.Interactivity typedHandler(sender, typedArgs); } - var subscription = new EventSubscription(handler, routes, handledEventsToo, (baseHandler, sender, args) => InvokeAdapter(baseHandler, sender, args)); + var subscription = new EventSubscription(handler, routes, handledEventsToo, InvokeAdapter); + + AddEventSubscription(routedEvent, subscription); + } + + /// + /// Adds a handler for the specified routed event. + /// + /// The routed event. + /// The handler. + /// The routing strategies to listen to. + /// Whether handled events should also be listened for. + public void AddHandler( + RoutedEvent routedEvent, + EventHandler? handler, + RoutingStrategies routes = RoutingStrategies.Direct | RoutingStrategies.Bubble, + bool handledEventsToo = false) + { + routedEvent = routedEvent ?? throw new ArgumentNullException(nameof(routedEvent)); + + if (handler is null) + return; + + static void InvokeAdapter(Delegate baseHandler, object sender, RoutedEventArgs args) + { + var typedHandler = (EventHandler)baseHandler; + var typedArgs = (CancelRoutedEventArgs)args; + + typedHandler(sender, typedArgs); + } + + var subscription = new EventSubscription(handler, routes, handledEventsToo, InvokeAdapter); AddEventSubscription(routedEvent, subscription); }