diff --git a/Toolkit.Foundation/Subscriber.cs b/Toolkit.Foundation/Subscriber.cs index 593060e..88c772f 100644 --- a/Toolkit.Foundation/Subscriber.cs +++ b/Toolkit.Foundation/Subscriber.cs @@ -25,6 +25,7 @@ public class Subscriber(SubscriptionCollection subscriptions, Type requestType = handlerArguments[0]; Type responseType = handlerArguments[1]; Type wrapperType = typeof(HandlerWrapper<,>).MakeGenericType(requestType, responseType); + AddSubscriptions(subscriber, subscribers, wrapperType); } } @@ -49,12 +50,14 @@ public class Subscriber(SubscriptionCollection subscriptions, Type requestType = handlerArguments[0]; Type responseType = handlerArguments[1]; Type wrapperType = typeof(HandlerWrapper<,>).MakeGenericType(requestType, responseType); + RemoveSubscriptions(subscriber, subscribers, wrapperType); } } } - private void AddOrUpdateSubscription(object subscriber, string preferredKey) + private void AddOrUpdateSubscription(object subscriber, + string preferredKey) { subscriptions.AddOrUpdate(preferredKey, _ => new List { new(subscriber) }, (_, collection) => { @@ -62,10 +65,16 @@ public class Subscriber(SubscriptionCollection subscriptions, return collection; }); - disposer.Add(subscriber, Disposable.Create(() => RemoveSubscription(subscriber, preferredKey))); + disposer.Add(subscriber, Disposable.Create(() => { + + RemoveSubscription(subscriber, preferredKey); + + })); } - private void AddSubscriptions(object subscriber, IDictionary> subscribers, Type handlerType) + private void AddSubscriptions(object subscriber, + IDictionary> subscribers, + Type handlerType) { if (subscribers.TryGetValue(handlerType, out List? keys)) { @@ -135,7 +144,9 @@ public class Subscriber(SubscriptionCollection subscriptions, } } - private void RemoveSubscriptions(object subscriber, IDictionary> subscribers, Type handlerType) + private void RemoveSubscriptions(object subscriber, + IDictionary> subscribers, + Type handlerType) { if (subscribers.TryGetValue(handlerType, out List? keys)) { diff --git a/Toolkit.UI.Avalonia/AttachedEventTriggerBehaviour.cs b/Toolkit.UI.Avalonia/AttachedEventTriggerBehaviour.cs index d6c9119..25cb5fd 100644 --- a/Toolkit.UI.Avalonia/AttachedEventTriggerBehaviour.cs +++ b/Toolkit.UI.Avalonia/AttachedEventTriggerBehaviour.cs @@ -21,13 +21,26 @@ public class AttachedEventTriggerBehaviour : Trigger { if (AssociatedObject is Interactive interactive) { - interactive.AddHandler(RoutedEvent, (object sender, RoutedEventArgs args) => - { - Interaction.ExecuteActions(AssociatedObject, Actions, null); - }); + interactive.AddHandler(RoutedEvent, Handle); } } base.OnAttached(); } + + protected override void OnDetaching() + { + if (RoutedEvent is not null) + { + if (AssociatedObject is Interactive interactive) + { + interactive.RemoveHandler(RoutedEvent, Handle); + } + } + + base.OnDetaching(); + } + + private void Handle(object sender, RoutedEventArgs args) => + Interaction.ExecuteActions(AssociatedObject, Actions, null); } \ No newline at end of file diff --git a/Toolkit.UI.Avalonia/ListBoxExtension.cs b/Toolkit.UI.Avalonia/ListBoxExtension.cs index 250da31..ff84730 100644 --- a/Toolkit.UI.Avalonia/ListBoxExtension.cs +++ b/Toolkit.UI.Avalonia/ListBoxExtension.cs @@ -39,7 +39,20 @@ public class ListBoxExtension } } + if (sender.DataContext == listBox.SelectedItem) + { + sender.RaiseEvent(new ItemInvokedEventArgs { RoutedEvent = ItemInvokedEvent }); + } + + void HandleUnloaded(object? _, RoutedEventArgs __) + { + listBox.SelectionChanged -= OnItemInvoked; + listBox.Unloaded -= HandleUnloaded; + } + listBox.SelectionChanged += OnItemInvoked; + listBox.Unloaded += HandleUnloaded; + return true; } @@ -48,13 +61,12 @@ public class ListBoxExtension if (!TrySetupListBox()) { - void OnAttachedToVisualTree(object? _, VisualTreeAttachmentEventArgs __) + void HandleLoaded(object? _, RoutedEventArgs __) { - sender.AttachedToVisualTree -= OnAttachedToVisualTree; TrySetupListBox(); } - sender.AttachedToVisualTree += OnAttachedToVisualTree; + sender.Loaded += HandleLoaded; } }