more issuesa

This commit is contained in:
TheXamlGuy
2024-07-23 21:13:16 +01:00
parent 82e5982632
commit 2c3de93faa
3 changed files with 47 additions and 11 deletions
+15 -4
View File
@@ -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<WeakReference> { 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<Type, List<object>> subscribers, Type handlerType)
private void AddSubscriptions(object subscriber,
IDictionary<Type, List<object>> subscribers,
Type handlerType)
{
if (subscribers.TryGetValue(handlerType, out List<object>? keys))
{
@@ -135,7 +144,9 @@ public class Subscriber(SubscriptionCollection subscriptions,
}
}
private void RemoveSubscriptions(object subscriber, IDictionary<Type, List<object>> subscribers, Type handlerType)
private void RemoveSubscriptions(object subscriber,
IDictionary<Type, List<object>> subscribers,
Type handlerType)
{
if (subscribers.TryGetValue(handlerType, out List<object>? keys))
{
@@ -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);
}
+15 -3
View File
@@ -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;
}
}