首页 > 解决方案 > 当用户在 Xamarin.Forms 中同时单击多个项目时,如何修复 CollectionView SelectionMode="Single" 错误?

问题描述

我在 Xamarin.Forms 的 CollectionView 中使用 SelectionMode="Single",但每当用户不小心同时单击或点击多个项目时,它都会使我的应用程序崩溃。我尝试在 SelectionChangedCommand 和 SelectedItem 周围使用 try{}catch{},但它仍然没有帮助。应用程序崩溃后我可以在 Visual Studio 中找到的错误是“System.ArgumentOutOfRangeException:'索引超出范围。必须是非负数并且小于集合的大小。参数名称:索引'”我该如何解决这个?

  #MyCollectionViewCode:

  <CollectionView ItemsSource="{Binding Content}" ItemsUpdatingScrollMode="KeepItemsInView"
  SelectionMode="Single" SelectedItem="{Binding SelectedData}"                            
  SelectionChangedCommand="{Binding DoDisplay}" 
  ItemTemplate="{StaticResource dataTemplateSelector}"                     
  />

标签: c#xamarin.forms

解决方案


推荐阅读