无法将 Owner 属性设置为已关闭的窗口:异常
我使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以尝试从NotificationMessage中注销以避免将来执行。
You could try to unregister from NotificationMessage to avoid future executions.
您需要从窗口关闭事件的消息中取消注册。
这将确保创建新实例时不会发生重复注册。
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.