Skip to content

Commit 09f967d

Browse files
committed
Revert "Fixing issues with DynamicResource usage"
This reverts commit 701607e.
1 parent b9046d9 commit 09f967d

File tree

4 files changed

+40
-72
lines changed

4 files changed

+40
-72
lines changed

src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/ResourceDictionary.cs

Lines changed: 20 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ private void CopyToWithoutLock(DictionaryEntry[] array, int arrayIndex)
109109
entry.Value = value; // refresh the entry value in case it was changed in the previous call
110110
}
111111
}
112-
112+
113113
// This is set when the RD is loaded from unsafe xps doc. This will be checked while creating reader for RD source.
114114
internal bool IsUnsafe { get; set; }
115115

@@ -157,7 +157,7 @@ public Uri Source
157157
// that it is being passed down by the Baml parsing code, and it is trying to give us more
158158
// information to avoid possible ambiguities in assembly resolving. Use the VersionedUri
159159
// to resolve, and the set _source to the OriginalUri so we don't change the return of Source property.
160-
// The versioned Uri is not stored, if the version info is needed while debugging, once this method
160+
// The versioned Uri is not stored, if the version info is needed while debugging, once this method
161161
// returns _reader should be set, from there BamlSchemaContext.LocalAssembly contains the version info.
162162
if (uriWrapper == null)
163163
{
@@ -169,10 +169,10 @@ public Uri Source
169169
_source = uriWrapper.OriginalUri;
170170
sourceUri = uriWrapper.VersionedUri;
171171
}
172-
172+
173173
Clear();
174-
175-
174+
175+
176176
Uri uri = BindUriHelper.GetResolvedUri(_baseUri, sourceUri);
177177

