gpt4 book ai didi

c# - xaml 中带有集合的嵌套对象似乎会泄漏到相邻的集合中

转载 作者:可可西里 更新时间:2023-11-01 11:51:12 26 4
gpt4 key购买 nike

当我定义一个对象集合时,每个对象都包含这样一个集合:

<Graph2:NestedObjectTest x:Key="nestedTest">
<Graph2:NestedObjectTest.Children>
<Graph2:ChildNestedObjectTest>
<Graph2:ChildNestedObjectTest.Children>
<Point X="0" Y="0"/>
<Point X="10" Y="10"/>
<Point X="20" Y="20"/>
</Graph2:ChildNestedObjectTest.Children>
</Graph2:ChildNestedObjectTest>
<Graph2:ChildNestedObjectTest>
<Graph2:ChildNestedObjectTest.Children>
<Point X="1" Y="0"/>
<Point X="11" Y="10"/>
<Point X="21" Y="20"/>
</Graph2:ChildNestedObjectTest.Children>
</Graph2:ChildNestedObjectTest>
<Graph2:ChildNestedObjectTest>
<Graph2:ChildNestedObjectTest.Children>
<Point X="2" Y="0"/>
<Point X="12" Y="10"/>
<Point X="22" Y="20"/>
</Graph2:ChildNestedObjectTest.Children>
</Graph2:ChildNestedObjectTest>
</Graph2:NestedObjectTest.Children>
</Graph2:NestedObjectTest>

每个 ChildNestedObjectTest 对象都获得所有九个 Point 对象,而不是每个对象都获得它们看起来应该拥有的三个对象。这是为什么?

后面的代码是:

namespace Graph2
{
public class NestedObjectTest : DependencyObject
{
public static readonly DependencyProperty ChildrenProperty = DependencyProperty.Register("Children", typeof(List<ChildNestedObjectTest>), typeof(NestedObjectTest), new FrameworkPropertyMetadata(new List<ChildNestedObjectTest>()));
public List<ChildNestedObjectTest> Children
{
get { return (List<ChildNestedObjectTest>)GetValue(ChildrenProperty); }
set { SetValue(ChildrenProperty, value); }
}
}
public class ChildNestedObjectTest : DependencyObject
{
public static readonly DependencyProperty ChildrenProperty = DependencyProperty.Register("Children", typeof(List<Point>), typeof(ChildNestedObjectTest), new FrameworkPropertyMetadata(new List<Point>()));
public List<Point> Children
{
get { return (List<Point>)GetValue(ChildrenProperty); }
set { SetValue(ChildrenProperty, value); }
}
}
}

我通过将 xaml 作为资源添加到窗口进行测试,然后使用:

Graph2.NestedObjectTest test = (Graph2.NestedObjectTest)this.Resources["nestedTest"];

最佳答案

Collection-Type Dependency Properties

总结

If your property is a reference type, the default value specified in dependency property metadata is not a default value per instance; instead it is a default value that applies to all instances of the type.

看起来像您在元数据中设置的默认静态值

 new FrameworkPropertyMetadata(new List<Point>(),
^
---- This

无论何时都被重新使用,封闭的泛型是另一个 DependencyObject/Freezable

修复

To correct this problem, you must reset the collection dependency property value to a unique instance, as part of the class constructor call. Because the property is a read-only dependency property, you use the SetValue(DependencyPropertyKey, Object) method to set it, using the DependencyPropertyKey that is only accessible within the class.

public NestedObjectTest() : base()
{
SetValue(ChildrenProperty, new List<ChildNestedObjectTest>());
}

public ChildNestedObjectTest() : base()
{
SetValue(ChildrenProperty, new List<Point>());
}

关于c# - xaml 中带有集合的嵌套对象似乎会泄漏到相邻的集合中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29267168/

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