Skip to content

Commit 2c7b325

Browse files
committed
Update WeakReferences to use WinRT.WeakReference.
1 parent d232fae commit 2c7b325

File tree

6 files changed

+16
-19
lines changed

6 files changed

+16
-19
lines changed

Microsoft.Toolkit.Uwp.UI.Animations/CompositionAnimations/AnimationCollection.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public class AnimationCollection : IList<AnimationBase>
2121

2222
// needed in order to be able to update animations when a animations are added/removed or
2323
// animation properties change (for example in binding)
24-
private WeakReference<UIElement> _parent;
24+
private WinRT.WeakReference<UIElement> _parent;
2525

2626
internal UIElement Parent
2727
{
@@ -31,7 +31,7 @@ internal UIElement Parent
3131
return element;
3232
}
3333

34-
set => _parent = new WeakReference<UIElement>(value);
34+
set => _parent = new WinRT.WeakReference<UIElement>(value);
3535
}
3636

3737
/// <summary>

Microsoft.Toolkit.Uwp.UI.Controls.DataGrid/Utilities/WeakEventListener.cs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ internal sealed class WeakEventListener<TInstance, TSource, TEventArgs>
2222
/// <summary>
2323
/// WeakReference to the instance listening for the event.
2424
/// </summary>
25-
private WeakReference weakInstance;
25+
private WinRT.WeakReference<TInstance> weakInstance;
2626

2727
/// <summary>
2828
/// Initializes a new instance of the <see cref="WeakEventListener{TInstance, TSource, TEventArgs}"/> class.
@@ -35,7 +35,7 @@ public WeakEventListener(TInstance instance)
3535
throw new ArgumentNullException(nameof(instance));
3636
}
3737

38-
weakInstance = new WeakReference(instance);
38+
weakInstance = new WinRT.WeakReference<TInstance>(instance);
3939
}
4040

4141
/// <summary>
@@ -55,8 +55,7 @@ public WeakEventListener(TInstance instance)
5555
/// <param name="eventArgs">Event arguments.</param>
5656
public void OnEvent(TSource source, TEventArgs eventArgs)
5757
{
58-
TInstance target = (TInstance)weakInstance.Target;
59-
if (target != null)
58+
if (weakInstance.TryGetTarget(out var target))
6059
{
6160
// Call registered action
6261
OnEventAction?.Invoke(target, source, eventArgs);

Microsoft.Toolkit.Uwp.UI.Media/Helpers/Cache/CompositionObjectCache{TKey,TValue}.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ internal sealed class CompositionObjectCache<TKey, TValue>
2020
/// <summary>
2121
/// The cache of weak references of type <typeparamref name="TValue"/> to <typeparamref name="TKey"/> instances, to avoid memory leaks
2222
/// </summary>
23-
private readonly ConditionalWeakTable<Compositor, Dictionary<TKey, WeakReference<TValue>>> cache = new ConditionalWeakTable<Compositor, Dictionary<TKey, WeakReference<TValue>>>();
23+
private readonly ConditionalWeakTable<Compositor, Dictionary<TKey, WinRT.WeakReference<TValue>>> cache = new ConditionalWeakTable<Compositor, Dictionary<TKey, WinRT.WeakReference<TValue>>>();
2424

2525
/// <summary>
2626
/// Tries to retrieve a valid instance from the cache, and uses the provided factory if an existing item is not found
@@ -59,11 +59,11 @@ public void AddOrUpdate(Compositor compositor, TKey key, TValue value)
5959
{
6060
_ = map.Remove(key);
6161

62-
map.Add(key, new WeakReference<TValue>(value));
62+
map.Add(key, new WinRT.WeakReference<TValue>(value));
6363
}
6464
else
6565
{
66-
map = new Dictionary<TKey, WeakReference<TValue>> { [key] = new WeakReference<TValue>(value) };
66+
map = new Dictionary<TKey, WinRT.WeakReference<TValue>> { [key] = new WinRT.WeakReference<TValue>(value) };
6767

6868
this.cache.Add(compositor, map);
6969
}

Microsoft.Toolkit.Uwp.UI.Media/Helpers/Cache/CompositionObjectCache{T}.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ internal sealed class CompositionObjectCache<T>
1818
/// <summary>
1919
/// The cache of weak references of type <typeparamref name="T"/>, to avoid memory leaks
2020
/// </summary>
21-
private readonly ConditionalWeakTable<Compositor, WeakReference<T>> cache = new ConditionalWeakTable<Compositor, WeakReference<T>>();
21+
private readonly ConditionalWeakTable<Compositor, WinRT.WeakReference<T>> cache = new ConditionalWeakTable<Compositor, WinRT.WeakReference<T>>();
2222

2323
/// <summary>
2424
/// Tries to retrieve a valid <typeparamref name="T"/> instance from the cache, and uses the provided factory if an existing item is not found
@@ -38,7 +38,7 @@ public T GetValue(Compositor compositor, Func<Compositor, T> producer)
3838

3939
// Create a new instance when needed
4040
var fallback = producer(compositor);
41-
this.cache.AddOrUpdate(compositor, new WeakReference<T>(fallback));
41+
this.cache.AddOrUpdate(compositor, new WinRT.WeakReference<T>(fallback));
4242

4343
return fallback;
4444
}

Microsoft.Toolkit.Uwp.UI/Triggers/NetworkConnectionStateTrigger.cs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ private class WeakEventListener<TInstance, TSource>
7272
/// <summary>
7373
/// WeakReference to the instance listening for the event.
7474
/// </summary>
75-
private WeakReference _weakInstance;
75+
private WinRT.WeakReference<TInstance> _weakInstance;
7676

7777
/// <summary>
7878
/// Gets or sets the method to call when the event fires.
@@ -95,7 +95,7 @@ public WeakEventListener(TInstance instance)
9595
throw new ArgumentNullException("instance");
9696
}
9797

