【发布时间】:2015-12-06 05:31:32
【问题描述】:
当我从一个页面导航到另一个页面时,我传递了一个类对象并添加到列表视图。但我面临的问题是,每次导航页面时,都会将项目添加到列表框中,但之前添加的项目会被删除。
谁能帮我解决这个问题以及如何检索上一个列表框项?
protected override void OnNavigatedTo(NavigationEventArgs e)
{
base.OnNavigatedTo(e);
if ((App.Current as App).isThereAnyChange)
{
getitem();
changeUI();
}
}
private void changeUI()
{
//throw new NotImplementedException();
txt1.Text = "";
items.Add(item1);
Deployment.Current.Dispatcher.BeginInvoke(() =>
{
this.sniplist.ItemsSource = items;
});
}
private void getitem()
{
// throw new NotImplementedException();
item1 = PhoneApplicationService.Current.State["item"] as Item;
// Debug.WriteLine(item1.description);
}
【问题讨论】:
-
项目集合是页面的成员吗?如果是这样,那么每次创建页面的新实例时,您都会创建一个新的集合...
-
@Jogy Yes items 是 ObservableCollection,它是页面的一部分。
-
更好的设计是创建一个单独的类来保存 ObservableCollection,并实例化该类一次,例如在 App.xaml.cs 中。
-
@Jogy 你能举个例子解释一下吗?
标签: c# windows-phone-8