I am working on a UWP project and tried to add a feature: swipe left and delete item from listview. I followed the instruction from https://docs.microsoft.com/en-us/windows/uwp/design/controls-and-patterns/swipe#how-does-swipe-work and download the sample code from https://github.com/Microsoft/Windows-universal-samples/tree/master/Samples/XamlUIBasics. Basically, I update the SwipePage.xaml.cs file from the github and add more items in the listview items source. When there are more than 25 listview items, if one item is swiped and deleted, the listview randomly pick one listview item and cover it with the delete icon. After you click delete icon and delete that listview item, continue scroll up/down the listview will crash the whole application.
Can anyone help solve this wired issue?
答案 0 :(得分:1)
原始代码存在缺陷。
private void DeleteItem_ItemInvoked(SwipeItem sender, SwipeItemInvokedEventArgs args)
{
int i = lv.Items.IndexOf(args.SwipeControl.DataContext);
items.RemoveAt(1);
}
items.RemoveAt应该删除 i ,而不是 1
private void DeleteItem_ItemInvoked(SwipeItem sender, SwipeItemInvokedEventArgs args)
{
int i = lv.Items.IndexOf(args.SwipeControl.DataContext);
items.RemoveAt(i);
}