File tree Expand file tree Collapse file tree 2 files changed +32
-1
lines changed
Expand file tree Collapse file tree 2 files changed +32
-1
lines changed Original file line number Diff line number Diff 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.
Original file line number Diff line number Diff line change 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+ }
You can’t perform that action at this time.
0 commit comments