我正在尝试自定义 CollectionView 中单元格的选择颜色,但无论我如何尝试,它总是丑陋的灰色。
我希望我的项目模板有圆角,但是当我选择一个项目时,我看到它后面有丑陋的方形灰色角,如下图所示:
这是我当前的 XAML:
<?xml version="1.0" encoding="UTF-8"?>
<ContentView
xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="Tests.CollectionViewTest">
<ContentView.Content>
<CollectionView
x:Name="collectionView"
Margin="15,0"
ItemSizingStrategy="MeasureFirstItem"
Grid.Row="1"
Grid.RowSpan="2"
VerticalScrollBarVisibility="Never"
BackgroundColor="Transparent"
SelectionMode="Multiple"
HorizontalOptions="Center"
VerticalOptions="Center"
>
<CollectionView.ItemsLayout>
<GridItemsLayout
Orientation="Vertical"
HorizontalItemSpacing="1"
VerticalItemSpacing="1"
Span="3" />
</CollectionView.ItemsLayout>
<CollectionView.ItemTemplate>
<DataTemplate>
<Frame
x:Name="selectionFrame"
CornerRadius="18"
BackgroundColor="Transparent"
Padding="0"
HasShadow="False"
IsClippedToBounds="True"
BorderColor="Transparent">
<VisualStateManager.VisualStateGroups>
<VisualStateGroup
Name="CommonStates">
<VisualState
Name="Normal" />
<VisualState
Name="Focused">
<VisualState.Setters>
<Setter
Property="BackgroundColor"
Value="Transparent" />
</VisualState.Setters>
</VisualState>
<VisualState
Name="Selected">
<VisualState.Setters>
<Setter
Property="BackgroundColor"
Value="#e25fc4" />
</VisualState.Setters>
</VisualState>
</VisualStateGroup>
</VisualStateManager.VisualStateGroups>
<StackLayout
BackgroundColor="#f7f0f6"
HorizontalOptions="FillAndExpand"
VerticalOptions="FillAndExpand"
Orientation="Vertical"
Padding="8,0,8,10"
Margin="10"
Spacing="0"
HeightRequest="100">
<Label
Padding="10"
x:Name="ServiceName"
BackgroundColor="Transparent"
Text="Some Text"
HorizontalTextAlignment="Center"
TextColor="HotPink"
FontSize="Micro"
FontAttributes="Bold"
HorizontalOptions="Center"
VerticalOptions="End" />
<Label
BackgroundColor="Transparent"
Text="Some More Text"
HorizontalTextAlignment="Center"
TextColor="HotPink"
FontSize="Micro"
HorizontalOptions="Center"
VerticalOptions="Start" />
</StackLayout>
</Frame>
</DataTemplate>
</CollectionView.ItemTemplate>
</CollectionView>
</ContentView.Content>
</ContentView>
我的代码隐藏:
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using Xamarin.Forms;
namespace Tests
{
public partial class CollectionViewTest : ContentView
{
public CollectionViewTest()
{
InitializeComponent();
collectionView.ItemsSource = new ObservableCollection<string>()
{
"", "", "", "", "", "", "", "", "", "", "", "", "", ""
};
}
}
}
我也尝试过其他方法,但没有任何效果。
有没有办法做到这一点,或者这只是 CollectionView 的一个错误?