olt*_*man 11 .net c# silverlight
我正在开发一个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);
}
Run Code Online (Sandbox Code Playgroud)
我第一次使用它,它按预期工作.但是,当我在单击使用此代码创建的Person对象后点击x_LayoutRoot.Children.Add(child)时,我收到一个ArgumentException,告诉我"Value不在预期范围内".
但是,当我在将child添加到x_LayoutRoot.Children之前添加以下代码时,问题就会消失.
child.SetValue(Canvas.NameProperty, "child" + objCount++);
Run Code Online (Sandbox Code Playgroud)
为什么会这样?这是一个Silverlight错误,还是(更有可能)我错过了什么?