diff --git a/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/PresentationSource.cs b/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/PresentationSource.cs
index ddd8abb434c..2c75428b282 100644
--- a/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/PresentationSource.cs
+++ b/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/PresentationSource.cs
@@ -449,7 +449,7 @@ protected void RootChanged(Visual oldRoot, Visual newRoot)
// To fire PresentationSourceChanged when the RootVisual changes;
// rather than simulate a "parent" pointer change, we just walk the
// collection of all nodes that need the event.
- foreach (DependencyObject element in _watchers)
+ foreach (DependencyObject element in s_watchers)
{
// We only need to update those elements that are in the
// same context as this presentation source.
@@ -475,7 +475,7 @@ protected void RootChanged(Visual oldRoot, Visual newRoot)
///
protected void AddSource()
{
- _sources.Add(this);
+ s_sources.Add(this);
}
///
@@ -483,7 +483,7 @@ protected void AddSource()
///
protected void RemoveSource()
{
- _sources.Remove(this);
+ s_sources.Remove(this);
}
///
@@ -613,17 +613,14 @@ internal static bool IsUnderSamePresentationSource(params ReadOnlySpan
- internal static WeakReferenceList CriticalCurrentSources
+ internal static WeakReferenceList CriticalCurrentSources
{
- get
- {
- return _sources;
- }
+ get => s_sources;
}
private static void AddElementToWatchList(DependencyObject element)
{
- if(_watchers.Add(element))
+ if (s_watchers.Add(element))
{
element.SetValue(CachedSourceProperty, PresentationSource.FindSource(element));
element.SetValue(GetsSourceChangedEventProperty, true);
@@ -633,7 +630,7 @@ private static void AddElementToWatchList(DependencyObject element)
private static void RemoveElementFromWatchList(DependencyObject element)
{
- if(_watchers.Remove(element))
+ if (s_watchers.Remove(element))
{
element.ClearValue(CachedSourceProperty);
element.ClearValue(GetsSourceChangedEventProperty);
@@ -741,11 +738,11 @@ private static readonly DependencyProperty CachedSourceProperty
private static readonly object _globalLock = new object();
// An array of weak-references to sources that we know about.
- private static WeakReferenceList _sources = new WeakReferenceList(_globalLock);
+ private static readonly WeakReferenceList s_sources = new(_globalLock);
// An array of weak-references to elements that need to know
// about source changes.
- private static WeakReferenceList _watchers = new WeakReferenceList(_globalLock);
+ private static readonly WeakReferenceList s_watchers = new(_globalLock);
#endregion
}
diff --git a/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Diagnostics/ResourceDictionaryDiagnostics.cs b/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Diagnostics/ResourceDictionaryDiagnostics.cs
index 6ea9c31af7d..66ab9491522 100644
--- a/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Diagnostics/ResourceDictionaryDiagnostics.cs
+++ b/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Diagnostics/ResourceDictionaryDiagnostics.cs
@@ -1,4 +1,4 @@
-// Licensed to the .NET Foundation under one or more agreements.
+// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
//
@@ -51,7 +51,7 @@ public static IEnumerable ThemedResourceDictionaries
{
if (!IsEnabled)
{
- return ResourceDictionaryDiagnostics.EmptyResourceDictionaryInfos;
+ return ReadOnlyCollection.Empty;
}
return SystemResources.ThemedResourceDictionaries;
@@ -69,7 +69,7 @@ public static IEnumerable GenericResourceDictionaries
{
if (!IsEnabled)
{
- return ResourceDictionaryDiagnostics.EmptyResourceDictionaryInfos;
+ return ReadOnlyCollection.Empty;
}
return SystemResources.GenericResourceDictionaries;
@@ -276,47 +276,38 @@ private static IReadOnlyCollection EmptyResourceDictionaries
public static IEnumerable GetFrameworkElementOwners(ResourceDictionary dictionary)
{
- return GetOwners(dictionary.FrameworkElementOwners, EmptyFrameworkElementList);
+ return GetOwners(dictionary.FrameworkElementOwners);
}
public static IEnumerable GetFrameworkContentElementOwners(ResourceDictionary dictionary)
{
- return GetOwners(dictionary.FrameworkContentElementOwners, EmptyFrameworkContentElementList);
+ return GetOwners(dictionary.FrameworkContentElementOwners);
}
public static IEnumerable GetApplicationOwners(ResourceDictionary dictionary)
{
- return GetOwners(dictionary.ApplicationOwners, EmptyApplicationList);
+ return GetOwners(dictionary.ApplicationOwners);
}
- private static IEnumerable GetOwners(WeakReferenceList list, IEnumerable emptyList)
+ private static IEnumerable GetOwners(WeakReferenceList list)
where T : DispatcherObject
{
if (!IsEnabled || list == null || list.Count == 0)
{
- return emptyList;
+ return Array.Empty();
}
- List result = new List(list.Count);
- foreach (Object o in list)
+ // Copy list manually as it doesn't implement ICollection
+ List owners = new List(list.Count);
+ foreach (T item in list)
{
- T owner = o as T;
- if (owner != null)
- {
- result.Add(owner);
- }
+ owners.Add(item);
}
- return result.AsReadOnly();
+ // Create a read-only copy of the list
+ return owners.AsReadOnly();
}
- private static IReadOnlyCollection EmptyFrameworkElementList
- => Array.Empty();
- private static IReadOnlyCollection EmptyFrameworkContentElementList
- => Array.Empty();
- private static IReadOnlyCollection EmptyApplicationList
- => Array.Empty();
-
#endregion
#region Notify when static resource references are resolved
@@ -548,7 +539,5 @@ internal class LookupResult
internal static bool IsEnabled { get; private set; }
- private static readonly ReadOnlyCollection EmptyResourceDictionaryInfos
- = new List().AsReadOnly();
}
}
diff --git a/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/ResourceDictionary.cs b/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/ResourceDictionary.cs
index 84e2c746bdf..7093caf50c5 100644
--- a/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/ResourceDictionary.cs
+++ b/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/ResourceDictionary.cs
@@ -1495,7 +1495,7 @@ internal void AddOwner(DispatcherObject owner)
{
if (_ownerFEs == null)
{
- _ownerFEs = new WeakReferenceList(1);
+ _ownerFEs = new WeakReferenceList(1);
}
else if (_ownerFEs.Contains(fe) && ContainsCycle(this))
{
@@ -1517,7 +1517,7 @@ internal void AddOwner(DispatcherObject owner)
{
if (_ownerFCEs == null)
{
- _ownerFCEs = new WeakReferenceList(1);
+ _ownerFCEs = new WeakReferenceList(1);
}
else if (_ownerFCEs.Contains(fce) && ContainsCycle(this))
{
@@ -1539,7 +1539,7 @@ internal void AddOwner(DispatcherObject owner)
{
if (_ownerApps == null)
{
- _ownerApps = new WeakReferenceList(1);
+ _ownerApps = new WeakReferenceList(1);
}
else if (_ownerApps.Contains(app) && ContainsCycle(this))
{
@@ -1628,32 +1628,28 @@ internal void RemoveOwner(DispatcherObject owner)
RemoveOwnerFromAllMergedDictionaries(owner);
}
- // Check if the given is an owner to this dictionary
- internal bool ContainsOwner(DispatcherObject owner)
+ ///
+ /// Checks if the given is owning this dictionary
+ ///
+ internal bool ContainsOwner(FrameworkElement owner)
{
- FrameworkElement fe = owner as FrameworkElement;
- if (fe != null)
- {
- return (_ownerFEs != null && _ownerFEs.Contains(fe));
- }
- else
- {
- FrameworkContentElement fce = owner as FrameworkContentElement;
- if (fce != null)
- {
- return (_ownerFCEs != null && _ownerFCEs.Contains(fce));
- }
- else
- {
- Application app = owner as Application;
- if (app != null)
- {
- return (_ownerApps != null && _ownerApps.Contains(app));
- }
- }
- }
+ return _ownerFEs?.Contains(owner) ?? false;
+ }
- return false;
+ ///
+ /// Checks if the given is owning this dictionary
+ ///
+ internal bool ContainsOwner(FrameworkContentElement owner)
+ {
+ return _ownerFCEs?.Contains(owner) ?? false;
+ }
+
+ ///
+ /// Checks if the given is owning this dictionary
+ ///
+ internal bool ContainsOwner(Application owner)
+ {
+ return _ownerApps?.Contains(owner) ?? false;
}
// Helper method that tries to set IsInitialized to true if BeginInit hasn't been called before this.
@@ -1681,62 +1677,50 @@ private void NotifyOwners(ResourcesChangeInfo info)
if (shouldInvalidate || hasImplicitStyles)
{
// Invalidate all FE owners
- if (_ownerFEs != null)
+ if (_ownerFEs is not null)
{
- foreach (Object o in _ownerFEs)
+ foreach (FrameworkElement fe in _ownerFEs)
{
- FrameworkElement fe = o as FrameworkElement;
- if (fe != null)
- {
- // Set the HasImplicitStyles flag on the owner
- if (hasImplicitStyles)
- fe.ShouldLookupImplicitStyles = true;
-
- // If this dictionary has been initialized fire an invalidation
- // to let the tree know of this change.
- if (shouldInvalidate)
- TreeWalkHelper.InvalidateOnResourcesChange(fe, null, info);
- }
+ // Set the HasImplicitStyles flag on the owner
+ if (hasImplicitStyles)
+ fe.ShouldLookupImplicitStyles = true;
+
+ // If this dictionary has been initialized fire an invalidation
+ // to let the tree know of this change.
+ if (shouldInvalidate)
+ TreeWalkHelper.InvalidateOnResourcesChange(fe, null, info);
}
}
// Invalidate all FCE owners
- if (_ownerFCEs != null)
+ if (_ownerFCEs is not null)
{
- foreach (Object o in _ownerFCEs)
+ foreach (FrameworkContentElement fce in _ownerFCEs)
{
- FrameworkContentElement fce = o as FrameworkContentElement;
- if (fce != null)
- {
- // Set the HasImplicitStyles flag on the owner
- if (hasImplicitStyles)
- fce.ShouldLookupImplicitStyles = true;
-
- // If this dictionary has been initialized fire an invalidation
- // to let the tree know of this change.
- if (shouldInvalidate)
- TreeWalkHelper.InvalidateOnResourcesChange(null, fce, info);
- }
+ // Set the HasImplicitStyles flag on the owner
+ if (hasImplicitStyles)
+ fce.ShouldLookupImplicitStyles = true;
+
+ // If this dictionary has been initialized fire an invalidation
+ // to let the tree know of this change.
+ if (shouldInvalidate)
+ TreeWalkHelper.InvalidateOnResourcesChange(null, fce, info);
}
}
// Invalidate all App owners
- if (_ownerApps != null)
+ if (_ownerApps is not null)
{
- foreach (Object o in _ownerApps)
+ foreach (Application app in _ownerApps)
{
- Application app = o as Application;
- if (app != null)
- {
- // Set the HasImplicitStyles flag on the owner
- if (hasImplicitStyles)
- app.HasImplicitStylesInResources = true;
-
- // If this dictionary has been initialized fire an invalidation
- // to let the tree know of this change.
- if (shouldInvalidate)
- app.InvalidateResourceReferences(info);
- }
+ // Set the HasImplicitStyles flag on the owner
+ if (hasImplicitStyles)
+ app.HasImplicitStylesInResources = true;
+
+ // If this dictionary has been initialized fire an invalidation
+ // to let the tree know of this change.
+ if (shouldInvalidate)
+ app.InvalidateResourceReferences(info);
}
}
}
@@ -1805,14 +1789,14 @@ private object FetchResource(
return GetValue(resourceKey, out canCache);
}
- private WeakReferenceList GetOrCreateWeakReferenceList(object resourceKey)
+ private WeakReferenceList GetOrCreateWeakReferenceList(object resourceKey)
{
- this._weakDeferredResourceReferencesMap ??= new();
+ _weakDeferredResourceReferencesMap ??= new Dictionary
private void ValidateDeferredResourceReferences(object resourceKey)
{
-
if (_weakDeferredResourceReferencesMap is null)
{
return;
@@ -1841,36 +1823,29 @@ private void ValidateDeferredResourceReferences(object resourceKey)
if (resourceKey is null)
{
- foreach (var weakDeferredResourceReferences in _weakDeferredResourceReferencesMap.Values)
+ foreach (WeakReferenceList weakReferencesList in _weakDeferredResourceReferencesMap.Values)
{
- foreach (var weakResourceReference in weakDeferredResourceReferences)
+ foreach (DeferredResourceReference weakReference in weakReferencesList)
{
- DeferredResourceReference deferredResourceReference = weakResourceReference as DeferredResourceReference;
-
- Inflate(deferredResourceReference);
+ Inflate(weakReference);
}
}
}
else
{
- if (_weakDeferredResourceReferencesMap.TryGetValue(resourceKey, out var weakDeferredResourceReferences))
+ if (_weakDeferredResourceReferencesMap.TryGetValue(resourceKey, out WeakReferenceList weakReferencesList))
{
- foreach (var weakResourceReference in weakDeferredResourceReferences)
+ foreach (DeferredResourceReference weakReference in weakReferencesList)
{
- DeferredResourceReference deferredResourceReference = weakResourceReference as DeferredResourceReference;
-
- Inflate(deferredResourceReference);
+ Inflate(weakReference);
}
}
}
- return;
-
- void Inflate(DeferredResourceReference deferredResourceReference)
+ static void Inflate(DeferredResourceReference deferredResourceReference)
{
- // This will inflate the deferred reference, causing it
- // to be removed from the list. The list may also be
- // purged of dead references.
+ // This will inflate the deferred reference, causing it to be removed from the list.
+ // The list may also be purged of dead references.
deferredResourceReference?.GetValue(BaseValueSourceInternal.Unknown);
}
}
@@ -2012,14 +1987,12 @@ private void PropagateParentOwners(ResourceDictionary mergedDictionary)
if (mergedDictionary._ownerFEs == null)
{
- mergedDictionary._ownerFEs = new WeakReferenceList(_ownerFEs.Count);
+ mergedDictionary._ownerFEs = new WeakReferenceList(_ownerFEs.Count);
}
- foreach (object o in _ownerFEs)
+ foreach (FrameworkElement fe in _ownerFEs)
{
- FrameworkElement fe = o as FrameworkElement;
- if (fe != null)
- mergedDictionary.AddOwner(fe);
+ mergedDictionary.AddOwner(fe);
}
}
@@ -2029,14 +2002,12 @@ private void PropagateParentOwners(ResourceDictionary mergedDictionary)
if (mergedDictionary._ownerFCEs == null)
{
- mergedDictionary._ownerFCEs = new WeakReferenceList(_ownerFCEs.Count);
+ mergedDictionary._ownerFCEs = new WeakReferenceList(_ownerFCEs.Count);
}
- foreach (object o in _ownerFCEs)
+ foreach (FrameworkContentElement fce in _ownerFCEs)
{
- FrameworkContentElement fce = o as FrameworkContentElement;
- if (fce != null)
- mergedDictionary.AddOwner(fce);
+ mergedDictionary.AddOwner(fce);
}
}
@@ -2046,14 +2017,12 @@ private void PropagateParentOwners(ResourceDictionary mergedDictionary)
if (mergedDictionary._ownerApps == null)
{
- mergedDictionary._ownerApps = new WeakReferenceList(_ownerApps.Count);
+ mergedDictionary._ownerApps = new WeakReferenceList(_ownerApps.Count);
}
- foreach (object o in _ownerApps)
+ foreach (Application app in _ownerApps)
{
- Application app = o as Application;
- if (app != null)
- mergedDictionary.AddOwner(app);
+ mergedDictionary.AddOwner(app);
}
}
}
@@ -2067,37 +2036,31 @@ private void PropagateParentOwners(ResourceDictionary mergedDictionary)
///
internal void RemoveParentOwners(ResourceDictionary mergedDictionary)
{
- if (_ownerFEs != null)
+ if (_ownerFEs is not null)
{
- foreach (Object o in _ownerFEs)
+ foreach (FrameworkElement fe in _ownerFEs)
{
- FrameworkElement fe = o as FrameworkElement;
mergedDictionary.RemoveOwner(fe);
-
}
}
- if (_ownerFCEs != null)
+ if (_ownerFCEs is not null)
{
Invariant.Assert(_ownerFCEs.Count > 0);
- foreach (Object o in _ownerFCEs)
+ foreach (FrameworkContentElement fec in _ownerFCEs)
{
- FrameworkContentElement fec = o as FrameworkContentElement;
mergedDictionary.RemoveOwner(fec);
-
}
}
- if (_ownerApps != null)
+ if (_ownerApps is not null)
{
Invariant.Assert(_ownerApps.Count > 0);
- foreach (Object o in _ownerApps)
+ foreach (Application app in _ownerApps)
{
- Application app = o as Application;
mergedDictionary.RemoveOwner(app);
-
}
}
}
@@ -2118,19 +2081,19 @@ private bool ContainsCycle(ResourceDictionary origin)
// three properties used by ResourceDictionaryDiagnostics
- internal WeakReferenceList FrameworkElementOwners
+ internal WeakReferenceList FrameworkElementOwners
{
- get { return _ownerFEs; }
+ get => _ownerFEs;
}
- internal WeakReferenceList FrameworkContentElementOwners
+ internal WeakReferenceList FrameworkContentElementOwners
{
- get { return _ownerFCEs; }
+ get => _ownerFCEs;
}
- internal WeakReferenceList ApplicationOwners
+ internal WeakReferenceList ApplicationOwners
{
- get { return _ownerApps; }
+ get => _ownerApps;
}
#endregion HelperMethods
@@ -2619,11 +2582,12 @@ private enum FallbackState
#region Data
+ private WeakReferenceList _ownerFEs;
+ private WeakReferenceList _ownerFCEs;
+ private WeakReferenceList _ownerApps;
+ private Dictionary