178178
WebRequest request = WpfWebRequestHelper.CreateRequest(uri);
@@ -1737,7 +1737,10 @@ private object FetchResource(
17371737
{
17381738
// Cache the deferredResourceReference so that it can be validated
17391739
// in case of a dictionary change prior to its inflation
1740-
_deferredResourceReferences ??= new DeferredResourceReferenceList();
1740+
if (_deferredResourceReferences == null)
1741+
{
1742+
_deferredResourceReferences = new DeferredResourceReferenceList();
1743+
}
17411744

17421745
if (_deferredResourceReferences.Get(resourceKey) is { } existingDeferredResourceReference
17431746
&& existingDeferredResourceReference.Dictionary == this)
@@ -1746,7 +1749,14 @@ private object FetchResource(
17461749
}
17471750
else
17481751
{
1749-
deferredResourceReference = _ownerApps is not null ? new DeferredAppResourceReference(this, resourceKey) : new DeferredResourceReference(this, resourceKey);
1752+
if (_ownerApps != null)
1753+
{
1754+
deferredResourceReference = new DeferredAppResourceReference(this, resourceKey);
1755+
}
1756+
else
1757+
{
1758+
deferredResourceReference = new DeferredResourceReference(this, resourceKey);
1759+
}
17501760

17511761
_deferredResourceReferences.AddOrSet(deferredResourceReference);
17521762
}
@@ -1771,29 +1781,10 @@ private object FetchResource(
17711781
/// </summary>
17721782
private void ValidateDeferredResourceReferences(object resourceKey)
17731783
{
1774-
if (_deferredResourceReferences is null)
1775-
{
1776-
return;
1777-
}
1778-
1779-
if (resourceKey is null)
1780-
{
1781-
foreach (DeferredResourceReference deferredResourceReference in _deferredResourceReferences)
1782-
{
1783-
Inflate(deferredResourceReference);
1784-
}
1785-
}
1786-
else
1784+
if (_deferredResourceReferences != null)
17871785
{
17881786
DeferredResourceReference deferredResourceReference = _deferredResourceReferences.Get(resourceKey);
17891787

1790-
Inflate(deferredResourceReference);
1791-
}
1792-
1793-
return;
1794-
1795-
void Inflate(DeferredResourceReference deferredResourceReference)
1796-
{
17971788
if (deferredResourceReference is not null)
17981789
{
17991790
// This will inflate the deferred reference, causing it
@@ -2513,9 +2504,9 @@ private enum PrivateFlags : byte
25132504

25142505
/// <summary>
25152506
/// This wrapper class exists so SourceUriTypeConverterMarkupExtension can pass
2516-
/// a more complete Uri to help resolve to the correct assembly, while also passing
2507+
/// a more complete Uri to help resolve to the correct assembly, while also passing
25172508
/// the original Uri so that ResourceDictionary.Source still returns the original value.
2518-
/// </summary>
2509+
/// </summary>
25192510
internal class ResourceDictionarySourceUriWrapper : Uri
25202511
{
25212512
public ResourceDictionarySourceUriWrapper(Uri originalUri, Uri versionedUri) : base(originalUri.OriginalString, UriKind.RelativeOrAbsolute)

src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/ResourceReferenceExpression.cs

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -278,10 +278,7 @@ private void InvalidateCacheValue()
278278
}
279279
}
280280

281-
// This will inflate the deferred reference, causing it
282-
// to be removed from the list. The list may also be
283-
// purged of dead references.
284-
deferredResourceReference.GetValue(BaseValueSourceInternal.Unknown);
281+
deferredResourceReference.RemoveFromDictionary();
285282
}
286283

287284
StopListeningForFreezableChanges(resource);
@@ -344,8 +341,8 @@ private void InvalidateMentorCache()
344341
internal void InvalidateExpressionValue(object sender, EventArgs e)
345342
{
346343
// VS has a scenario where a TreeWalk invalidates all reference expressions on a DependencyObject.
347-
// If there is a dependency between RRE's,
348-
// invalidating one RRE could cause _targetObject to be null on the other RRE. Hence this check.
344+
// If there is a dependency between RRE's,
345+
// invalidating one RRE could cause _targetObject to be null on the other RRE. Hence this check.
349346
if (_targetObject == null)
350347
{
351348
return;
@@ -407,7 +404,7 @@ private void ListenForFreezableChanges(object resource)
407404
{
408405
_weakContainerRRE = new ResourceReferenceExpressionWeakContainer(this);
409406
}
410-
407+
411408
// Hook up the event to the weak container to prevent memory leaks (Bug436021)
412409
_weakContainerRRE.AddChangedHandler(resourceAsFreezable);
413410
WriteInternalState(InternalState.IsListeningForFreezableChanges, true);
@@ -438,7 +435,7 @@ private void StopListeningForFreezableChanges(object resource)
438435
}
439436
}
440437

441-
// It is possible that a freezable was unfrozen during the call to ListForFreezableChanges
438+
// It is possible that a freezable was unfrozen during the call to ListForFreezableChanges
442439
// but was frozen before the call to StopListeningForFreezableChanges
443440
WriteInternalState(InternalState.IsListeningForFreezableChanges, false);
444441
}
@@ -515,8 +512,8 @@ private enum InternalState : byte
515512
#region ResourceReferenceExpressionWeakContainer
516513

517514
/// <summary>
518-
/// ResourceReferenceExpressionWeakContainer handles the Freezable.Changed event
519-
/// without holding a strong reference to ResourceReferenceExpression.
515+
/// ResourceReferenceExpressionWeakContainer handles the Freezable.Changed event
516+
/// without holding a strong reference to ResourceReferenceExpression.
520517
/// </summary>
521518
private class ResourceReferenceExpressionWeakContainer : WeakReference
522519
{
@@ -545,7 +542,7 @@ public void AddChangedHandler(Freezable resource)
545542
}
546543

547544
_resource = resource;
548-
545+
549546
Debug.Assert(!_resource.IsFrozen);
550547
_resource.Changed += new EventHandler(this.InvalidateTargetSubProperty);
551548
}
@@ -561,7 +558,7 @@ public void RemoveChangedHandler()
561558

562559
private Freezable _resource;
563560
}
564-
#endregion
561+
#endregion
565562
}
566563

567564
/// <summary>

src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/SystemResources.cs

