我正在开发一个Silverlight 3应用程序,并在尝试将对象添加到画布时发生这个奇怪的错误。我的代码如下:Children.Add(item)的值不在预期范围内
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,告诉我“值不在预期范围内”。
但是,在将子代添加到x_LayoutRoot.Children之前添加以下代码时,问题消失。
child.SetValue(Canvas.NameProperty, "child" + objCount++);
为什么会发生这种情况?这是一个Silverlight错误,或者(更可能)我错过了什么?
你可以使用FrameworkElement.NameProperty代替Canvas.NameProperty – 2011-06-02 17:47:12