Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# WPF在bool更改时是否关闭对话框?_C#_Wpf_Dialog - Fatal编程技术网

C# WPF在bool更改时是否关闭对话框?

C# WPF在bool更改时是否关闭对话框?,c#,wpf,dialog,C#,Wpf,Dialog,我在一个C#WPF应用程序中有一些简单的代码,使用它会弹出一个请稍候类型的对话框 我想要的是,当布尔值更改时(从后台线程),对话框自动关闭 我有: //create dialog var view = new Dialog { DataContext = new Domain.DialogViewModel("Please wait...", true) }; //show var result = await DialogHost.Show(view, "RootDialog", C

我在一个C#WPF应用程序中有一些简单的代码,使用它会弹出一个
请稍候
类型的对话框

我想要的是,当布尔值更改时(从后台线程),对话框自动关闭

我有:

//create dialog
var view = new Dialog
{
    DataContext = new Domain.DialogViewModel("Please wait...", true)
};

//show
var result = await DialogHost.Show(view, "RootDialog", ClosingEventHandler);


//wait
while (finished == false)
{

}

//close dialog, or enable the 'ok' button on it.

DialogHost.CloseDialogCommand.Execute(view, view); //does not work.

如何从代码关闭对话框?

我不熟悉您正在使用的DialogHost功能,但是您的问题非常简单。您将有一个在后台线程上运行的任务,然后在关闭等待对话框时切换回UI线程

using System;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;

...

void StartLengthyTask() {
    var dlg = CreateWaitDialog();
    dlg.Show();

    // run lengthy task in a background thread
    Task.Run(new Action(BackgroundThread))

        // switch back to UI thread when finished
        .ConfigureAwait(true)
        .GetAwaiter()

        // close the wait dialog
        .OnCompleted(() => dlg.Close());

  // logic here will execute immediately without waiting on the background task
}

void BackgroundTask() {
    Thread.Sleep(5000);
}

啊,我需要的是覆盖
OpenedEventHandler
。使用此选项:

 var view = new Dialog
            {
                DataContext = new Domain.DialogViewModel("")
            };

            //show the dialog
            var result = await DialogHost.Show(view, "RootDialog", ExtendedOpenedEventHandler, ExtendedClosingEventHandler);
我可以在函数中获取会话,例如:

   private void ExtendedOpenedEventHandler(object sender, DialogOpenedEventArgs eventArgs)
        {

            //...now, lets update the "session" with some new content!
            eventArgs.Session.UpdateContent(new ProgressDialog());
            //note, you can also grab the session when the dialog opens via the DialogOpenedEventHandler



            //lets run a fake operation for 3 seconds then close this baby.
            Task.Delay(TimeSpan.FromSeconds(3))
                .ContinueWith((t, _) => eventArgs.Session.Close(false), null,
                   TaskScheduler.FromCurrentSynchronizationContext());
        }
然后在我希望使用时关闭对话框:

eventArgs.Session.Close(false)