我看到奇怪的行为。我有强烈的感觉,它是.net工具链中的一个错误。连接propery的奇怪的WPF错误
重现步骤:
- 创建WPF应用程序。
- 在该解决方案中创建类库。
- 在类库中定义一些公共类(可以为空)。
- 在WPF应用程序中定义一些具有附加属性和Window作为目标类的类。
- 它附加到窗口,并提供价值
,你会得到错误:
The object 'Window' already has a child and cannot add ''. 'Window' can accept only one child.
如果你改变结构,使在同一装配附加属性和价值类(这个类可以是类库或Wpf应用程序)它工作正常。
如果你把内容后的代码也工作正常。这给了错误:
<Window x:Class="WpfApplication9.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:my="clr-namespace:ClassLibrary1;assembly=ClassLibrary1"
xmlns:my2="clr-namespace:ClassLibrary1"
Title="MainWindow" Height="350" Width="525">
<my2:Property.MyProperty>
<my:ValueClass />
</my2:Property.MyProperty>
<Grid>
</Grid>
</Window>
这不:
<Grid>
</Grid>
<my2:Property.MyProperty>
<my:ValueClass />
</my2:Property.MyProperty>
任何想法?
谢谢!
@Rick Sladkey我没有清楚地理解第一段,你能稍微扩展一下吗?此外,附加的属性与XAML所在的组件相同。该值位于另一个属性中。 – Andrey 2011-04-21 18:21:04
@安德雷:当然。我在答案中增加了一些。 – 2011-04-21 18:59:36
@Rick Sladkey谢谢,非常有趣。但是按照你的解释,我不明白为什么它在两个类中都有效:一个是属性,一个是用于XAML所在的同一个程序集的值。 – Andrey 2011-04-21 19:03:15