难道WPF 4.5亲子行为改变:我们孩子现在添加到另一个父无例外? [英] Did WPF 4.5 parent-child behavior change: we can now add a child to another parent without exception?

查看:240
本文介绍了难道WPF 4.5亲子行为改变:我们孩子现在添加到另一个父无例外?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在.NET 4.0中以下code抛出 InvalidOperationException异常的消息指定的元素已经是另一个元素的逻辑子,先断开它。

In .Net 4.0 the following code throws an InvalidOperationException with the message "Specified element is already the logical child of another element. Disconnect it first."

var parent = new System.Windows.Controls.ContentControl();
var child = new System.Windows.Controls.Button();

parent.Content = child;

var parent2 = new System.Windows.Controls.ContentControl();
parent2.Content = child;    // throws InvalidOperationException in .Net 4.0, not in 4.5

但是,在运行的机器上这个code与.net 4.5安装导致的没有的异常被抛出。这似乎使可视树有一些奇怪的状态,显示为一个不正确的用户界面。

However, running this code on a machine with .Net 4.5 installed results in no exception being thrown. This appears to cause the visual tree to have some strange state which shows up as an incorrect UI.

为什么不例外吗抛出语句似乎仍然present在.net 4.5 FrameworkElement的。 AddLogicalChild 。什么会导致它被不扔?

Why no exception? The throw statement appears to be still present in .Net 4.5 FrameworkElement.AddLogicalChild. What would cause it to be not thrown?

我很高兴接受的行为改变了一个很好的理由,我不得不改变我的编码,但是,因为它的立场,沉默的失败,腐败的UI看起来像从强异常退步时明确的断线 FrameworkElement的从逻辑树福尔戈。

I'm happy to accept that the behavior changed for a good reason, and I have to change my coding, however, as it stands, the silent fail with corrupt UI seems like a step backward from the strong exception when the explicit disconnection of a FrameworkElement from the logical tree was forgotton.

推荐答案

有些时候,它是有道理的孩子有一个以上的逻辑父,例如在的布局对布局动画。我猜WPF团队决定是时候让开发人员决定何时以及如何使用,而不是不允许它这样。

There are times when it makes sense for a child to have more than one logical parent, for instance in layout-to-layout animation. I'm guessing the WPF team decided it was time to let developers decide when and how to use this instead of disallowing it.

这篇关于难道WPF 4.5亲子行为改变:我们孩子现在添加到另一个父无例外?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