无法关闭showDialog

时间:2013-10-10 13:41:18

标签: c# mvvm

我有一个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();
                                  };
 }

当closingRequest发生时,执行会在行

上中断
  

DialogResult = true;

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();一切都开始有效了。

1 个答案:

答案 0 :(得分: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);
    }