1

我有一个showDialog。在第一次加载 showDialog 后,我从 viewModel 调用另一个 showDialog:

var connectionWindow = new ConnectionWindow();
connectionWindow.Closed += (o, args) =>
{
    if (connectionWindow.DialogResult.HasValue && connectionWindow.DialogResult.Value)
    {
    }
    else
    {
    }
};
connectionWindow.ShowDialog();

在第二个显示对话框中,我试图通过以下方式关闭它:

 public ConnectionWindow()
 {
     _viewModel = new ConnectionWindowViewModel();
     InitializeComponent();
     DataContext = _viewModel;
     _viewModel.ClosingRequest += () =>
                                  {
                                      DialogResult = true;
                                      Close();
                                  };
 }

当关闭请求发生时,执行中断就行

对话结果=真;

VS中没有错误或其他任何东西,但它没有关闭第二个showDialog窗口。关于如何关闭第二个对话框的任何想法?

编辑 我想通过单击按钮关闭第二个 showDialog。这是我的按钮的 xaml 代码:

<Button Content="Save Connection" Width="108" IsDefault="True" Command="{Binding OnSaveConnectionCommand}">

Edit2 问题在于从任务中触发 OnClosingRequest:

private void OnSaveConnection(object sender)
{
    Mouse.OverrideCursor = Cursors.Wait;
    var uiScheduler = TaskScheduler.FromCurrentSynchronizationContext();
    Task.Factory.StartNew(() =>
                          {
                              if (IsServerConnected(_entityBuilder.ToString()))
                              {
                                   OnClosingRequest();      
                              }
                              else
                              {
                                  MessageBox.Show("Cannot establish connection with server " , Properties.Resources.Error, MessageBoxButton.OK, MessageBoxImage.Error);
                              }
                          }).ContinueWith(_ => {Mouse.OverrideCursor = Cursors.Arrow;}, uiScheduler);
    }

当我移动 OnClosingRequest(); 继续,一切开始工作。

4

1 回答 1

0

问题在于从任务中触发 OnClosingRequest:

private void OnSaveConnection(object sender)
{
    Mouse.OverrideCursor = Cursors.Wait;
    var uiScheduler = TaskScheduler.FromCurrentSynchronizationContext();
    Task.Factory.StartNew(() =>
                          {
                              if (IsServerConnected(_entityBuilder.ToString()))
                              {
                                   OnClosingRequest();      
                              }
                              else
                              {
                                  MessageBox.Show("Cannot establish connection with server " , Properties.Resources.Error, MessageBoxButton.OK, MessageBoxImage.Error);
                              }
                          }).ContinueWith(_ => {Mouse.OverrideCursor = Cursors.Arrow;}, uiScheduler);
    }
于 2015-08-07T06:16:27.663 回答