Skip to content

Commit 9ec8019

Browse files
authored
Merge branch 'main' into Remove-CommunityToolkit.Maui.SourceGenerators.Internals.-
2 parents 787a140 + 15611b7 commit 9ec8019

File tree

2 files changed

+32
-1
lines changed

2 files changed

+32
-1
lines changed

src/CommunityToolkit.Maui/Alerts/Snackbar/Snackbar.shared.cs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ public partial class Snackbar : ISnackbar
88
static readonly WeakEventManager weakEventManager = new();
99

1010
bool isDisposed;
11+
WeakReference<IView>? weakView;
1112

1213
/// <summary>
1314
/// Initializes a new instance of <see cref="Snackbar"/>
@@ -57,7 +58,20 @@ public string ActionButtonText
5758
public Action? Action { get; init; }
5859

5960
/// <inheritdoc/>
60-
public IView? Anchor { get; init; }
61+
public IView? Anchor
62+
{
63+
get => weakView?.GetTargetOrDefault();
64+
init
65+
{
66+
if (value is null)
67+
{
68+
weakView = null;
69+
return;
70+
}
71+
72+
weakView = new(value);
73+
}
74+
}
6175

6276
/// <summary>
6377
/// Occurs when <see cref="IsShown"/> changes.
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
namespace CommunityToolkit.Maui.Extensions;
2+
3+
static class WeakReferenceExtensions
4+
{
5+
public static T? GetTargetOrDefault<T>(this WeakReference<T> self)
6+
where T : class
7+
{
8+
ArgumentNullException.ThrowIfNull(self);
9+
10+
if (self.TryGetTarget(out var target))
11+
{
12+
return target;
13+
}
14+
15+
return default;
16+
}
17+
}

0 commit comments

Comments
 (0)