@@ -202,7 +202,7 @@ private static DialogHost GetInstance(object? dialogIdentifier)
202
202
throw new InvalidOperationException ( "No loaded DialogHost instances." ) ;
203
203
204
204
List < DialogHost > targets = new ( ) ;
205
- foreach ( var instance in LoadedInstances . ToList ( ) )
205
+ foreach ( var instance in LoadedInstances . ToList ( ) )
206
206
{
207
207
if ( instance . TryGetTarget ( out DialogHost ? dialogInstance ) )
208
208
{
@@ -750,7 +750,7 @@ private string GetStateName()
750
750
751
751
private void OnUnloaded ( object sender , RoutedEventArgs routedEventArgs )
752
752
{
753
- foreach ( var weakRef in LoadedInstances . ToList ( ) )
753
+ foreach ( var weakRef in LoadedInstances . ToList ( ) )
754
754
{
755
755
if ( ! weakRef . TryGetTarget ( out DialogHost ? dialogHost ) ||
756
756
Equals ( dialogHost , this ) )
@@ -761,7 +761,19 @@ private void OnUnloaded(object sender, RoutedEventArgs routedEventArgs)
761
761
}
762
762
763
763
private void OnLoaded ( object sender , RoutedEventArgs routedEventArgs )
764
- => LoadedInstances . Add ( new WeakReference < DialogHost > ( this ) ) ;
765
-
764
+ {
765
+ foreach ( var weakRef in LoadedInstances . ToList ( ) )
766
+ {
767
+ if ( ! weakRef . TryGetTarget ( out DialogHost ? dialogHost ) )
768
+ {
769
+ LoadedInstances . Remove ( weakRef ) ;
770
+ }
771
+ if ( Equals ( dialogHost , this ) )
772
+ {
773
+ return ;
774
+ }
775
+ }
776
+ LoadedInstances . Add ( new WeakReference < DialogHost > ( this ) ) ;
777
+ }
766
778
}
767
779
}
0 commit comments