无法将 Owner 属性设置为已关闭的窗口:异常

发布于 2024-12-04 16:28:05 字数 804 浏览 1 评论 0原文

我使用 WPF 4.0 amd MVVM LIght ToolKit,我有以下代码:

public partial class View1: Window
{
    /// <summary>
    /// Initializes a new instance of the FavoritesView class.
    /// </summary>
    public View1()
    {
        InitializeComponent();
    }

    private void Window_Loaded(object sender, RoutedEventArgs e)
    {    
        Messenger.Default.Register<NotificationMessage>(this,
            (msg) =>
            {
                if (msg.Notification == "OpenDocument")
                {
                    DocumentView view = new DocumentView();
                    view.Owner=this;
                    view.ShowDialog();
                }
            });            
    }
}

当我多次打开-关闭 DocumentView 窗口时,我收到异常“无法将所有者属性设置为已关闭的窗口”。为什么?有什么想法吗?

I use WPF 4.0 amd MVVM LIght ToolKit, i have following code:

public partial class View1: Window
{
    /// <summary>
    /// Initializes a new instance of the FavoritesView class.
    /// </summary>
    public View1()
    {
        InitializeComponent();
    }

    private void Window_Loaded(object sender, RoutedEventArgs e)
    {    
        Messenger.Default.Register<NotificationMessage>(this,
            (msg) =>
            {
                if (msg.Notification == "OpenDocument")
                {
                    DocumentView view = new DocumentView();
                    view.Owner=this;
                    view.ShowDialog();
                }
            });            
    }
}

When i many times open-close DocumentView window i get exception "Cannot set Owner property to a Window that has been closed". Why? Any ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

人│生佛魔见 2024-12-11 16:28:05

您可以尝试从NotificationMessage中注销以避免将来执行。

Messenger.Default.Unregister(this);

You could try to unregister from NotificationMessage to avoid future executions.

Messenger.Default.Unregister(this);
失去的东西太少 2024-12-11 16:28:05

您需要从窗口关闭事件的消息中取消注册。
这将确保创建新实例时不会发生重复注册。

private void Window_Closed(object sender, RoutedEventArgs e)
{ 
   Messenger.Default.UnRegister<NotificationMessage>(this);
}

you need to Unregister from the message on the window closed event.
This will make sure that duplicate registration does not happen when a new instance is created.

private void Window_Closed(object sender, RoutedEventArgs e)
{ 
   Messenger.Default.UnRegister<NotificationMessage>(this);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文