wpf 打开窗口(或对话框)以阻止主窗口而不停止进一步的指令

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12332221/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 05:16:28  来源:igfitidea点击:

Open window (or dialog) to block main window without stopping further instructions

c#wpfdialog

提问by philkark

I have a MainWindowthat will with the click of the button do some calculations that might take a few seconds. During that time I would like to show a little dialog (or separate window) saying "Please wait..." in the middle of MainWindowthat will fully block it, so the user is not able to do anything until this dialog is closed. I tried creating a window and opening it with window.ShowDialog();but that will of course not continue the instructions...

我有一个MainWindow点击按钮会做一些可能需要几秒钟的计算。在那段时间里,我想显示一个小对话框(或单独的窗口),中间说“请稍候...”MainWindow将完全阻止它,因此在关闭此对话框之前用户无法执行任何操作。我尝试创建一个窗口并打开它,window.ShowDialog();但这当然不会继续说明......

Is there any other way that will work?

有没有其他可行的方法?

回答by zmilojko

The solution is very straightforward - you should set your MainForm as a parent in the Show (not ShowDialog, as you noticed), and then call Enabled = false on your own form:

解决方案非常简单 - 您应该将 MainForm 设置为 Show 中的父级(不是 ShowDialog,正如您所注意到的),然后在您自己的表单上调用 Enabled = false:

        Form2 f = new Form2();
        f.Show(this);
        this.Enabled = false;

        //do your stuff here

        f.Hide();
        this.Enabled = true;
        f.Dispose();
        f = null;

The only problem here is that you cannot cancel your process... But if your calculation is in another thread, the other form's controls will trigger their handlers and you can cancel the work in the other thread.

这里唯一的问题是你不能取消你的进程......但是如果你的计算在另一个线程中,另一个表单的控件将触发它们的处理程序,你可以取消另一个线程中的工作。

回答by H.B.

How about starting the calculations on another thread before opening the window?

在打开窗口之前在另一个线程上开始计算怎么样?

You could possibly also create a new DispatcherFramefor your calculations.

您也可以DispatcherFrame为您的计算创建一个新的。

Or show a Popupand set IsEnabledon a control close to the root to false.

或者在靠近根的控件上显示 aPopup和 setIsEnabledfalse

Or move your code to an event on the dialog, like Loaded.

或者将您的代码移动到对话框上的事件,例如Loaded.

回答by Aghilas Yakoub

You can try with this code

您可以尝试使用此代码

Without blocking

无阻塞

In your code

在你的代码中

    ....
    Thread newThread = new Thread(new ThreadStart(Create));
    newThread.SetApartmentState(ApartmentState.STA);
    newThread.IsBackground = true;
    newThread.Start();
    ....
}

private void Create()
{
    Window1 tempWindow = new Window1();
    tempWindow.Show();       
    System.Windows.Threading.Dispatcher.Run();
}

Blocking your main window

阻止您的主窗口

You can also use Prismframework, and use InteractionRequestclass, in order to generate your windows

您还可以使用Prism框架和InteractionRequest类,以生成您的窗口

http://msdn.microsoft.com/en-us/library/gg405494%28v=pandp.40%29.aspx

http://msdn.microsoft.com/en-us/library/gg405494%28v=pandp.40%29.aspx