Skip to content

Commit 902e623

Browse files
authored
Don't allow multiple instances of the same dialog host in loaded instances (#2440)
Fixes #85
1 parent ad3bee2 commit 902e623

File tree

1 file changed

+16
-4
lines changed

1 file changed

+16
-4
lines changed

MaterialDesignThemes.Wpf/DialogHost.cs

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,7 @@ private static DialogHost GetInstance(object? dialogIdentifier)
202202
throw new InvalidOperationException("No loaded DialogHost instances.");
203203

204204
List<DialogHost> targets = new();
205-
foreach(var instance in LoadedInstances.ToList())
205+
foreach (var instance in LoadedInstances.ToList())
206206
{
207207
if (instance.TryGetTarget(out DialogHost? dialogInstance))
208208
{
@@ -750,7 +750,7 @@ private string GetStateName()
750750

751751
private void OnUnloaded(object sender, RoutedEventArgs routedEventArgs)
752752
{
753-
foreach(var weakRef in LoadedInstances.ToList())
753+
foreach (var weakRef in LoadedInstances.ToList())
754754
{
755755
if (!weakRef.TryGetTarget(out DialogHost? dialogHost) ||
756756
Equals(dialogHost, this))
@@ -761,7 +761,19 @@ private void OnUnloaded(object sender, RoutedEventArgs routedEventArgs)
761761
}
762762

763763
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+
}
766778
}
767779
}

0 commit comments

Comments
 (0)