这个问题已经有答案了:
Specified Visual is already a child of another Visual or the root of a CompositionTarget(1个答案)
14天前关闭
我有一个空白的多米诺骨牌,我想用这个函数用点填充它:
private void Arrange(Grid grid, byte dots)
{
Ellipse e = new Ellipse
{
Fill = new SolidColorBrush(Colors.Black)
};
Border b = new Border
{
Background = new SolidColorBrush(Colors.Black),
Margin = new Thickness(0, 5, 0, 5),
CornerRadius = new CornerRadius(2.5)
};
//BoneView.Layout.Children.Add(b);
Grid.SetColumn(b, 1);
if (dots == 0)
return;
if (dots == 1)
{
grid.Children.Add(e);
Grid.SetColumn(e, 1);
Grid.SetRow(e, 1);
return;
}
if (dots == 2)
{
List<Ellipse> ellipses = new List<Ellipse>();
for (int i = 0; i < dots; i++)
{
ellipses.Add(e);
grid.Children.Add(ellipses[i]);
}
Grid.SetColumn(ellipses[1], 2);
Grid.SetRow(ellipses[1], 2);
return;
}
if (dots == 3)
{
List<Ellipse> ellipses = new List<Ellipse>();
for (int i = 0; i < dots; i++)
{
ellipses.Add(e);
grid.Children.Add(ellipses[i]);
}
Grid.SetColumn(ellipses[1], 2);
Grid.SetRow(ellipses[1], 2);
Grid.SetColumn(ellipses[2], 1);
Grid.SetRow(ellipses[2], 1);
return;
}
if (dots == 4)
{
List<Ellipse> ellipses = new List<Ellipse>();
for (int i = 0; i < dots; i++)
{
ellipses.Add(e);
grid.Children.Add(ellipses[i]);
}
Grid.SetColumn(ellipses[1], 2);
Grid.SetRow(ellipses[1], 2);
Grid.SetColumn(ellipses[2], 2);
Grid.SetRow(ellipses[2], 0);
Grid.SetColumn(ellipses[3], 0);
Grid.SetRow(ellipses[3], 2);
return;
}
if (dots == 5)
{
List<Ellipse> ellipses = new List<Ellipse>();
for (int i = 0; i < dots; i++)
{
ellipses.Add(e);
grid.Children.Add(ellipses[i]);
}
Grid.SetColumn(ellipses[1], 2);
Grid.SetRow(ellipses[1], 2);
Grid.SetColumn(ellipses[2], 2);
Grid.SetRow(ellipses[2], 0);
Grid.SetColumn(ellipses[3], 0);
Grid.SetRow(ellipses[3], 2);
Grid.SetColumn(ellipses[4], 1);
Grid.SetRow(ellipses[4], 1);
return;
}
if (dots == 6)
{
List<Ellipse> ellipses = new List<Ellipse>();
for (int i = 0; i < dots; i++)
{
ellipses.Add(e);
grid.Children.Add(ellipses[i]);
}
Grid.SetColumn(ellipses[1], 2);
Grid.SetRow(ellipses[1], 2);
Grid.SetColumn(ellipses[2], 2);
Grid.SetRow(ellipses[2], 0);
Grid.SetColumn(ellipses[3], 0);
Grid.SetRow(ellipses[3], 2);
Grid.SetColumn(ellipses[4], 1);
Grid.SetRow(ellipses[4], 0);
Grid.SetColumn(ellipses[5], 1);
Grid.SetRow(ellipses[5], 2);
return;
}
在grid.Children.Add(e)
处,它抛出“指定的视觉对象已经是另一个视觉对象的子对象或CompositionTarget的根对象”异常。
据我所知,这是因为椭圆e
已经添加到网格中。
我该如何解决此问题?
下面是多米诺骨牌的设计:
<Border Background="AntiqueWhite"
CornerRadius="7"
>
<Grid Name="Layout">
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition Width="5"/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<Grid Grid.Column="0" x:Name="Left">
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition/>
<RowDefinition/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
</Grid>
<Grid Grid.Column="2" x:Name="Right">
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition/>
<RowDefinition/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
</Grid>
</Grid>
</Border>
1条答案
按热度按时间jucafojl1#
这是因为你在函数的顶部示例化了
Ellipse
,代码为Ellipse e = new Ellipse
...,然后当一个多米诺骨牌有多个点时,你为每个点添加相同的示例化e
。每次要添加点时,都需要示例化Ellipse
。简单的解决方案是在添加点的
for...each
循环中示例化Ellipse
,正如我在下面所做的那样。