CollectionView SelectedItem 未在 Xamarin Forms 中突出显示

CollectionView SelectedItem is not highlighted in Xamarin Forms

我试图突出显示 Xamarin Forms CollectionView 项,但它似乎没有突出显示。在调试时我发现如果我删除 SwipeView Gesture 事件它会按预期工作。此外,在当前代码中,选择项已更改,但颜色未更改。如何突出显示所选项目?

<CollectionView ItemsSource="{Binding SomeList}" 
SelectedItem="{Binding CurrentItem}" SelectionMode="Single" x:Name="itemView">
<CollectionView.ItemsLayout>
     <GridItemsLayout Orientation="Vertical" Span="1"/>
  </CollectionView.ItemsLayout>
  <CollectionView.ItemTemplate>
    <DataTemplate>
    <SwipeView>
     <SwipeView.GestureRecognizers>
         <SwipeGestureRecognizer Direction="Right"
          Command="{Binding BindingContext.SwipGestureCommand, Source={x:Reference itemView}}"
                                                            CommandParameter="right"/>
   </SwipeView.GestureRecognizers>
   <StackLayout Orientation="Vertical" Padding="5">
    <VisualStateManager.VisualStateGroups>
         <VisualStateGroup Name="CommonStates">
        <VisualState Name="Normal" />
        <VisualState Name="Selected">
             <VisualState.Setters>
                     <Setter Property="BackgroundColor" Value="Yellow" />
             </VisualState.Setters>
       </VisualState>
      </VisualStateGroup>
      </VisualStateManager.VisualStateGroups>
          <Label LineBreakMode="WordWrap"  Text="{Binding SomeText}"/>
        </StackLayout>
      </SwipeView>
    </DataTemplate>
  </CollectionView.ItemTemplate>
</CollectionView>

如何高亮选中的collectionview项?

请将Xamarin.Forms版本升级到4.6及以上,然后修改你的代码如下代码,设置swip背景颜色为白色,并添加VisualStateManager.VisualStateGroups用于swip。

 <CollectionView
            x:Name="itemView"
            ItemsSource="{Binding SomeList}"
            SelectionMode="Single">
            <CollectionView.ItemsLayout>
                <GridItemsLayout Orientation="Vertical" Span="1" />
            </CollectionView.ItemsLayout>
            <CollectionView.ItemTemplate>
                <DataTemplate>
                    <SwipeView BackgroundColor="White">
                        <SwipeView.GestureRecognizers>
                            <SwipeGestureRecognizer
                                Command="{Binding BindingContext.SwipGestureCommand, Source={x:Reference itemView}}"
                                CommandParameter="right"
                                Direction="Right" />
                        </SwipeView.GestureRecognizers>
                        <StackLayout Padding="5" Orientation="Vertical">
                            <Label LineBreakMode="WordWrap" Text="{Binding SomeText}" />

                        </StackLayout>
                        <VisualStateManager.VisualStateGroups>
                            <VisualStateGroup Name="CommonStates">
                                <VisualState Name="Normal" />
                                <VisualState Name="Selected">
                                    <VisualState.Setters>
                                        <Setter Property="BackgroundColor" Value="Yellow" />
                                    </VisualState.Setters>
                                </VisualState>
                            </VisualStateGroup>
                        </VisualStateManager.VisualStateGroups>
                    </SwipeView>
                </DataTemplate>
            </CollectionView.ItemTemplate>
        </CollectionView>