gpt4 book ai didi

.net - WPF 4.5 父子行为是否改变 : we can now add a child to another parent without exception?

转载 作者:行者123 更新时间:2023-12-04 01:12:54 24 4
gpt4 key购买 nike

在 .Net 4.0 中,以下代码抛出 InvalidOperationException带有消息“指定的元素已经是另一个元素的逻辑子元素。首先断开它。”

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

但是,在安装了 .Net 4.5 的机器上运行此代码不会引发异常。这似乎导致可视化树有一些奇怪的状态,显示为不正确的 UI。

为什么没有异常(exception)? throw声明似乎仍然存在于 .Net 4.5 FrameworkElement.AddLogicalChild .什么会导致它不被抛出?

我很高兴接受行为改变是有充分理由的,我必须改变我的编码,但是,就目前而言,当显式断开一个 FrameworkElement从逻辑树被遗忘了。

最佳答案

有时,一个 child 拥有多个逻辑父节点是有意义的,例如在 layout-to-layout animation 中。 .我猜 WPF 团队决定是时候让开发人员决定何时以及如何使用它而不是禁止它。

关于.net - WPF 4.5 父子行为是否改变 : we can now add a child to another parent without exception?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12270603/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com