如何让主窗口等待新打开的窗口在 C# WPF 中关闭?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20540139/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to make main window wait until a newly opened window closes in C# WPF?
提问by Arjun Ajith
I am new to WPF as well as C#, please bear with me.
我是 WPF 和 C# 的新手,请耐心等待。
I have a main window which opens up a new window. Now this new window is a prompt whether or not to overwrite a file, and the main window accesses a public variable in the new window to check for the prompt's result.
我有一个主窗口,可以打开一个新窗口。现在这个新窗口是一个是否覆盖文件的提示,主窗口访问新窗口中的一个公共变量来检查提示的结果。
But I can't get the main window processing to wait until the new window closes.
但是我无法让主窗口处理等到新窗口关闭。
Window1 Win = new Window1();
Win.Show();
if (Win.pr_res == 1)
{
abc.Text = "File to be overwritten";
File.Delete(_destination);
Start();
}
else
{
abc.Text = "Operation Aborted";
}
I tried adding a while loop checking another public boolean in the main window, but that just hangs the entire program.
我尝试在主窗口中添加一个 while 循环检查另一个公共布尔值,但这只会挂起整个程序。
while(!_closecheck);
Any suggestions are welcome.
欢迎任何建议。
回答by Rohit Vats
Use ShowDialoginstead of Show-
使用ShowDialog而不是Show-
Win.ShowDialog();
From MSDN-
从MSDN-
Opens a window and returns only when the newly opened window is closed.
打开一个窗口并仅在新打开的窗口关闭时返回。
回答by A.K.
Use ShowDialog() method as it Opens a window and returns only when the newly opened window is closed.
使用 ShowDialog() 方法,因为它打开一个窗口并仅在新打开的窗口关闭时返回。
syntax
句法
Win.ShowDialog();
回答by JeffRSon
Although ShowDialogworks fine, you may set MainWindow.IsEnabled = falseif you don't like that modal window. Sometimes it's useful to see the main window.
虽然ShowDialog工作正常,但MainWindow.IsEnabled = false如果您不喜欢该模式窗口,您可以进行设置。有时查看主窗口很有用。
回答by MoisgObg
Instead of using the .Show() property for openning/showing the window, use .ShowDialog() instead :)
不要使用 .Show() 属性打开/显示窗口,而是使用 .ShowDialog() :)