98-
_weakInstance = new WeakReference(instance);
98+
_weakInstance = new WinRT.WeakReference<TInstance>(instance);
9999
}
100100

101101
/// <summary>
@@ -104,8 +104,7 @@ public WeakEventListener(TInstance instance)
104104
/// <param name="source">Event source.</param>
105105
public void OnEvent(TSource source)
106106
{
107-
TInstance target = (TInstance)_weakInstance.Target;
108-
if (target != null)
107+
if (_weakInstance.TryGetTarget(out var target))
109108
{
110109
// Call registered action
111110
OnEventAction?.Invoke(target, source);

Microsoft.Toolkit.Uwp/Helpers/WeakEventListener.cs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public sealed class WeakEventListener<TInstance, TSource, TEventArgs>
2020
/// <summary>
2121
/// WeakReference to the instance listening for the event.
2222
/// </summary>
23-
private readonly WeakReference _weakInstance;
23+
private readonly WinRT.WeakReference<TInstance> _weakInstance;
2424

2525
/// <summary>
2626
/// Initializes a new instance of the <see cref="WeakEventListener{TInstance, TSource, TEventArgs}"/> class.
@@ -33,7 +33,7 @@ public WeakEventListener(TInstance instance)
3333
throw new ArgumentNullException(nameof(instance));
3434
}
3535

36-
_weakInstance = new WeakReference(instance);
36+
_weakInstance = new WinRT.WeakReference<TInstance>(instance);
3737
}
3838

3939
/// <summary>
@@ -53,8 +53,7 @@ public WeakEventListener(TInstance instance)
5353
/// <param name="eventArgs">Event arguments.</param>
5454
public void OnEvent(TSource source, TEventArgs eventArgs)
5555
{
56-
var target = (TInstance)_weakInstance.Target;
57-
if (target != null)
56+
if (_weakInstance.TryGetTarget(out var target))
5857
{
5958
// Call registered action
6059
OnEventAction?.Invoke(target, source, eventArgs);

0 commit comments

Comments
 (0)