C#WPF - 暂停调度程序处理时无法执行此操作

时间:2017-04-07 17:50:10

标签: c# wpf exception

我有ContentDialogHostContentDialog中显示Grid

ShowDialog()方法处理在主机中显示控件,并返回ContentDialogResult

我希望它像Windows对话框一样工作,你调用ShowDialog(),代码会停止,直到用户按下任何对话框按钮。

这是我的实施:

public ContentDialogResult ShowDialog(ContentDialog dialog)

        {
            // show host
            this.Visibility = Visibility.Visible;

            _dialogResult = null;

            if (_currentDialog == null)
            {
                containerGrid.Children.Add(dialog);
                _currentDialog = dialog;
            }

            dialog.PrimaryButtonClick += (s, ev) => { _dialogResult = ContentDialogResult.Primary; };
            dialog.SecondaryButtonClick += (s, ev) => { _dialogResult = ContentDialogResult.Secondary; };

            while (_dialogResult == null)
            {
                if (this.Dispatcher.HasShutdownStarted ||
                    this.Dispatcher.HasShutdownFinished)
                {
                    break;
                }

                // !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
                // THIS IS WHERE IT THROWS THE EXCEPTION BELOW
                // !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

                this.Dispatcher.Invoke(
                    DispatcherPriority.Background,
                    new ThreadStart(delegate { }));
                Thread.Sleep(20);
            }

            CloseDialog();

            return _dialogResult.Value;
        }

这包含在Popups类中,如下所示:

public ContentDialogHost.ContentDialogResult ShowDialog(ContentDialog dialog)
        {
            return mainwindow.contentdialogHost.ShowDialog(dialog);
        }

仅在从IsVisibleChanged事件调用ShowDialog()时发生异常,例如:

private void Page_IsVisibleChanged(object sender, DependencyPropertyChangedEventArgs e)
        {
            if (this.IsVisible)
                ShowSubviewDebugDialog(); // this calls `ShowDialog()` from the above mentioned Popups class
        }

我该怎么做才能解决或解决这个问题?

0 个答案:

没有答案