Fix more edge cases
This commit is contained in:
@@ -1,7 +1,3 @@
|
|||||||
namespace Toolkit.Foundation;
|
namespace Toolkit.Foundation;
|
||||||
|
|
||||||
public class AggerateAttribute(Type type, object key,
|
public class AggerateAttribute(Type type, object key) : NotificationAttribute(type, key);
|
||||||
AggerateMode mode = AggerateMode.Reset) : NotificationAttribute(type, key)
|
|
||||||
{
|
|
||||||
public AggerateMode Mode => mode;
|
|
||||||
}
|
|
||||||
@@ -1,31 +0,0 @@
|
|||||||
namespace Toolkit.Foundation;
|
|
||||||
|
|
||||||
public record Aggerate
|
|
||||||
{
|
|
||||||
public static AggerateEventArgs<TValue, TOptions> With<TValue, TOptions>(TOptions options) where TOptions : class
|
|
||||||
{
|
|
||||||
return new AggerateEventArgs<TValue, TOptions>(options);
|
|
||||||
}
|
|
||||||
|
|
||||||
public static AggerateEventArgs<TValue> With<TValue>()
|
|
||||||
{
|
|
||||||
return new AggerateEventArgs<TValue>();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public record AggerateEventArgs<TValue, TOptions>(TOptions? Options = null) :
|
|
||||||
IAggerate
|
|
||||||
where TOptions : class
|
|
||||||
{
|
|
||||||
public object? Key { get; init; }
|
|
||||||
|
|
||||||
public AggerateMode Mode { get; init; }
|
|
||||||
}
|
|
||||||
|
|
||||||
public record AggerateEventArgs<TValue> :
|
|
||||||
IAggerate
|
|
||||||
{
|
|
||||||
public object? Key { get; init; }
|
|
||||||
|
|
||||||
public AggerateMode Mode { get; init; }
|
|
||||||
}
|
|
||||||
@@ -1,7 +0,0 @@
|
|||||||
namespace Toolkit.Foundation;
|
|
||||||
|
|
||||||
public enum AggerateMode
|
|
||||||
{
|
|
||||||
Append,
|
|
||||||
Reset
|
|
||||||
}
|
|
||||||
@@ -0,0 +1,10 @@
|
|||||||
|
namespace Toolkit.Foundation;
|
||||||
|
|
||||||
|
public record Aggregate
|
||||||
|
{
|
||||||
|
public static AggregateEventArgs<TValue, TOptions> As<TValue, TOptions>(TOptions options)
|
||||||
|
where TOptions : class => new(options);
|
||||||
|
|
||||||
|
public static AggerateEventArgs<TValue> As<TValue>() =>
|
||||||
|
new AggerateEventArgs<TValue>();
|
||||||
|
}
|
||||||
@@ -0,0 +1,8 @@
|
|||||||
|
namespace Toolkit.Foundation;
|
||||||
|
|
||||||
|
public record AggregateEventArgs<TValue, TOptions>(TOptions? Options = null) :
|
||||||
|
IAggregate
|
||||||
|
where TOptions : class;
|
||||||
|
|
||||||
|
public record AggerateEventArgs<TValue> :
|
||||||
|
IAggregate;
|
||||||
@@ -0,0 +1,3 @@
|
|||||||
|
namespace Toolkit.Foundation;
|
||||||
|
|
||||||
|
public record AggregateExpression(IAggregate Value, object? Key = null);
|
||||||
@@ -6,6 +6,7 @@ public class HandlerProvider(SubscriptionCollection subscriptions) :
|
|||||||
public IEnumerable<object?> Get(Type type,
|
public IEnumerable<object?> Get(Type type,
|
||||||
object? key = null)
|
object? key = null)
|
||||||
{
|
{
|
||||||
|
var d = subscriptions;
|
||||||
string subscriptionKey = $"{(key is not null ? $"{key}:" : "")}{type}";
|
string subscriptionKey = $"{(key is not null ? $"{key}:" : "")}{type}";
|
||||||
if (subscriptions.TryGetValue(subscriptionKey, out List<WeakReference>? subscribers))
|
if (subscriptions.TryGetValue(subscriptionKey, out List<WeakReference>? subscribers))
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -1,6 +0,0 @@
|
|||||||
namespace Toolkit.Foundation;
|
|
||||||
|
|
||||||
public interface IAggerate
|
|
||||||
{
|
|
||||||
object? Key { get; init; }
|
|
||||||
}
|
|
||||||
@@ -0,0 +1,3 @@
|
|||||||
|
namespace Toolkit.Foundation;
|
||||||
|
|
||||||
|
public interface IAggregate;
|
||||||
@@ -2,14 +2,14 @@
|
|||||||
|
|
||||||
public interface IPublisher
|
public interface IPublisher
|
||||||
{
|
{
|
||||||
void Publish<TMessage>(object key)
|
void Publish<TMessage>(object? key = null)
|
||||||
where TMessage : new();
|
where TMessage : new();
|
||||||
|
|
||||||
void Publish<TMessage>(TMessage message)
|
void Publish<TMessage>(TMessage message)
|
||||||
where TMessage : notnull;
|
where TMessage : notnull;
|
||||||
|
|
||||||
void Publish<TMessage>(TMessage message,
|
void Publish<TMessage>(TMessage message,
|
||||||
object key)
|
object? key = null)
|
||||||
where TMessage : notnull;
|
where TMessage : notnull;
|
||||||
|
|
||||||
void Publish(object message,
|
void Publish(object message,
|
||||||
@@ -22,9 +22,9 @@ public interface IPublisher
|
|||||||
void Publish(object message);
|
void Publish(object message);
|
||||||
|
|
||||||
void PublishUI<TMessage>(TMessage message,
|
void PublishUI<TMessage>(TMessage message,
|
||||||
object key) where TMessage : notnull;
|
object? key = null) where TMessage : notnull;
|
||||||
|
|
||||||
void PublishUI<TMessage>(object key)
|
void PublishUI<TMessage>(object? key = null)
|
||||||
where TMessage : new();
|
where TMessage : new();
|
||||||
|
|
||||||
void PublishUI<TMessage>(TMessage message)
|
void PublishUI<TMessage>(TMessage message)
|
||||||
|
|||||||
@@ -192,20 +192,20 @@ public partial class ObservableCollection<TItem> :
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void BeginAggregation()
|
public void Fetch(bool reset = false)
|
||||||
{
|
{
|
||||||
if (this.GetAttribute<AggerateAttribute>() is AggerateAttribute attribute)
|
if (reset)
|
||||||
{
|
|
||||||
if (attribute.Mode == AggerateMode.Reset)
|
|
||||||
{
|
{
|
||||||
Clear();
|
Clear();
|
||||||
}
|
}
|
||||||
|
|
||||||
object? key = this.GetPropertyValue(() => attribute.Key) is { } value ? value : attribute.Key;
|
AggregateExpression expression = CreateAggregateExpression();
|
||||||
Publisher.PublishUI(OnAggerate(key));
|
Publisher.PublishUI(expression.Value, expression.Key);
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
protected virtual IAggregate OnAggerate() =>
|
||||||
|
new AggerateEventArgs<TItem>();
|
||||||
|
|
||||||
public void Clear()
|
public void Clear()
|
||||||
{
|
{
|
||||||
clearing = true;
|
clearing = true;
|
||||||
@@ -389,7 +389,7 @@ public partial class ObservableCollection<TItem> :
|
|||||||
}
|
}
|
||||||
|
|
||||||
Initialized = true;
|
Initialized = true;
|
||||||
BeginAggregation();
|
Fetch();
|
||||||
|
|
||||||
return Task.CompletedTask;
|
return Task.CompletedTask;
|
||||||
}
|
}
|
||||||
@@ -565,8 +565,11 @@ public partial class ObservableCollection<TItem> :
|
|||||||
collection.Insert(index > Count ? Count : index, item);
|
collection.Insert(index > Count ? Count : index, item);
|
||||||
}
|
}
|
||||||
|
|
||||||
protected virtual IAggerate OnAggerate(object? key) =>
|
protected virtual AggregateExpression CreateAggregateExpression() =>
|
||||||
new AggerateEventArgs<TItem>() with { Key = key };
|
new AggregateExpression(new AggerateEventArgs<TItem>());
|
||||||
|
|
||||||
|
protected virtual object? CreateAggregationKey() =>
|
||||||
|
default;
|
||||||
|
|
||||||
protected virtual void RemoveItem(int index) =>
|
protected virtual void RemoveItem(int index) =>
|
||||||
collection.RemoveAt(index);
|
collection.RemoveAt(index);
|
||||||
|
|||||||
@@ -9,7 +9,7 @@ public class Publisher(IHandlerProvider handlerProvider,
|
|||||||
IDispatcher dispatcher) :
|
IDispatcher dispatcher) :
|
||||||
IPublisher
|
IPublisher
|
||||||
{
|
{
|
||||||
public void Publish<TMessage>(object key)
|
public void Publish<TMessage>(object? key = null)
|
||||||
where TMessage : new() =>
|
where TMessage : new() =>
|
||||||
Publish(serviceFactory.Create<TMessage>() ?? new TMessage(), async args => await args(), key);
|
Publish(serviceFactory.Create<TMessage>() ?? new TMessage(), async args => await args(), key);
|
||||||
|
|
||||||
@@ -17,7 +17,8 @@ public class Publisher(IHandlerProvider handlerProvider,
|
|||||||
where TMessage : notnull =>
|
where TMessage : notnull =>
|
||||||
Publish(message, async args => await args(), null);
|
Publish(message, async args => await args(), null);
|
||||||
|
|
||||||
public void Publish<TMessage>(TMessage message, object key)
|
public void Publish<TMessage>(TMessage message,
|
||||||
|
object? key = null)
|
||||||
where TMessage : notnull =>
|
where TMessage : notnull =>
|
||||||
Publish(message, async args => await args(), key);
|
Publish(message, async args => await args(), key);
|
||||||
|
|
||||||
@@ -60,7 +61,7 @@ public class Publisher(IHandlerProvider handlerProvider,
|
|||||||
where TMessage : new() =>
|
where TMessage : new() =>
|
||||||
Publish(new TMessage(), async args => await args(), null);
|
Publish(new TMessage(), async args => await args(), null);
|
||||||
|
|
||||||
public void PublishUI<TMessage>(object key)
|
public void PublishUI<TMessage>(object? key = null)
|
||||||
where TMessage : new() =>
|
where TMessage : new() =>
|
||||||
Publish(new TMessage(), args => dispatcher.Invoke(async () => await args()), key);
|
Publish(new TMessage(), args => dispatcher.Invoke(async () => await args()), key);
|
||||||
|
|
||||||
@@ -69,7 +70,7 @@ public class Publisher(IHandlerProvider handlerProvider,
|
|||||||
Publish(message, args => dispatcher.Invoke(async () => await args()), null);
|
Publish(message, args => dispatcher.Invoke(async () => await args()), null);
|
||||||
|
|
||||||
public void PublishUI<TMessage>(TMessage message,
|
public void PublishUI<TMessage>(TMessage message,
|
||||||
object key)
|
object? key = null)
|
||||||
where TMessage : notnull =>
|
where TMessage : notnull =>
|
||||||
Publish(message, args => dispatcher.Invoke(async () => await args()), key);
|
Publish(message, args => dispatcher.Invoke(async () => await args()), key);
|
||||||
|
|
||||||
|
|||||||
@@ -10,13 +10,16 @@ public class Subscription(SubscriptionCollection subscriptions,
|
|||||||
{
|
{
|
||||||
Type handlerType = subscriber.GetType();
|
Type handlerType = subscriber.GetType();
|
||||||
|
|
||||||
IDictionary<Type, object> keys = GetKeysFromHandler(subscriber);
|
IDictionary<Type, List<object>> subscribers = GetSubscriptionKeys(subscriber);
|
||||||
foreach (Type interfaceType in GetHandlerInterfaces(handlerType))
|
foreach (Type interfaceType in GetHandlerInterfaces(handlerType))
|
||||||
{
|
{
|
||||||
if (interfaceType.GetGenericArguments().FirstOrDefault() is Type argumentType)
|
if (interfaceType.GetGenericArguments().FirstOrDefault() is Type argumentType)
|
||||||
{
|
{
|
||||||
keys.TryGetValue(argumentType, out object? key);
|
subscribers.TryGetValue(argumentType, out List<object>? keys);
|
||||||
|
if (keys is not null)
|
||||||
|
{
|
||||||
|
foreach (object key in keys)
|
||||||
|
{
|
||||||
string subscriptionKey = $"{(key is not null ? $"{key}:" : "")}{argumentType}";
|
string subscriptionKey = $"{(key is not null ? $"{key}:" : "")}{argumentType}";
|
||||||
subscriptions.AddOrUpdate(subscriptionKey, _ => new List<WeakReference> { new(subscriber) }, (_, collection) =>
|
subscriptions.AddOrUpdate(subscriptionKey, _ => new List<WeakReference> { new(subscriber) }, (_, collection) =>
|
||||||
{
|
{
|
||||||
@@ -27,26 +30,58 @@ public class Subscription(SubscriptionCollection subscriptions,
|
|||||||
disposer.Add(subscriber, Disposable.Create(() => RemoveSubscriber(subscriber, subscriptionKey)));
|
disposer.Add(subscriber, Disposable.Create(() => RemoveSubscriber(subscriber, subscriptionKey)));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
string subscriptionKey = $"{argumentType}";
|
||||||
|
subscriptions.AddOrUpdate(subscriptionKey, _ => new List<WeakReference> { new(subscriber) }, (_, collection) =>
|
||||||
|
{
|
||||||
|
collection.Add(new WeakReference(subscriber));
|
||||||
|
return collection;
|
||||||
|
});
|
||||||
|
|
||||||
|
disposer.Add(subscriber, Disposable.Create(() => RemoveSubscriber(subscriber, subscriptionKey)));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Remove(object subscriber)
|
public void Remove(object subscriber)
|
||||||
{
|
{
|
||||||
Type handlerType = subscriber.GetType();
|
Type handlerType = subscriber.GetType();
|
||||||
IDictionary<Type, object> keys = GetKeysFromHandler(subscriber);
|
IDictionary<Type, List<object>> subscribers = GetSubscriptionKeys(subscriber);
|
||||||
foreach (Type interfaceType in GetHandlerInterfaces(handlerType))
|
foreach (Type interfaceType in GetHandlerInterfaces(handlerType))
|
||||||
{
|
{
|
||||||
if (interfaceType.GetGenericArguments().FirstOrDefault() is Type argumentType)
|
if (interfaceType.GetGenericArguments().FirstOrDefault() is Type argumentType)
|
||||||
{
|
{
|
||||||
keys.TryGetValue(argumentType, out object? key);
|
subscribers.TryGetValue(argumentType, out List<object>? keys);
|
||||||
|
if (keys is not null)
|
||||||
|
{
|
||||||
|
foreach (object key in keys)
|
||||||
|
{
|
||||||
string subscriptionKey = $"{(key is not null ? $"{key}:" : "")}{argumentType}";
|
string subscriptionKey = $"{(key is not null ? $"{key}:" : "")}{argumentType}";
|
||||||
if (subscriptions.TryGetValue(subscriptionKey, out List<WeakReference>? subscribers))
|
if (subscriptions.TryGetValue(subscriptionKey, out List<WeakReference>? existing))
|
||||||
{
|
{
|
||||||
for (int i = subscribers.Count - 1; i >= 0; i--)
|
for (int i = existing.Count - 1; i >= 0; i--)
|
||||||
{
|
{
|
||||||
if (!subscribers[i].IsAlive || subscribers[i].Target == subscriber)
|
if (!existing[i].IsAlive || existing[i].Target == subscriber)
|
||||||
{
|
{
|
||||||
subscribers.RemoveAt(i);
|
existing.RemoveAt(i);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
string subscriptionKey = $"{argumentType}";
|
||||||
|
if (subscriptions.TryGetValue(subscriptionKey, out List<WeakReference>? existing))
|
||||||
|
{
|
||||||
|
for (int i = existing.Count - 1; i >= 0; i--)
|
||||||
|
{
|
||||||
|
if (!existing[i].IsAlive || existing[i].Target == subscriber)
|
||||||
|
{
|
||||||
|
existing.RemoveAt(i);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -74,23 +109,23 @@ public class Subscription(SubscriptionCollection subscriptions,
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
//private object? GetKeyFromHandler(object handler) =>
|
private IDictionary<Type, List<object>> GetSubscriptionKeys(object subscriber)
|
||||||
// handler.GetAttribute<NotificationAttribute>() is NotificationAttribute attribute
|
|
||||||
// ? handler.GetPropertyValue(() => attribute.Key) is { } value ? value : attribute.Key : null;
|
|
||||||
|
|
||||||
private IDictionary<Type, object> GetKeysFromHandler(object handler)
|
|
||||||
{
|
{
|
||||||
Dictionary<Type, object> keys = [];
|
Dictionary<Type, List<object>> keys = [];
|
||||||
|
foreach (NotificationAttribute attribute in subscriber.GetAttributes<NotificationAttribute>())
|
||||||
foreach (NotificationAttribute attribute in handler.GetAttributes<NotificationAttribute>())
|
|
||||||
{
|
{
|
||||||
keys.Add(attribute.Type, attribute.Key);
|
if (!keys.TryGetValue(attribute.Type, out List<object>? value))
|
||||||
|
{
|
||||||
|
value = ([]);
|
||||||
|
keys[attribute.Type] = value;
|
||||||
|
}
|
||||||
|
|
||||||
|
value.Add(attribute.Key);
|
||||||
}
|
}
|
||||||
|
|
||||||
return keys;
|
return keys;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
private IEnumerable<Type> GetHandlerInterfaces(Type handlerType) =>
|
private IEnumerable<Type> GetHandlerInterfaces(Type handlerType) =>
|
||||||
handlerType.GetInterfaces().Where(interfaceType =>
|
handlerType.GetInterfaces().Where(interfaceType =>
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -2,7 +2,8 @@
|
|||||||
|
|
||||||
namespace Toolkit.UI.Avalonia;
|
namespace Toolkit.UI.Avalonia;
|
||||||
|
|
||||||
public class AttachedBehaviour : Trigger
|
public class AttachedBehaviour :
|
||||||
|
Trigger
|
||||||
{
|
{
|
||||||
protected override void OnAttachedToVisualTree()
|
protected override void OnAttachedToVisualTree()
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -0,0 +1,77 @@
|
|||||||
|
using Avalonia;
|
||||||
|
using Avalonia.Controls;
|
||||||
|
using Avalonia.Interactivity;
|
||||||
|
using Avalonia.LogicalTree;
|
||||||
|
using Toolkit.UI.Controls.Avalonia;
|
||||||
|
|
||||||
|
namespace Toolkit.UI.Avalonia;
|
||||||
|
|
||||||
|
public class ListBoxExtension
|
||||||
|
{
|
||||||
|
public static readonly AttachedProperty<bool> IsItemInvokedEnabledProperty =
|
||||||
|
AvaloniaProperty.RegisterAttached<NavigationViewItem, bool>("IsItemInvokedEnabled",
|
||||||
|
typeof(ListBoxExtension), false);
|
||||||
|
|
||||||
|
public static readonly RoutedEvent<ItemInvokedEventArgs> ItemInvokedEvent =
|
||||||
|
RoutedEvent.Register<ItemInvokedEventArgs>("ItemInvoked",
|
||||||
|
RoutingStrategies.Bubble, typeof(ListBoxExtension));
|
||||||
|
|
||||||
|
static ListBoxExtension()
|
||||||
|
{
|
||||||
|
IsItemInvokedEnabledProperty.Changed.AddClassHandler<ListBoxItem>(OnIsItemClickEnabledPropertyChanged);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void OnIsItemClickEnabledPropertyChanged(ListBoxItem sender,
|
||||||
|
AvaloniaPropertyChangedEventArgs args)
|
||||||
|
{
|
||||||
|
bool TrySetupListBox()
|
||||||
|
{
|
||||||
|
if (sender.GetLogicalAncestors().OfType<ListBox>().FirstOrDefault() is ListBox listBox)
|
||||||
|
{
|
||||||
|
void OnItemInvoked(object? _, SelectionChangedEventArgs args)
|
||||||
|
{
|
||||||
|
if (args.AddedItems is { Count: > 0 })
|
||||||
|
{
|
||||||
|
if (sender.DataContext == listBox.SelectedItem)
|
||||||
|
{
|
||||||
|
sender.RaiseEvent(new ItemInvokedEventArgs { RoutedEvent = ItemInvokedEvent });
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (sender.DataContext == listBox.SelectedItem)
|
||||||
|
{
|
||||||
|
sender.RaiseEvent(new ItemInvokedEventArgs { RoutedEvent = ItemInvokedEvent });
|
||||||
|
}
|
||||||
|
|
||||||
|
listBox.SelectionChanged += OnItemInvoked;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!TrySetupListBox())
|
||||||
|
{
|
||||||
|
void OnAttachedToVisualTree(object? _, VisualTreeAttachmentEventArgs __)
|
||||||
|
{
|
||||||
|
sender.AttachedToVisualTree -= OnAttachedToVisualTree;
|
||||||
|
TrySetupListBox();
|
||||||
|
}
|
||||||
|
|
||||||
|
sender.AttachedToVisualTree += OnAttachedToVisualTree;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static bool GetIsItemInvokedEnabled(ListBoxItem element) =>
|
||||||
|
element.GetValue(IsItemInvokedEnabledProperty);
|
||||||
|
|
||||||
|
public static void SetIsItemInvokedEnabled(ListBoxItem element, bool value) =>
|
||||||
|
element.SetValue(IsItemInvokedEnabledProperty, value);
|
||||||
|
|
||||||
|
public static void AddItemInvokedHandler(ListBoxItem element, EventHandler<ItemInvokedEventArgs> handler) =>
|
||||||
|
element.AddHandler(ItemInvokedEvent, handler);
|
||||||
|
|
||||||
|
public static void RemoveItemInvokedHandler(ListBoxItem element, EventHandler<ItemInvokedEventArgs> handler) =>
|
||||||
|
element.RemoveHandler(ItemInvokedEvent, handler);
|
||||||
|
}
|
||||||
@@ -5,22 +5,22 @@ using Toolkit.UI.Controls.Avalonia;
|
|||||||
|
|
||||||
namespace Toolkit.UI.Avalonia;
|
namespace Toolkit.UI.Avalonia;
|
||||||
|
|
||||||
public class NavigationViewItemExtension
|
public class NavigationViewExtension
|
||||||
{
|
{
|
||||||
public static readonly AttachedProperty<bool> IsItemClickEnabledProperty =
|
public static readonly AttachedProperty<bool> IsItemInvokedEnabledProperty =
|
||||||
AvaloniaProperty.RegisterAttached<NavigationViewItem, bool>("IsItemClickEnabled",
|
AvaloniaProperty.RegisterAttached<NavigationViewItem, bool>("IsItemInvokedEnabled",
|
||||||
typeof(NavigationViewItemExtension), false);
|
typeof(NavigationViewExtension), false);
|
||||||
|
|
||||||
public static readonly RoutedEvent<ItemInvokedEventArgs> ItemClickEvent =
|
public static readonly RoutedEvent<ItemInvokedEventArgs> ItemInvokedEvent =
|
||||||
RoutedEvent.Register<ItemInvokedEventArgs>("ItemClick",
|
RoutedEvent.Register<ItemInvokedEventArgs>("ItemInvoked",
|
||||||
RoutingStrategies.Bubble, typeof(NavigationViewItemExtension));
|
RoutingStrategies.Bubble, typeof(NavigationViewExtension));
|
||||||
|
|
||||||
static NavigationViewItemExtension()
|
static NavigationViewExtension()
|
||||||
{
|
{
|
||||||
IsItemClickEnabledProperty.Changed.AddClassHandler<NavigationViewItem>(OnIsItemClickEnabledPropertyChanged);
|
IsItemInvokedEnabledProperty.Changed.AddClassHandler<NavigationViewItem>(OnIsItemInvokedEnabledPropertyChanged);
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void OnIsItemClickEnabledPropertyChanged(NavigationViewItem sender,
|
private static void OnIsItemInvokedEnabledPropertyChanged(NavigationViewItem sender,
|
||||||
AvaloniaPropertyChangedEventArgs args)
|
AvaloniaPropertyChangedEventArgs args)
|
||||||
{
|
{
|
||||||
bool TrySetupNavigationView()
|
bool TrySetupNavigationView()
|
||||||
@@ -31,7 +31,7 @@ public class NavigationViewItemExtension
|
|||||||
{
|
{
|
||||||
if (args.InvokedItemContainer == sender)
|
if (args.InvokedItemContainer == sender)
|
||||||
{
|
{
|
||||||
sender.RaiseEvent(new ItemInvokedEventArgs { RoutedEvent = ItemClickEvent });
|
sender.RaiseEvent(new ItemInvokedEventArgs { RoutedEvent = ItemInvokedEvent });
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -54,15 +54,15 @@ public class NavigationViewItemExtension
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static bool GetIsItemClickEnabled(NavigationViewItem element) =>
|
public static bool GetIsItemInvokedEnabled(NavigationViewItem element) =>
|
||||||
element.GetValue(IsItemClickEnabledProperty);
|
element.GetValue(IsItemInvokedEnabledProperty);
|
||||||
|
|
||||||
public static void SetIsItemClickEnabled(NavigationViewItem element, bool value) =>
|
public static void SetIsItemInvokedEnabled(NavigationViewItem element, bool value) =>
|
||||||
element.SetValue(IsItemClickEnabledProperty, value);
|
element.SetValue(IsItemInvokedEnabledProperty, value);
|
||||||
|
|
||||||
public static void AddItemClickHandler(NavigationViewItem element, EventHandler<ItemInvokedEventArgs> handler) =>
|
public static void AddItemInvokedHandler(NavigationViewItem element, EventHandler<ItemInvokedEventArgs> handler) =>
|
||||||
element.AddHandler(ItemClickEvent, handler);
|
element.AddHandler(ItemInvokedEvent, handler);
|
||||||
|
|
||||||
public static void RemoveItemClickHandler(NavigationViewItem element, EventHandler<ItemInvokedEventArgs> handler) =>
|
public static void RemoveItemInvokedHandler(NavigationViewItem element, EventHandler<ItemInvokedEventArgs> handler) =>
|
||||||
element.RemoveHandler(ItemClickEvent, handler);
|
element.RemoveHandler(ItemInvokedEvent, handler);
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user