“元素已经是另一个元素的子元素".为什么会收到此错误消息? [英] "Element is already the child of another element" Why am I getting this error message?
问题描述
我正在使用Windows Phone 7应用程序,并且有一种方法
I am working on a Windows Phone 7 application and I have a method
public void PaintButtons()
{
IList p1numlist = GenerateRandom();
IList p2numlist = GenerateRandom();
p1b1.Content = images.ElementAt((int)p1numlist[0]);
p1b2.Content = images.ElementAt((int)p1numlist[1]);
p1b3.Content = images.ElementAt((int)p1numlist[2]);
p2b1.Content = images.ElementAt((int)p2numlist[0]); //This is the line where the exception is generated. Exception: System.InvalidOperationException, Message: Element is already the child of another element
p2b2.Content = images.ElementAt((int)p2numlist[1]);
p2b3.Content = images.ElementAt((int)p2numlist[2]);
//p1b1,p1b2,p1b3,p2b1,p2b2,p2b3 are buttons
//GenerateRandom() method returns a List of unique random numbers of size 3
}
// I am adding images to the <code>images</code> collection using the following code
IList<Image> images = new List<Image>();
public Page1()
{
InitializeComponent();
LoadImages();
}
public void LoadImages()
{
Uri[] uri = new Uri[3];
uri[0] = new Uri("/BtnPics/Blue.png", UriKind.Relative);
uri[1] = new Uri("/BtnPics/Red.png", UriKind.Relative);
uri[2] = new Uri("/BtnPics/Yellow.png", UriKind.Relative);
foreach (Uri u in uri)
{
BitmapImage bmp = new BitmapImage(u);
images.Add(new Image() { Source = bmp });
}
}
我现在被困住了,请帮忙. XAML内容如下:
I am stuck at this point, please help. The XAML content is as follows:
<grid height="800" name="LayoutRoot" width="478">
<grid.columndefinitions>
<columndefinition width="65*" />
<columndefinition width="413*" />
</grid.columndefinitions>
<Button Height="122" HorizontalAlignment="Left" Margin="-12,682,0,0" Name="p1b1" VerticalAlignment="Top" Width="170" Grid.ColumnSpan="2" TabIndex="3" />
<Button Height="122" HorizontalAlignment="Left" Margin="85,682,0,0" Name="p1b2" VerticalAlignment="Top" Width="170" Grid.Column="1" TabIndex="2" />
<Button Height="122" HorizontalAlignment="Left" Margin="248,682,0,0" Name="p1b3" VerticalAlignment="Top" Width="170" Grid.Column="1" TabIndex="1" />
<textblock height="68" horizontalalignment="Left" margin="60,618,0,0" name="player1textBlock" text="TextBlock" verticalalignment="Top" width="223" grid.column="1" />
<textblock height="68" horizontalalignment="Left" margin="61,116,0,0" name="player2textBlock" text="TextBlock" verticalalignment="Top" width="223" grid.column="1" />
<Image Height="220" HorizontalAlignment="Left" Margin="45,260,0,0" Name="image1" Stretch="Fill" VerticalAlignment="Top" Width="250" Grid.Column="1" />
<Button Grid.ColumnSpan="2" Height="122" HorizontalAlignment="Left" Margin="-12,-4,0,0" Name="p2b3" TabIndex="3" VerticalAlignment="Top" Width="170" />
<Button Grid.Column="1" Height="122" HorizontalAlignment="Right" Margin="0,-4,158,0" Name="p2b2" TabIndex="2" VerticalAlignment="Top" Width="170" />
<Button Grid.Column="1" Height="122" HorizontalAlignment="Left" Margin="248,-4,0,0" Name="p2b1" TabIndex="1" VerticalAlignment="Top" Width="170" />
</grid>
有人可以帮忙吗?
Can somebody help?
推荐答案
我意识到,
images
是FrameworkElement
s(Image
s)的集合.
As I realized,
images
is a collection ofFrameworkElement
s (Image
s).
一个FrameworkElement
在逻辑树中只能有一个父级. ( FrameworkElement 类具有Parent
属性,该属性包含一个和唯一一个元素的父级.)
A FrameworkElement
can has only one parent in the logical-tree. (The FrameworkElement class has a Parent
property that contains the one and only parent of the element.)
当将Button
的Content
属性设置为Image
时,Image
的Parent
属性设置为Button
,并且当您尝试设置相同的Button
,您会得到异常,因为Image
已经有一个父级.
When you set the Content
property of the Button
to an Image
, the Parent
property of the Image
is set to the Button
and, when you try to set the same Image
to another Button
, you get the exception because the Image
already has a parent.
您必须为每个Button
创建一个不同的Image
.
You have to create a different Image
for each Button
.
这篇关于“元素已经是另一个元素的子元素".为什么会收到此错误消息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!