How to make main window wait until a newly opened window closes in C# WPF?

喜欢而已 提交于 2019-12-21 12:18:54

问题


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";
 }

I tried adding a while loop checking another public boolean in the main window, but that just hangs the entire program.

 while(!_closecheck);

Any suggestions are welcome.


回答1:


Use ShowDialog instead of Show -

Win.ShowDialog();

From MSDN -

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




回答2:


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

syntax

Win.ShowDialog();



回答3:


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.




回答4:


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



来源:https://stackoverflow.com/questions/20540139/how-to-make-main-window-wait-until-a-newly-opened-window-closes-in-c-sharp-wpf

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!