/// <summary></summary> /// <typeparam name="TKEY"></typeparam> /// <typeparam name="T"></typeparam> /// <param name="resourceDictionary"></param> /// <param name="predicate"></param> /// <returns></returns> public static IEnumerable <T> FindResourceByKeyCore <TKEY, T>(ResourceDictionary resourceDictionary, Predicate <TKEY> predicate = null) where TKEY : ResourceKey { var dictionaryStack = new Stack <FindResourceStackItem>(); var current = new FindResourceStackItem(resourceDictionary); var returnedKeys = new List <TKEY>(); while (current != null) { // enumerate merged resources while (current.TryGetNextDictionary(out var stackItem)) { dictionaryStack.Push(current); current = stackItem; } // enumerate resource keys foreach (var key in current.Resources.Keys.OfType <TKEY>()) { if (!returnedKeys.Contains(key) && (predicate == null || predicate(key)) && current.Resources[key] is T v) { returnedKeys.Add(key); yield return(v); } } current = dictionaryStack.Count > 0 ? dictionaryStack.Pop() : null; } } // func FindResourceByKeyCore
} // ctor public bool TryGetNextDictionary(out FindResourceStackItem stackItem) { if (mergedIndex >= 0) { stackItem = new FindResourceStackItem(Resources.MergedDictionaries[mergedIndex--]); return(true); } else { stackItem = null; return(false); } } // func TryGetNextDictionary