如何使主窗口等到C#WPF中新打开的窗口关闭?

时间:2021-07-10 15:12:54

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.

欢迎任何建议。

4 个解决方案

#1


26  

Use ShowDialog instead of Show -

使用ShowDialog而不是Show -

Win.ShowDialog();

From MSDN -

来自MSDN -

Opens a window and returns only when the newly opened window is closed.

打开一个窗口,仅在新打开的窗口关闭时返回。

#2


4  

Use ShowDialog() method as it Opens a window and returns only when the newly opened window is closed.

使用ShowDialog()方法打开一个窗口,仅在新打开的窗口关闭时返回。

syntax

句法

Win.ShowDialog();

#3


2  

Although ShowDialog works fine, you may set MainWindow.IsEnabled = false if you don't like that modal window. Sometimes it's useful to see the main window.

虽然ShowDialog工作正常,但如果你不喜欢那个模态窗口,可以设置MainWindow.IsEnabled = false。有时看到主窗口很有用。

#4


0  

Instead of using the .Show() property for openning/showing the window, use .ShowDialog() instead :)

而不是使用.Show()属性来打开/显示窗口,而是使用.ShowDialog()代替:)

#1


26  

Use ShowDialog instead of Show -

使用ShowDialog而不是Show -

Win.ShowDialog();

From MSDN -

来自MSDN -

Opens a window and returns only when the newly opened window is closed.

打开一个窗口,仅在新打开的窗口关闭时返回。

#2


4  

Use ShowDialog() method as it Opens a window and returns only when the newly opened window is closed.

使用ShowDialog()方法打开一个窗口,仅在新打开的窗口关闭时返回。

syntax

句法

Win.ShowDialog();

#3


2  

Although ShowDialog works fine, you may set MainWindow.IsEnabled = false if you don't like that modal window. Sometimes it's useful to see the main window.

虽然ShowDialog工作正常,但如果你不喜欢那个模态窗口,可以设置MainWindow.IsEnabled = false。有时看到主窗口很有用。

#4


0  

Instead of using the .Show() property for openning/showing the window, use .ShowDialog() instead :)

而不是使用.Show()属性来打开/显示窗口,而是使用.ShowDialog()代替:)