Skip to content

Commit e44b53f

Browse files
authored
Call dialog after canceling (#1213)
Fixes #1212. After canceling a dialog, it should still invoke the event handlers.
1 parent 5bbd26f commit e44b53f

File tree

3 files changed

+47
-12
lines changed

3 files changed

+47
-12
lines changed

MaterialDesignThemes.Wpf.Tests/DialogHostTests.cs

Lines changed: 40 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
using System;
2+
using System.ComponentModel;
23
using System.Threading.Tasks;
34
using System.Windows;
5+
using System.Windows.Controls;
46
using Xunit;
57

68
namespace MaterialDesignThemes.Wpf.Tests
@@ -20,7 +22,7 @@ public void Dispose()
2022
_dialogHost.RaiseEvent(new RoutedEventArgs(FrameworkElement.UnloadedEvent));
2123
}
2224

23-
[StaFact(Timeout = 500)]
25+
[StaFact]
2426
public void CanOpenAndCloseDialogWithIsOpen()
2527
{
2628
_dialogHost.IsOpen = true;
@@ -33,7 +35,7 @@ public void CanOpenAndCloseDialogWithIsOpen()
3335
Assert.True(session.IsEnded);
3436
}
3537

36-
[StaFact(Timeout = 500)]
38+
[StaFact]
3739
public async Task CanOpenAndCloseDialogWithShowMethod()
3840
{
3941
var id = Guid.NewGuid();
@@ -46,7 +48,7 @@ public async Task CanOpenAndCloseDialogWithShowMethod()
4648
Assert.False(_dialogHost.IsOpen);
4749
}
4850

49-
[StaFact(Timeout = 500)]
51+
[StaFact]
5052
public async Task CanOpenDialogWithShowMethodAndCloseWithIsOpen()
5153
{
5254
var id = Guid.NewGuid();
@@ -59,7 +61,7 @@ public async Task CanOpenDialogWithShowMethodAndCloseWithIsOpen()
5961
Assert.False(_dialogHost.IsOpen);
6062
}
6163

62-
[StaFact(Timeout = 500)]
64+
[StaFact]
6365
public async Task DialogHostExposesSessionAsProperty()
6466
{
6567
var id = Guid.NewGuid();
@@ -73,7 +75,7 @@ await DialogHost.Show("Content", id,
7375
})));
7476
}
7577

76-
[StaFact(Timeout = 500)]
78+
[StaFact]
7779
public async Task CannotShowDialogWhileItIsAlreadyOpen()
7880
{
7981
var id = Guid.NewGuid();
@@ -88,7 +90,7 @@ await DialogHost.Show("Content", id,
8890
})));
8991
}
9092

91-
[StaFact(Timeout = 500)]
93+
[StaFact]
9294
public async Task WhenNoDialogsAreOpenItThrows()
9395
{
9496
var id = Guid.NewGuid();
@@ -99,7 +101,7 @@ public async Task WhenNoDialogsAreOpenItThrows()
99101
Assert.Equal("No loaded DialogHost instances.", ex.Message);
100102
}
101103

102-
[StaFact(Timeout = 500)]
104+
[StaFact]
103105
public async Task WhenNoDialogsMatchIdentifierItThrows()
104106
{
105107
var id = Guid.NewGuid();
@@ -109,7 +111,7 @@ public async Task WhenNoDialogsMatchIdentifierItThrows()
109111
Assert.Equal($"No loaded DialogHost have an {nameof(DialogHost.Identifier)} property matching dialogIdentifier argument.", ex.Message);
110112
}
111113

112-
[StaFact(Timeout = 500)]
114+
[StaFact]
113115
public async Task WhenMultipleDialogHostsHaveTheSameIdentifierItThrows()
114116
{
115117
var id = Guid.NewGuid();
@@ -125,7 +127,7 @@ public async Task WhenMultipleDialogHostsHaveTheSameIdentifierItThrows()
125127
Assert.Equal("Multiple viable DialogHosts. Specify a unique Identifier on each DialogHost, especially where multiple Windows are a concern.", ex.Message);
126128
}
127129

128-
[StaFact(Timeout = 500)]
130+
[StaFact]
129131
public async Task WhenNoIdentifierIsSpecifiedItUsesSingleDialogHost()
130132
{
131133
bool isOpen = false;
@@ -138,12 +140,40 @@ public async Task WhenNoIdentifierIsSpecifiedItUsesSingleDialogHost()
138140
Assert.True(isOpen);
139141
}
140142

141-
[StaFact(Timeout = 500)]
143+
[StaFact]
142144
public async Task WhenContentIsNullItThrows()
143145
{
144146
var ex = await Assert.ThrowsAsync<ArgumentNullException>(() => DialogHost.Show(null));
145147

146148
Assert.Equal("content", ex.ParamName);
147149
}
150+
151+
[StaFact]
152+
[Description("Issue 1212")]
153+
public async Task WhenContentIsUpdatedClosingEventHandlerIsInvoked()
154+
{
155+
int closeInvokeCount = 0;
156+
void ClosingHandler(object s, DialogClosingEventArgs e)
157+
{
158+
closeInvokeCount++;
159+
if (closeInvokeCount == 1)
160+
{
161+
e.Cancel();
162+
}
163+
}
164+
165+
var dialogTask = DialogHost.Show("Content", ClosingHandler);
166+
_dialogHost.CurrentSession.Close("FirstResult");
167+
_dialogHost.CurrentSession.Close("SecondResult");
168+
object result = await dialogTask;
169+
170+
Assert.Equal("SecondResult", result);
171+
Assert.Equal(2, closeInvokeCount);
172+
}
173+
174+
private class TestDialog : Control
175+
{
176+
public void CloseDialog() => DialogHost.CloseDialogCommand.Execute(null, this);
177+
}
148178
}
149179
}

MaterialDesignThemes.Wpf.Tests/MaterialDesignThemes.Wpf.Tests.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
<Reference Include="PresentationFramework" />
3838
<Reference Include="System" />
3939
<Reference Include="System.Core" />
40+
<Reference Include="System.Xaml" />
4041
<Reference Include="System.Xml.Linq" />
4142
<Reference Include="System.Data.DataSetExtensions" />
4243
<Reference Include="Microsoft.CSharp" />

MaterialDesignThemes.Wpf/DialogHost.cs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -583,12 +583,16 @@ internal void Close(object parameter)
583583
DialogClosingCallback?.Invoke(this, dialogClosingEventArgs);
584584
_asyncShowClosingEventHandler?.Invoke(this, dialogClosingEventArgs);
585585

586-
_dialogTaskCompletionSource?.TrySetResult(parameter);
587-
586+
588587
if (!dialogClosingEventArgs.IsCancelled)
588+
{
589+
_dialogTaskCompletionSource?.TrySetResult(parameter);
589590
SetCurrentValue(IsOpenProperty, false);
591+
}
590592
else
593+
{
591594
CurrentSession.IsEnded = false;
595+
}
592596
}
593597

594598
/// <summary>

0 commit comments

Comments
 (0)