只有在创建 Window 并显示为对话框后才能设置 DialogResult
我有一个 WPF 主窗口 mywindow.showDialog 当单击窗口上的按钮时,将执行命令 假设该命令是 SendToTableCommand
protected virtual void SendToTableExecute(object o)
{
UIThread.BeginInvoke(new Action<object>(SendToTableExecuteUI),o);
}
private void SendToTableExecuteUI(object o)
{
if (o is Control)
{
m_OwningWindow = UIHelper.FindVisualParent<Window>((Control)o);
}
do sth...
if (m_OwningWindow != null)
{
//only set DialogResult when window is ShowDialog before
if(System.Windows.Interop.ComponentDispatcher.IsThreadModal)
m_OwningWindow.DialogResult = true;
}
}
不久前,m_OwningWindow.DialogResult = true
抛出异常。所以我添加了一个使用 IsThreadModal 的 if 检查。它已经工作了一段时间,但现在 m_OwningWindow 没有关闭,因为 IsThreadModal 为 false。
我不知道解决问题的正确方法是什么,并且认为我没有正确处理它。 请帮忙。提前致谢
I have a main WPF window, mywindow.showDialog
when a button is clicked on the window, a command is executed
let's say the command is SendToTableCommand
protected virtual void SendToTableExecute(object o)
{
UIThread.BeginInvoke(new Action<object>(SendToTableExecuteUI),o);
}
private void SendToTableExecuteUI(object o)
{
if (o is Control)
{
m_OwningWindow = UIHelper.FindVisualParent<Window>((Control)o);
}
do sth...
if (m_OwningWindow != null)
{
//only set DialogResult when window is ShowDialog before
if(System.Windows.Interop.ComponentDispatcher.IsThreadModal)
m_OwningWindow.DialogResult = true;
}
}
Sometime ago, m_OwningWindow.DialogResult = true
throws exception. So I added an if check that uses IsThreadModal. It has worked for a while, but now m_OwningWindowdoes not close because IsThreadModal is false.
I do not know what's the right way to solve the issue and think I did not handle it properly.
Please help. thanks in advance
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
杰森的回复让我想起了一个解决方法。即使用Window.Close(),然后在窗口上添加一个bool类型属性,比如OKClicked,用window.Close()替换任何设置DialogResult的地方; window.OKClicked = true 或 false。将 window.DialogResult 的引用替换为 window.OKClicked。
解决方法有什么问题吗?谢谢
Jason's reply reminds me of a workaround. i.e. using Window.Close(), then add a bool type property on window, say OKClicked, replace anywhere that set DialogResult with window.Close(); window.OKClicked = true or false. replace reference to window.DialogResult with window.OKClicked.
Any problem with the workaround? thanks
在分配 DialogResult 之前我隐藏了窗口。交换顺序,以便在隐藏窗口之前分配 DialogResult ,解决了我的问题。即使窗口是
ShowDialog
'd,它也必须被视为“打开”才能设置DialogResult
。编辑:窗口应该关闭,而不是隐藏。我发帖后就被我咬了。
I was hiding my window before assigning
DialogResult
. Swapping the order, soDialogResult
is assigned before the window is hidden, fixed my problem. Even if the window wasShowDialog
'd, it must be considered "open" in order forDialogResult
to be set.Edit: And the window should be closed, not hidden. That bit me after I posted.
使用
Form.Modal
确定您的表单是作为窗口还是模式对话框打开。当您希望关闭表单时,您应该能够 Close() 表单,无论它是否是对话框。 (在某些情况下,您可能还需要在关闭后处理它)
此外,DialogResult 是一个枚举类型 -
true
不是我希望看到分配给它的值。通常会使用DialogResult.OK
或DialogResult.Yes
来实现此目的。Use
Form.Modal
to determine if your form is being opened as a window or a modal dialog.You should be able to Close() the form when you want it to close, regardless of whether it's a dialog or not. (Under certain circumstances you may also need to Dispose it after closing)
Also, DialogResult is an enumerated type -
true
isn't a value I'd expect to see being assigned to it. TypicallyDialogResult.OK
orDialogResult.Yes
would be used for this.