我正在尝试动态创建和添加项目到Windows Phone应用程序中的网格。 在我在一个简单的页面上做之前,工作正常。现在我在Pivot页面上做同样的事情,我在添加第二项时遇到异常。 “元素已经是另一个元素的孩子。” XAML代码方面:
<controls:PivotItem Header="pirmadienis" x:Name="Monday">
<ScrollViewer Height="600">
<Grid x:Name="MondayGrid">
<TextBlock Text="Laikas" Margin="21,0,-21,0" VerticalAlignment="Top" HorizontalAlignment="Left"/>
<TextBlock x:Name="fatherTime" FontSize="40" Text="44:44" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="0,21,0,0" Visibility="Collapsed"/>
<TextBlock x:Name="fatherText" HorizontalAlignment="Left" Margin="115,21,0,0" Text="WWWWWWWWW" VerticalAlignment="Top" FontSize="40" Width="341" Visibility="Collapsed"/>
<TextBlock Text="Pavadinimas" Margin="215,0,0,0" VerticalAlignment="Top" HorizontalAlignment="Left"/>
</Grid>
</ScrollViewer>
</controls:PivotItem>
<!--Pivot item two-->
<controls:PivotItem Header="antradienis" x:Name="Tuesday" Visibility="Collapsed">
<ScrollViewer Height="600">
<Grid x:Name="TuesdayGrid">
<TextBlock Text="Laikas" Margin="21,0,-21,0" VerticalAlignment="Top" HorizontalAlignment="Left"/>
<TextBlock Text="Pavadinimas" Margin="215,0,0,0" VerticalAlignment="Top" HorizontalAlignment="Left"/>
</Grid>
</ScrollViewer>
</controls:PivotItem>
<!--Pivot item three-->
<controls:PivotItem Header="trečiadienis" x:Name="Wednesday" Visibility="Collapsed">
<ScrollViewer Height="600">
<Grid x:Name="WednesdayGrid">
<TextBlock Text="Laikas" Margin="21,0,-21,0" VerticalAlignment="Top" HorizontalAlignment="Left"/>
<TextBlock Text="Pavadinimas" Margin="215,0,0,0" VerticalAlignment="Top" HorizontalAlignment="Left"/>
</Grid>
</ScrollViewer>
</controls:PivotItem>
<!--Pivot item four-->
<controls:PivotItem Header="ketvirtadienis" x:Name="Thursday" Visibility="Collapsed">
<ScrollViewer Height="600">
<Grid x:Name="ThursdayGrid">
<TextBlock Text="Laikas" Margin="21,0,-21,0" VerticalAlignment="Top" HorizontalAlignment="Left"/>
<TextBlock Text="Pavadinimas" Margin="215,0,0,0" VerticalAlignment="Top" HorizontalAlignment="Left"/>
</Grid>
</ScrollViewer>
</controls:PivotItem>
<!--Pivot item five-->
<controls:PivotItem Header="penktadienis" x:Name="Friday" Visibility="Collapsed">
<ScrollViewer Height="600">
<Grid x:Name="FridayGrid">
<TextBlock Text="Laikas" Margin="21,0,-21,0" VerticalAlignment="Top" HorizontalAlignment="Left"/>
<TextBlock Text="Pavadinimas" Margin="215,0,0,0" VerticalAlignment="Top" HorizontalAlignment="Left"/>
</Grid>
</ScrollViewer>
</controls:PivotItem>
<!--Pivot item six-->
<controls:PivotItem Header="šeštadienis" x:Name="Saturday" Visibility="Collapsed">
<ScrollViewer Height="600">
<Grid x:Name="SaturdayGrid">
<TextBlock Text="Laikas" Margin="21,0,-21,0" VerticalAlignment="Top" HorizontalAlignment="Left"/>
<TextBlock Text="Pavadinimas" Margin="215,0,0,0" VerticalAlignment="Top" HorizontalAlignment="Left"/>
</Grid>
</ScrollViewer>
</controls:PivotItem>
</controls:Pivot>
C#部分代码:
void addDateItem(string text)
{
Thickness tempThick = fatherTime.Margin;
tempThick.Top += itemsAdded * 58;
TextBlock temp = new TextBlock()
{
Text = text,
FontSize = 40,
HorizontalAlignment = HorizontalAlignment.Left,
VerticalAlignment = VerticalAlignment.Top,
Margin = tempThick
};
temp.Tap += paspaudus;
MondayGrid.Children.Add(temp);
TuesdayGrid.Children.Add(temp); // Exception on this line
WednesdayGrid.Children.Add(temp);
ThursdayGrid.Children.Add(temp);
FridayGrid.Children.Add(temp);
SaturdayGrid.Children.Add(temp);
itemsAdded++;
}
答案 0 :(得分:2)
错误消息很明确。您无法将已添加为父项子项的元素添加到其他父元素。您必须为每个TextBlock
实例化新的<grid>
对象。