该代码在 Android 中按预期工作,但在 iOS 中工作不一致。有时,选定标签的边框在点击时会改变颜色。在轻按 3 或 4 次不同标签后,它们根本不再被选中。长按标签按预期工作,打开不同的视图。返回到包含附加的 CollectionView 代码的视图时,标签选择已重新启用。该过程然后重复自身。我尝试更改 Xamarin.Forms 和 Xamarin.Community.Toolkit 的 Nuget 版本(甚至是最新的预发行版)、xaml 和代码隐藏都无济于事。我不知所措。任何帮助,将不胜感激。
<CollectionView x:Name="categoryCollectionView"
ItemsSource="{Binding Categories,Mode=TwoWay}"
SelectionMode="Single"
SelectedItem="{Binding Category}"
SelectionChanged="OnMenuItemSelected"
Grid.Column="0"
HorizontalOptions="FillAndExpand"
Grid.ColumnSpan="8"
Grid.Row="3"
VerticalScrollBarVisibility="Always" >
<CollectionView.ItemsLayout>
<GridItemsLayout x:Name="CategoriesGridLayout"
Orientation="Vertical"
Span="3"
VerticalItemSpacing="2"/>
</CollectionView.ItemsLayout>
<CollectionView.ItemTemplate>
<DataTemplate>
<Grid Padding="0">
<VisualStateManager.VisualStateGroups>
<VisualStateGroup x:Name="CommonStates">
<VisualState x:Name="Normal" />
<VisualState x:Name="Selected">
<VisualState.Setters>
<Setter TargetName="lblCategory" Property="Label.TextColor" Value="Red" />
<Setter Property="BackgroundColor" Value="Green" />
</VisualState.Setters>
</VisualState>
</VisualStateGroup>
</VisualStateManager.VisualStateGroups>
<StackLayout Orientation="Vertical"
VerticalOptions="Center"
xct:TouchEffect.LongPressCommand="{Binding LongPressCategoryCommand, Source={RelativeSource AncestorType={x:Type vm:CategoryVM}}}"
xct:TouchEffect.LongPressCommandParameter="{Binding CategoryString}" >
<StackLayout.Margin>
<OnPlatform x:TypeArguments="Thickness" iOS="2,2,4,4" Android="2" />
</StackLayout.Margin>
<Label x:Name="lblCategory"
Text="{Binding CategoryString}"
FontSize="Small"
BackgroundColor="#f5e2e1"
VerticalOptions="Center"
HorizontalOptions="CenterAndExpand"
WidthRequest="350"
Padding="5"
TextColor="#232F34"
LineBreakMode="TailTruncation"
Style="{DynamicResource CategoryLabelStyle}" >
<Label.Triggers>
<DataTrigger TargetType="Label"
Binding="{Binding Source={x:Reference fontSizeSwitch}, Path=IsToggled}"
Value="true">
<Setter Property="FontSize"
Value="Medium" />
<Setter Property="Padding"
Value="2" />
</DataTrigger>
</Label.Triggers>
</Label>
</StackLayout>
</Grid>
</DataTemplate>
</CollectionView.ItemTemplate>
</CollectionView>