xamarin.forms - android上的xamarin.forms Collectionview中没有可见的选择
问题描述
我有这个 xaml collectionView 代码。我在 uwp 上得到了可见的选择,但在 android 9 上没有。
<CollectionView Grid.Row="6" ItemsSource="{Binding SearchResults}" x:Name="searchResultCollection" Grid.ColumnSpan="2" SelectionMode="Single">
<CollectionView.ItemTemplate>
<DataTemplate>
<Grid Style="{StaticResource listViewGridStyle}">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Grid.GestureRecognizers>
<TapGestureRecognizer Command="{Binding BindingContext.SearchResultSelectionChangedCommand, Source={x:Reference searchResultCollection}}" CommandParameter="{Binding}"/>
</Grid.GestureRecognizers>
<Label Text="Start" Grid.Row="0" Grid.Column="0" Style="{StaticResource listViewLabelStyle}"/>
<Label Text="{Binding Start, Converter={StaticResource dateTimeToString}}" Grid.Row="0" Grid.Column="1" Style="{StaticResource listViewLabelStyle}"/>
<Label Text="{Binding SearchMatchLabel}" Grid.Row="1" Grid.Column="0" Style="{StaticResource listViewLabelStyle}"/>
<Label Text="{Binding SearchMatchHighlight}" TextType="Html" Grid.Row="1" Style="{StaticResource listViewLabelStyle}" Grid.Column="1"/>
</Grid>
</DataTemplate>
</CollectionView.ItemTemplate>
</CollectionView>
我已更新到当前的 Xamarin.Forms 版本,但问题仍然存在。有任何想法吗?
更新:我已将所选背景添加到我的样式中,但它仍然不起作用。我已经尝试使用橙色作为背景颜色的 uwp,但显示的是浅蓝色。看起来 State 没有更改为 Selected
<Style x:Key="listViewGridStyle" TargetType="Grid">
<Setter Property="RowSpacing">
<Setter.Value>
<OnIdiom Phone="0.1" Tablet="5" Desktop="5"/>
</Setter.Value>
</Setter>
<Setter Property="ColumnSpacing">
<Setter.Value>
<OnIdiom Phone="2" Tablet="10" Desktop="10"/>
</Setter.Value>
</Setter>
<Setter Property="VisualStateManager.VisualStateGroups">
<VisualStateGroupList>
<VisualStateGroup x:Name="CommonStates">
<VisualState x:Name="Normal" />
<VisualState x:Name="Selected">
<VisualState.Setters>
<Setter Property="BackgroundColor"
Value="LightSkyBlue" />
</VisualState.Setters>
</VisualState>
</VisualStateGroup>
</VisualStateGroupList>
</Setter>
</Style>
解决方案:我已经添加了点击事件
<Grid.GestureRecognizers>
<TapGestureRecognizer Tapped="TapGestureRecognizer_Tapped" Command="{Binding BindingContext.SearchResultSelectionChangedCommand, Source={x:Reference searchResultCollection}}" CommandParameter="{Binding}"/>
</Grid.GestureRecognizers>
在文件后面的代码中,我手动更改了状态:
private void TapGestureRecognizer_Tapped(object sender, System.EventArgs e)
{
var nextSelected = sender as VisualElement;
if (_previousSelected != null) VisualStateManager.GoToState(_previousSelected, "Normal");
VisualStateManager.GoToState(nextSelected, "Selected");
_previousSelected = nextSelected;
}
解决方案
我注意到您的 Grid 有一个listViewGridStyle
. 我假设您的网格具有背景颜色,因此默认选定的项目颜色将不起作用。您需要自己更改所选项目的颜色。
示例在这里:
<ContentPage ...>
<ContentPage.Resources>
<Style TargetType="Grid">
<Setter Property="VisualStateManager.VisualStateGroups">
<VisualStateGroupList>
<VisualStateGroup x:Name="CommonStates">
<VisualState x:Name="Normal" />
<VisualState x:Name="Selected">
<VisualState.Setters>
<Setter Property="BackgroundColor"
Value="LightSkyBlue" />
</VisualState.Setters>
</VisualState>
</VisualStateGroup>
</VisualStateGroupList>
</Setter>
</Style>
</ContentPage.Resources>
<StackLayout Margin="20">
<CollectionView ItemsSource="{Binding Monkeys}"
SelectionMode="Single">
<CollectionView.ItemTemplate>
<DataTemplate>
<Grid Padding="10">
...
</Grid>
</DataTemplate>
</CollectionView.ItemTemplate>
</CollectionView>
</StackLayout>
</ContentPage>
包含 Selected VisualState 的 Style 必须具有 TargetType 属性值,该属性值是 DataTemplate 的根元素的类型,它被设置为 ItemTemplate 属性值。
试试看,随时问我任何问题。
推荐阅读
- assembly - 无法弄清楚装配循环
- javascript - 递归 JSON 字符串解析器
- python - 使用 statsmodels 进行季节性调整:路径中未找到 x13as
- r - 右 | 无法编织 | 'stringi' 不适用于 R-3.4.3
- php - 将它们转换为 PDF 时质量较差的 PHP Image Magick 图像
- wordpress - 重定向到 /amp 页面
- jenkins - 使用 docker-container 作为 jenkins 构建代理时,如何在工作区中保留构建目录?
- php - 在一个批次中向多个不同的 API 发送请求
- elasticsearch - Elasticsearch-如何使聚合仅返回匹配的键
- javascript - 使用 Promises 设置序列到 Javascript 函数