所以基本上情况就是这样。我有一个longlistselector
显示数据(例如,汽车列表):
<phone:LongListSelector x:Name="list" ItemsSource="{Binding CarList}">
<phone:LongListSelector.ItemTemplate>
<DataTemplate>
<StackPanel>
<toolkit:ContextMenuService.ContextMenu>
<toolkit:ContextMenu>
<toolkit:MenuItem Click="DeleteMenuItem_Click" Header="delete"/>
</toolkit:ContextMenu>
</toolkit:ContextMenuService.ContextMenu>
<TextBlock Text="{Binding SomeText}">
</StackPanel>
</DataTemplate>
</phone:LongListSelector.ItemTemplate>
</phone:LongListSelector>
然后这就是我处理删除的方式:
private void DeleteMenuItem_Click(object sender, RoutedEventArgs e)
{
Car data = (sender as MenuItem).DataContext as Car;
using (var db = new SQLiteConnection(App.DBpath))
{
var existing = db.Query<Feed>("select * from Cars where Id = " + data.Id.ToString()).FirstOrDefault();
if (existing != null)
{
db.RunInTransaction(() =>
{
db.Delete(existing);
});
App.ViewModel.loadCarData();
}
}
}
现在的问题是,在删除了一些之后,上下文菜单开始给出旧的选择并且不更新,这使得var existing = db.Query<Feed>("select * from Cars where Id = " + data.Id.ToString()).FirstOrDefault();
返回 null 显然是因为它已经从数据库中删除了。知道如何解决这个问题吗?