| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262 | 
							- using System;
 
- using System.Collections;
 
- using System.Collections.Generic;
 
- using System.Linq;
 
- using Avalonia.Collections;
 
- namespace Avalonia.Controls
 
- {
 
-     /// <summary>
 
-     /// An indexed dictionary of resources.
 
-     /// </summary>
 
-     public class ResourceDictionary : IResourceDictionary
 
-     {
 
-         private Dictionary<object, object?>? _inner;
 
-         private IResourceHost? _owner;
 
-         private AvaloniaList<IResourceProvider>? _mergedDictionaries;
 
-         /// <summary>
 
-         /// Initializes a new instance of the <see cref="ResourceDictionary"/> class.
 
-         /// </summary>
 
-         public ResourceDictionary() { }
 
-         /// <summary>
 
-         /// Initializes a new instance of the <see cref="ResourceDictionary"/> class.
 
-         /// </summary>
 
-         public ResourceDictionary(IResourceHost owner) => Owner = owner;
 
-         public int Count => _inner?.Count ?? 0;
 
-         public object? this[object key]
 
-         {
 
-             get => _inner?[key];
 
-             set
 
-             {
 
-                 Inner[key] = value;
 
-                 Owner?.NotifyHostedResourcesChanged(ResourcesChangedEventArgs.Empty);
 
-             }
 
-         }
 
-         public ICollection<object> Keys => (ICollection<object>?)_inner?.Keys ?? Array.Empty<object>();
 
-         public ICollection<object?> Values => (ICollection<object?>?)_inner?.Values ?? Array.Empty<object?>();
 
-         public IResourceHost? Owner
 
-         {
 
-             get => _owner;
 
-             private set
 
-             {
 
-                 if (_owner != value)
 
-                 {
 
-                     _owner = value;
 
-                     OwnerChanged?.Invoke(this, EventArgs.Empty);
 
-                 }
 
-             }
 
-         }
 
-         public IList<IResourceProvider> MergedDictionaries
 
-         {
 
-             get
 
-             {
 
-                 if (_mergedDictionaries == null)
 
-                 {
 
-                     _mergedDictionaries = new AvaloniaList<IResourceProvider>();
 
-                     _mergedDictionaries.ResetBehavior = ResetBehavior.Remove;
 
-                     _mergedDictionaries.ForEachItem(
 
-                         x =>
 
-                         {
 
-                             if (Owner is object)
 
-                             {
 
-                                 x.AddOwner(Owner);
 
-                             }
 
-                         },
 
-                         x =>
 
-                         {
 
-                             if (Owner is object)
 
-                             {
 
-                                 x.RemoveOwner(Owner);
 
-                             }
 
-                         }, 
 
-                         () => throw new NotSupportedException("Dictionary reset not supported"));
 
-                 }
 
-                 return _mergedDictionaries;
 
-             }
 
-         }
 
-         bool IResourceNode.HasResources
 
-         {
 
-             get
 
-             {
 
-                 if (_inner?.Count > 0)
 
-                 {
 
-                     return true;
 
-                 }
 
-                 if (_mergedDictionaries?.Count > 0)
 
-                 {
 
-                     foreach (var i in _mergedDictionaries)
 
-                     {
 
-                         if (i.HasResources)
 
-                         {
 
-                             return true;
 
-                         }
 
-                     }
 
-                 }
 
-                 return false;
 
-             }
 
-         }
 
-         bool ICollection<KeyValuePair<object, object?>>.IsReadOnly => false;
 
-         private Dictionary<object, object?> Inner => _inner ??= new();
 
-         public event EventHandler? OwnerChanged;
 
-         public void Add(object key, object? value)
 
-         {
 
-             Inner.Add(key, value);
 
-             Owner?.NotifyHostedResourcesChanged(ResourcesChangedEventArgs.Empty);
 
-         }
 
-         public void Clear()
 
-         {
 
-             if (_inner?.Count > 0)
 
-             {
 
-                 _inner.Clear();
 
-                 Owner?.NotifyHostedResourcesChanged(ResourcesChangedEventArgs.Empty);
 
-             }
 
-         }
 
-         public bool ContainsKey(object key) => _inner?.ContainsKey(key) ?? false;
 
-         public bool Remove(object key)
 
-         {
 
-             if (_inner?.Remove(key) == true)
 
-             {
 
-                 Owner?.NotifyHostedResourcesChanged(ResourcesChangedEventArgs.Empty);
 
-                 return true;
 
-             }
 
-             return false;
 
-         }
 
-         public bool TryGetResource(object key, out object? value)
 
-         {
 
-             if (_inner is not null && _inner.TryGetValue(key, out value))
 
-             {
 
-                 return true;
 
-             }
 
-             if (_mergedDictionaries != null)
 
-             {
 
-                 for (var i = _mergedDictionaries.Count - 1; i >= 0; --i)
 
-                 {
 
-                     if (_mergedDictionaries[i].TryGetResource(key, out value))
 
-                     {
 
-                         return true;
 
-                     }
 
-                 }
 
-             }
 
-             value = null;
 
-             return false;
 
-         }
 
-         public bool TryGetValue(object key, out object? value)
 
-         {
 
-             if (_inner is not null)
 
-                 return _inner.TryGetValue(key, out value);
 
-             value = null;
 
-             return false;
 
-         }
 
-         void ICollection<KeyValuePair<object, object?>>.Add(KeyValuePair<object, object?> item)
 
-         {
 
-             Add(item.Key, item.Value);
 
-         }
 
-         bool ICollection<KeyValuePair<object, object?>>.Contains(KeyValuePair<object, object?> item)
 
-         {
 
-             return (_inner as ICollection<KeyValuePair<object, object?>>)?.Contains(item) ?? false;
 
-         }
 
-         void ICollection<KeyValuePair<object, object?>>.CopyTo(KeyValuePair<object, object?>[] array, int arrayIndex)
 
-         {
 
-             (_inner as ICollection<KeyValuePair<object, object?>>)?.CopyTo(array, arrayIndex);
 
-         }
 
-         bool ICollection<KeyValuePair<object, object?>>.Remove(KeyValuePair<object, object?> item)
 
-         {
 
-             if ((_inner as ICollection<KeyValuePair<object, object?>>)?.Remove(item) == true)
 
-             {
 
-                 Owner?.NotifyHostedResourcesChanged(ResourcesChangedEventArgs.Empty);
 
-                 return true;
 
-             }
 
-             return false;
 
-         }
 
-         public IEnumerator<KeyValuePair<object, object?>> GetEnumerator()
 
-         {
 
-             return _inner?.GetEnumerator() ?? Enumerable.Empty<KeyValuePair<object, object?>>().GetEnumerator();
 
-         }
 
-         IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
 
-         void IResourceProvider.AddOwner(IResourceHost owner)
 
-         {
 
-             owner = owner ?? throw new ArgumentNullException(nameof(owner));
 
-             if (Owner != null)
 
-             {
 
-                 throw new InvalidOperationException("The ResourceDictionary already has a parent.");
 
-             }
 
-             
 
-             Owner = owner;
 
-             var hasResources = _inner?.Count > 0;
 
-             
 
-             if (_mergedDictionaries is object)
 
-             {
 
-                 foreach (var i in _mergedDictionaries)
 
-                 {
 
-                     i.AddOwner(owner);
 
-                     hasResources |= i.HasResources;
 
-                 }
 
-             }
 
-             if (hasResources)
 
-             {
 
-                 owner.NotifyHostedResourcesChanged(ResourcesChangedEventArgs.Empty);
 
-             }
 
-         }
 
-         void IResourceProvider.RemoveOwner(IResourceHost owner)
 
-         {
 
-             owner = owner ?? throw new ArgumentNullException(nameof(owner));
 
-             if (Owner == owner)
 
-             {
 
-                 Owner = null;
 
-                 var hasResources = _inner?.Count > 0;
 
-                 if (_mergedDictionaries is object)
 
-                 {
 
-                     foreach (var i in _mergedDictionaries)
 
-                     {
 
-                         i.RemoveOwner(owner);
 
-                         hasResources |= i.HasResources;
 
-                     }
 
-                 }
 
-                 if (hasResources)
 
-                 {
 
-                     owner.NotifyHostedResourcesChanged(ResourcesChangedEventArgs.Empty);
 
-                 }
 
-             }
 
-         }
 
-     }
 
- }
 
 
  |