Lines changed: 9 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1739,7 +1739,8 @@ internal override object GetValue(BaseValueSourceInternal valueSource)
17391739
// the dictionary else just retun the cached value
17401740
if (_dictionary != null)
17411741
{
1742-
object value = _dictionary.GetValue(_keyOrValue, out bool canCache);
1742+
bool canCache;
1743+
object value = _dictionary.GetValue(_keyOrValue, out canCache);
17431744
if (canCache)
17441745
{
17451746
// Note that we are replacing the _keyorValue field
@@ -1795,7 +1796,8 @@ internal override Type GetValueType()
17951796
{
17961797
// Take a peek at the element type of the ElementStartRecord
17971798
// within the ResourceDictionary's deferred content.
1798-
return _dictionary.GetValueType(_keyOrValue, out bool _);
1799+
bool found;
1800+
return _dictionary.GetValueType(_keyOrValue, out found);
17991801
}
18001802
else
18011803
{
@@ -1804,7 +1806,7 @@ internal override Type GetValueType()
18041806
}
18051807

18061808
// remove this DeferredResourceReference from its ResourceDictionary
1807-
protected virtual void RemoveFromDictionary()
1809+
internal virtual void RemoveFromDictionary()
18081810
{
18091811
if (_dictionary != null)
18101812
{
@@ -1974,7 +1976,7 @@ internal override Type GetValueType()
19741976
}
19751977

19761978
// remove this DeferredResourceReference from its ResourceDictionary
1977-
protected override void RemoveFromDictionary()
1979+
internal override void RemoveFromDictionary()
19781980
{
19791981
// DeferredThemeResourceReferences are never added to the dictionary's
19801982
// list of deferred references, so they don't need to be removed.
@@ -2038,11 +2040,11 @@ internal override bool IsUnset
20382040
#endregion Properties
20392041
}
20402042

2041-
internal class DeferredResourceReferenceList : IEnumerable<DeferredResourceReference>
2043+
internal class DeferredResourceReferenceList
20422044
{
20432045
private readonly object _syncRoot = new();
20442046
private readonly Dictionary<object, WeakReference<DeferredResourceReference>> _entries = new();
2045-
private int _potentiallyDeadEntryCount;
2047+
private int _potentiallyDeadEntryCount = 0;
20462048

20472049
public void AddOrSet(DeferredResourceReference deferredResourceReference)
20482050
{
@@ -2115,11 +2117,6 @@ private void PurgeIfRequired()
21152117
}
21162118

21172119
private void Purge()
2118-
{
2119-
Purge(null);
2120-
}
2121-
2122-
private void Purge(List<DeferredResourceReference> aliveItems)
21232120
{
21242121
lock (_syncRoot)
21252122
{
@@ -2128,14 +2125,10 @@ private void Purge(List<DeferredResourceReference> aliveItems)
21282125

21292126
foreach (KeyValuePair<object, WeakReference<DeferredResourceReference>> entry in _entries)
21302127
{
2131-
if (entry.Value.TryGetTarget(out var item) is false)
2128+
if (entry.Value.TryGetTarget(out _) == false)
21322129
{
21332130
deadKeys.Add(entry.Key);
21342131
}
2135-
else
2136-
{
2137-
aliveItems?.Add(item);
2138-
}
21392132
}
21402133

21412134
foreach (object deadKey in deadKeys)
@@ -2144,17 +2137,5 @@ private void Purge(List<DeferredResourceReference> aliveItems)
21442137
}
21452138
}
21462139
}
2147-
2148-
public IEnumerator<DeferredResourceReference> GetEnumerator()
2149-
{
2150-
var aliveItems = new List<DeferredResourceReference>(_entries.Count);
2151-
Purge(aliveItems);
2152-
return aliveItems.GetEnumerator();
2153-
}
2154-
2155-
IEnumerator IEnumerable.GetEnumerator()
2156-
{
2157-
return GetEnumerator();
2158-
}
21592140
}
21602141
}

src/Microsoft.DotNet.Wpf/src/WindowsBase/System/Windows/DependencyObject.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -863,7 +863,7 @@ internal bool ProvideSelfAsInheritanceContext( DependencyObject doValue, Depende
863863
// on side-effects from setting the "Freezable context". Freezable's
864864
// implementation does its own checks of the conditions omitted here.
865865
// Enhancement suggestion: Freezable should follow the same rules for
866-
// InheritanceContext as everyone else
866+
// InheritanceContext as everyone else
867867

868868

869869
if (doValue != null &&
@@ -910,7 +910,7 @@ internal bool RemoveSelfAsInheritanceContext( DependencyObject doValue, Dependen
910910
// on side-effects from setting the "Freezable context". Freezable's
911911
// implementation does its own checks of the conditions omitted here.
912912
// Enhancement suggestion: Freezable should follow the same rules for
913-
// InheritanceContext as everyone else
913+
// InheritanceContext as everyone else
914914

915915

916916
if (doValue != null &&
@@ -3534,7 +3534,6 @@ internal enum UpdateResult
35343534
}
35353535

35363536
[FriendAccessAllowed] // Built into Base, also used by Framework.
3537-
[Flags]
35383537
internal enum RequestFlags
35393538
{
35403539
FullyResolved = 0x00,

0 commit comments

Comments
 (0)