gpt4 book ai didi

c# - Children.Add(item) 值不在预期范围内

转载 作者:可可西里 更新时间:2023-11-01 08:35:31 25 4
gpt4 key购买 nike

我正在开发 Silverlight 3 应用程序,当我尝试将对象添加到 Canvas 时遇到这个非常奇怪的错误。我的代码如下:

for (int i = 0; i < person.Children.Count; i++)
{
//Add children in same position as parent
Person child = person.Children[i];
child.x_PositionTransform.X = person.x_PositionTransform.X;
child.x_PositionTransform.Y = person.x_PositionTransform.Y;
child.Click += new RoutedEventHandler(person_Click);
x_LayoutRoot.Children.Add(child);
}

我第一次使用它时,它按预期工作。但是,当我在单击使用此代码创建的 Person 对象后点击 x_LayoutRoot.Children.Add(child) 时,我收到一个 ArgumentException,告诉我“值不在预期范围内。”

但是,当我在将 child 添加到 x_LayoutRoot.Children 之前添加以下代码时,问题就消失了。

child.SetValue(Canvas.NameProperty, "child" + objCount++);

为什么会这样?这是 Silverlight 错误,还是(更有可能)我只是遗漏了什么?

最佳答案

我想我已经找到了原因:我添加了多个同名的 Person 对象。因此,如果将来有人遇到此问题,请确保您的所有对象都具有唯一的名称!

关于c# - Children.Add(item) 值不在预期范围内,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1214173/

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