如何让主窗口等待新打开的窗口在 C# WPF 中关闭? [英] How to make main window wait until a newly opened window closes in C# WPF?
问题描述
我是 WPF 和 C# 的新手,请耐心等待.
I am new to WPF as well as C#, please bear with me.
我有一个主窗口,可以打开一个新窗口.现在这个新窗口是提示是否覆盖文件,主窗口访问新窗口中的一个公共变量来检查提示结果.
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";
}
我尝试在主窗口中添加一个 while 循环来检查另一个公共布尔值,但这只会挂起整个程序.
I tried adding a while loop checking another public boolean in the main window, but that just hangs the entire program.
while(!_closecheck);
欢迎提出任何建议.
推荐答案
使用 ShowDialog 而不是 Show
-
Win.ShowDialog();
来自 MSDN -
打开一个窗口,只有当新打开的窗口被打开时才返回关闭.
Opens a window and returns only when the newly opened window is closed.
这篇关于如何让主窗口等待新打开的窗口在 C# WPF 中关闭?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!