diff --git a/src/Avalonia.Controls/Window.cs b/src/Avalonia.Controls/Window.cs index c8e09b8f9c..e40e114769 100644 --- a/src/Avalonia.Controls/Window.cs +++ b/src/Avalonia.Controls/Window.cs @@ -371,8 +371,16 @@ namespace Avalonia.Controls /// /// Shows the window. /// + /// + /// The window has already been closed. + /// public override void Show() { + if (PlatformImpl == null) + { + throw new InvalidOperationException("Cannot re-show a closed window."); + } + if (IsVisible) { return; @@ -397,6 +405,9 @@ namespace Avalonia.Controls /// Shows the window as a dialog. /// /// The dialog's owner window. + /// + /// The window has already been closed. + /// /// /// A task that can be used to track the lifetime of the dialog. /// diff --git a/tests/Avalonia.Controls.UnitTests/WindowTests.cs b/tests/Avalonia.Controls.UnitTests/WindowTests.cs index 8221dadc86..ee416c4cb0 100644 --- a/tests/Avalonia.Controls.UnitTests/WindowTests.cs +++ b/tests/Avalonia.Controls.UnitTests/WindowTests.cs @@ -292,6 +292,51 @@ namespace Avalonia.Controls.UnitTests } } + [Fact] + public void Calling_Show_On_Closed_Window_Should_Throw() + { + using (UnitTestApplication.Start(TestServices.StyledWindow)) + { + var windowImpl = Mock.Of(x => x.Scaling == 1); + var target = new Window(windowImpl); + + target.Show(); + target.Close(); + + var openedRaised = false; + target.Opened += (s, e) => openedRaised = true; + + var ex = Assert.Throws(() => target.Show()); + Assert.Equal("Cannot re-show a closed window.", ex.Message); + Assert.False(openedRaised); + } + } + + [Fact] + public async Task Calling_ShowDialog_On_Closed_Window_Should_Throw() + { + using (UnitTestApplication.Start(TestServices.StyledWindow)) + { + var parent = new Mock(); + var windowImpl = new Mock(); + windowImpl.SetupProperty(x => x.Closed); + windowImpl.Setup(x => x.Scaling).Returns(1); + + var target = new Window(windowImpl.Object); + var task = target.ShowDialog(parent.Object); + + windowImpl.Object.Closed(); + await task; + + var openedRaised = false; + target.Opened += (s, e) => openedRaised = true; + + var ex = await Assert.ThrowsAsync(() => target.ShowDialog(parent.Object)); + Assert.Equal("Cannot re-show a closed window.", ex.Message); + Assert.False(openedRaised); + } + } + [Fact] public void Window_Should_Be_Centered_When_WindowStartupLocation_Is_CenterScreen() {