Reputation: 5724
I am in doubt. I have a large list container possibly thousands of items. When I virtualize (which is almost a demand for performance), I get this exception as soon as I try to update the selection:
Failed to update selection | [Exception] System.Exception: Catastrophic failure (Exception from HRESULT: 0x8000FFFF (E_UNEXPECTED))
at System.Runtime.InteropServices.WindowsRuntime.IVector`1.RemoveAt(UInt32 index)
at System.Runtime.InteropServices.WindowsRuntime.VectorToListAdapter.RemoveAtHelper[T](IVector`1 _this, UInt32 index)
at System.Runtime.InteropServices.WindowsRuntime.VectorToListAdapter.RemoveAt[T](Int32 index)
at MyApp.Views.SearchView.<>c__DisplayClass9_0.<UpdateListViewSelection>b__0()
at MyApp.Views.SearchView.<>c__DisplayClass10_0.<<ExecuteSafely>b__0>d.MoveNext()
I tried clearing the selection in 2 ways:
A:
listView.SelectedItems.Clear();
B:
for (int i = 0; i < listView.SelectedItems.Count; i++)
{
listView.SelectedItems.RemoveAt(i--);
}
The alternative is not to virtualize, but that's even worse... Anyone knows how to safely clear the selection on a virtualized listview in UWP?
Upvotes: 4
Views: 1207
Reputation: 5724
It seems important to clear the right way based on the SelectionMode
of the ListView. Below is a safe way to clear the selection of a ListView (without having to care about the SelectionMode
yourself):
public static void ClearSelection(this ListViewBase listView)
{
Argument.IsNotNull(() => listView);
switch (listView.SelectionMode)
{
case ListViewSelectionMode.None:
break;
case ListViewSelectionMode.Single:
listView.SelectedItem = null;
break;
case ListViewSelectionMode.Multiple:
case ListViewSelectionMode.Extended:
listView.SelectedItems.Clear();
break;
default:
throw new ArgumentOutOfRangeException();
}
}
Upvotes: 6