Children.Add(项目)的价值并不在预期范围内 [英] Children.Add(item) value does not fall within the expected range
问题描述
我正在开发一个Silverlight 3应用程序,并得到这个非常奇怪的错误,当我尝试将对象添加到画布。我的code是如下:
I'm developing a Silverlight 3 app and getting this really weird error when I try to add an object to a Canvas. My code is as follows:
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);
}
我第一次用这个,它工作正常。然而,当我点击使用这个code创建的Person对象后打x_LayoutRoot.Children.Add(孩子),我得到一个ArgumentException告诉我,值没有在预期范围之内。
The first time I use this, it works as expected. However, when I hit x_LayoutRoot.Children.Add(child) after clicking a Person object that was created using this code, I get an ArgumentException telling me that "Value does not fall within the expected range."
然而,当我将孩子x_LayoutRoot.Children前添加以下code,问题消失。
However, when I add the following code before adding child to x_LayoutRoot.Children, the problem disappears.
child.SetValue(Canvas.NameProperty, "child" + objCount++);
这是怎么回事?这是一个Silverlight的bug,或者(更有可能)我只是失去了一些东西?
Why is this happening? Is this a Silverlight bug, or (more likely) am I just missing something?
推荐答案
我想我已经想通了这个原因:我将用相同名称的多个Person对象。因此,如果任何人有这个问题,在未来,确保所有对象有唯一的名字!
I think I've figured out the cause of this: I was adding multiple Person objects with the same name. So, if anyone has this issue in the future, make sure all of your objects have unique names!
这篇关于Children.Add(项目)的价值并不在预期范围内的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!