diff --git a/Needlework.Net/Needlework.Net.csproj b/Needlework.Net/Needlework.Net.csproj index 0da5007..7cde83f 100644 --- a/Needlework.Net/Needlework.Net.csproj +++ b/Needlework.Net/Needlework.Net.csproj @@ -27,7 +27,6 @@ - diff --git a/Needlework.Net/ViewLocator.cs b/Needlework.Net/ViewLocator.cs index 8160fac..4ec4716 100644 --- a/Needlework.Net/ViewLocator.cs +++ b/Needlework.Net/ViewLocator.cs @@ -1,60 +1,15 @@ using Avalonia.Controls; using Avalonia.Controls.Templates; -using Avalonia.VisualTree; -using BitFaster.Caching; -using BitFaster.Caching.Lru; using System; using System.Collections.Generic; using System.ComponentModel; -using System.Diagnostics.CodeAnalysis; namespace Needlework.Net { public class ViewLocator : IDataTemplate { - private class ObjectComparer : IEqualityComparer - { - public new bool Equals(object? x, object? y) - { - if (ReferenceEquals(x, y)) - return true; - - if (x == null || y == null) - return false; - - return x.Equals(y); - } - - public int GetHashCode([DisallowNull] object obj) - { - return obj.GetHashCode(); - } - } - - private readonly ICache _controlCache = new ConcurrentLruBuilder() - .WithExpireAfterAccess(TimeSpan.FromMinutes(5)) - .WithKeyComparer(new ObjectComparer()) - .WithCapacity(1024) - .WithMetrics() - .Build(); - private readonly Dictionary> _viewRegister = []; - public ViewLocator() - { - _controlCache.Events.Value!.ItemRemoved += (source, args) => - { - var descendants = args.Value!.GetVisualDescendants(); - foreach (var descendant in descendants) - { - if (descendant.DataContext is INotifyPropertyChanged key) - { - _controlCache.TryRemove(key, out _); - } - } - }; - } - public void Register(Func viewActivator) where T : INotifyPropertyChanged { @@ -68,13 +23,7 @@ namespace Needlework.Net throw new Exception("Data type has no registered view activator."); } - bool isCold = !_controlCache.TryGet(data!, out var res); - if (isCold) - { - res ??= activator(); - _controlCache.AddOrUpdate(data!, res); - } - + var res = activator(); res!.DataContext = data; return res; }