我在Windows Phone项目中有两页:
<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
<StackPanel x:Name="stp" Orientation="Horizontal"
HorizontalAlignment="Left" Height="auto" VerticalAlignment="Top" Width="auto"/>
</Grid>
private void gotoAddPage(object sender, EventArgs e)
{
NavigationService.Navigate(new Uri("/AddPage.xaml",
UriKind.RelativeOrAbsolute));
}
protected override void OnNavigatedTo(NavigationEventArgs e)
{
(App.Current as App).todoList.ForEach(delegate(Grid g)
{
stp.Children.Add(g);
});
base.OnNavigatedTo(e);
}
private void OnAddButtonClick(object sender, RoutedEventArgs e)
{
RowDefinition rd = new RowDefinition();
Grid gr = new Grid();
gr.RowDefinitions.Add(rd);
gr.ShowGridLines = true;
TextBlock tb = new TextBlock();
tb.Text = addTxtbox.Text;
gr.Children.Add(tb);
(App.Current as App).todoList.Add(gr);
NavigationService.Navigate(new Uri("/MainPage.xaml",
UriKind.RelativeOrAbsolute));
}
我使用App作为List的存储 问题是当我尝试将列表项添加到MainPage上的堆栈面板时,它创建了一个异常(Element已经是另一个元素的子元素。)之后我在列表中添加第二个项目(在AddPage上)并返回到MainPage并尝试打印它。