只有在创建 Window 并显示为对话框后才能设置 DialogResult

发布于 2024-11-05 03:19:03 字数 852 浏览 7 评论 0原文

我有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

锦上情书 2024-11-12 03:19:03

杰森的回复让我想起了一个解决方法。即使用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

无法言说的痛 2024-11-12 03:19:03

在分配 DialogResult 之前我隐藏了窗口。交换顺序,以便在隐藏窗口之前分配 DialogResult ,解决了我的问题。即使窗口是 ShowDialog'd,它也必须被视为“打开”才能设置 DialogResult

编辑:窗口应该关闭,而不是隐藏。我发帖后就被我咬了。

I was hiding my window before assigning DialogResult. Swapping the order, so DialogResult is assigned before the window is hidden, fixed my problem. Even if the window was ShowDialog'd, it must be considered "open" in order for DialogResult to be set.

Edit: And the window should be closed, not hidden. That bit me after I posted.

轻拂→两袖风尘 2024-11-12 03:19:03

使用 Form.Modal 确定您的表单是作为窗口还是模式对话框打开。

当您希望关闭表单时,您应该能够 Close() 表单,无论它是否是对话框。 (在某些情况下,您可能还需要在关闭后处理它)

此外,DialogResult 是一个枚举类型 - true 不是我希望看到分配给它的值。通常会使用 DialogResult.OKDialogResult.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. Typically DialogResult.OK or DialogResult.Yes would be used for this.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文