From cfb90c8201378dab7afc8d75316ea9c069c2c79c Mon Sep 17 00:00:00 2001 From: robloo Date: Mon, 16 Jan 2023 08:18:47 -0500 Subject: [PATCH] Add Interactive.AddHandler() for CancelRoutedEventArgs This hopefully fixes unit tests due to wrong overload being selected by the compiler. --- .../Interactivity/Interactive.cs | 34 +++++++++++++++++-- 1 file changed, 32 insertions(+), 2 deletions(-) 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); }