我有一个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 =真;
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();移到ContinueWith时,一切都开始工作了。
发布于 2015-08-07 06:16:27
问题在于从任务中触发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);
}https://stackoverflow.com/questions/19297431
复制相似问题