我有疑问。我有一个大型列表容器可能有数千个项目。当我虚拟化(这几乎是对性能的要求)时,我会在尝试更新选择时立即得到此异常:
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()
我尝试以两种方式清除选择:
答:
listView.SelectedItems.Clear();
B:
for (int i = 0; i < listView.SelectedItems.Count; i++)
{
listView.SelectedItems.RemoveAt(i--);
}
替代方案不是虚拟化,但更糟糕的是......任何人都知道如何安全地清除UWP中虚拟列表视图上的选择?
答案 0 :(得分:3)
基于ListView的SelectionMode
清除正确的方法似乎很重要。以下是清除ListView选择的安全方法(无需自己关心SelectionMode
):
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();
}
}