More bug fixes

This commit is contained in:
TheXamlGuy
2024-07-10 22:18:32 +01:00
parent d87fa37d09
commit 129efe8b2b
3 changed files with 106 additions and 6 deletions
+74 -4
View File
@@ -127,7 +127,7 @@ public partial class ObservableCollection<TItem> :
object? IList.this[int index] object? IList.this[int index]
{ {
get => index >= 0 ? collection[index] : null; get => index >= 0 && collection.Count > 0 ? collection[index] : null;
set set
{ {
TItem? item = default; TItem? item = default;
@@ -144,6 +144,59 @@ public partial class ObservableCollection<TItem> :
} }
} }
public void SetSource(IList<TItem> source)
{
foreach (TItem item in source)
{
Add(item);
}
if (source is INotifyCollectionChanged observableSource)
{
observableSource.CollectionChanged += SourceCollectionChanged;
}
}
private void SourceCollectionChanged(object? sender,
NotifyCollectionChangedEventArgs args)
{
switch (args.Action)
{
case NotifyCollectionChangedAction.Add:
if (args.NewItems is not null)
{
foreach (TItem newItem in args.NewItems)
{
Add(newItem);
}
}
break;
case NotifyCollectionChangedAction.Remove:
if (args.OldItems is not null)
{
foreach (TItem oldItem in args.OldItems)
{
if (this.FirstOrDefault(x => x.Equals(oldItem)) is TItem removedItem)
{
Remove(removedItem);
}
}
}
break;
case NotifyCollectionChangedAction.Reset:
Clear();
if (sender is IEnumerable<TItem> collection)
{
foreach (TItem item in collection)
{
Add(item);
}
}
break;
}
}
public virtual Task OnActivated() public virtual Task OnActivated()
{ {
IsActivated = true; IsActivated = true;
@@ -210,16 +263,33 @@ public partial class ObservableCollection<TItem> :
} }
} }
public void Clear(Action<ObservableCollection<TItem>> factory) public void Reset(Action<ObservableCollection<TItem>> factory, bool disposeItems = true)
{ {
Clear(); Clear(disposeItems);
factory.Invoke(this); factory.Invoke(this);
} }
public void Clear(bool disposeItems = false)
{
isClearing = true;
if (disposeItems)
{
foreach (TItem item in this.ToList())
{
Disposer.Dispose(item);
Disposer.Remove(this, item);
}
}
ClearItems();
isClearing = false;
}
public void Clear() public void Clear()
{ {
isClearing = true; isClearing = true;
foreach (TItem item in this.ToList()) foreach (TItem item in this.ToList())
{ {
Disposer.Dispose(item); Disposer.Dispose(item);
@@ -1,5 +1,4 @@
using Avalonia; using Avalonia;
using Avalonia.Controls;
using Avalonia.Threading; using Avalonia.Threading;
using Avalonia.Xaml.Interactivity; using Avalonia.Xaml.Interactivity;
using System.Collections; using System.Collections;
@@ -1,8 +1,39 @@
namespace Toolkit.UI.Controls.Avalonia; using FluentAvalonia.UI.Controls;
using System.Reflection;
namespace Toolkit.UI.Controls.Avalonia;
public class NavigationView : public class NavigationView :
FluentAvalonia.UI.Controls.NavigationView FluentAvalonia.UI.Controls.NavigationView
{ {
public NavigationView()
{
ItemInvoked += OnItemInvoked;
}
protected override Type StyleKeyOverride => protected override Type StyleKeyOverride =>
typeof(FluentAvalonia.UI.Controls.NavigationView); typeof(FluentAvalonia.UI.Controls.NavigationView);
private void ApplyNavigationFix()
{
if (typeof(FluentAvalonia.UI.Controls.NavigationView)
.GetField("_shouldRaiseItemInvokedAfterSelection",
BindingFlags.NonPublic | BindingFlags.Instance)
is FieldInfo shouldRaiseItemInvokedAfterSelectionFieldInfo)
{
if (shouldRaiseItemInvokedAfterSelectionFieldInfo.GetValue(this) is bool value)
{
if (value)
{
shouldRaiseItemInvokedAfterSelectionFieldInfo.SetValue(this, false);
}
}
};
}
private void OnItemInvoked(object? sender,
NavigationViewItemInvokedEventArgs args)
{
ApplyNavigationFix();
}
} }