Skip to content

Commit f212c0e

Browse files
Yoooi0Keboo
andauthored
Improve null dialog identifier handling (#2445)
* Improve null dialog identifier handling (#2262) * Adding unit test Fixing nullable reference type declaration of DialogHost methods. Co-authored-by: Kevin Bost <[email protected]>
1 parent 1e91d0a commit f212c0e

File tree

2 files changed

+27
-4
lines changed

2 files changed

+27
-4
lines changed

MaterialDesignThemes.Wpf.Tests/DialogHostTests.cs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -333,5 +333,28 @@ public void WhenOpenDialogsAreOpenIsExist()
333333
DialogHost.Close(id);
334334
Assert.False(DialogHost.IsDialogOpen(id));
335335
}
336+
337+
[StaFact]
338+
[Description("Issue 2262")]
339+
public async Task WhenOnlySingleDialogHostIdentifierIsNullItShowsDialog()
340+
{
341+
DialogHost dialogHost2 = new();
342+
dialogHost2.ApplyDefaultStyle();
343+
dialogHost2.Identifier = Guid.NewGuid();
344+
345+
try
346+
{
347+
dialogHost2.RaiseEvent(new RoutedEventArgs(FrameworkElement.LoadedEvent));
348+
Task showTask = DialogHost.Show("Content");
349+
Assert.True(DialogHost.IsDialogOpen(null));
350+
Assert.False(DialogHost.IsDialogOpen(dialogHost2.Identifier));
351+
DialogHost.Close(null);
352+
await showTask;
353+
}
354+
finally
355+
{
356+
dialogHost2.RaiseEvent(new RoutedEventArgs(FrameworkElement.UnloadedEvent));
357+
}
358+
}
336359
}
337360
}

MaterialDesignThemes.Wpf/DialogHost.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ static DialogHost()
159159
/// Close a modal dialog.
160160
/// </summary>
161161
/// <param name="dialogIdentifier"> of the instance where the dialog should be closed. Typically this will match an identifer set in XAML. </param>
162-
public static void Close(object dialogIdentifier)
162+
public static void Close(object? dialogIdentifier)
163163
=> Close(dialogIdentifier, null);
164164

165165
/// <summary>
@@ -183,7 +183,7 @@ public static void Close(object? dialogIdentifier, object? parameter)
183183
/// </summary>
184184
/// <param name="dialogIdentifier">The identifier to use to retrieve the DialogHost</param>
185185
/// <returns>The DialogSession if one is in process, or null</returns>
186-
public static DialogSession? GetDialogSession(object dialogIdentifier)
186+
public static DialogSession? GetDialogSession(object? dialogIdentifier)
187187
{
188188
DialogHost dialogHost = GetInstance(dialogIdentifier);
189189
return dialogHost.CurrentSession;
@@ -194,7 +194,7 @@ public static void Close(object? dialogIdentifier, object? parameter)
194194
/// </summary>
195195
/// <param name="dialogIdentifier">of the instance where the dialog should be closed. Typically this will match an identifer set in XAML.</param>
196196
/// <returns></returns>
197-
public static bool IsDialogOpen(object dialogIdentifier) => GetDialogSession(dialogIdentifier)?.IsEnded == false;
197+
public static bool IsDialogOpen(object? dialogIdentifier) => GetDialogSession(dialogIdentifier)?.IsEnded == false;
198198

199199
private static DialogHost GetInstance(object? dialogIdentifier)
200200
{
@@ -207,7 +207,7 @@ private static DialogHost GetInstance(object? dialogIdentifier)
207207
if (instance.TryGetTarget(out DialogHost? dialogInstance))
208208
{
209209
dialogInstance.Dispatcher.VerifyAccess();
210-
if (dialogIdentifier is null || Equals(dialogIdentifier, dialogInstance.Identifier))
210+
if (Equals(dialogIdentifier, dialogInstance.Identifier))
211211
{
212212
targets.Add(dialogInstance);
213213
}

0 commit comments

Comments
 (0)