Xamarin.forms CollectionView VisualStateManager无法更改选择颜色

Xamarin.forms CollectionView VisualStateManager无法更改选择颜色,xamarin.forms,datatemplate,visualstatemanager,xamarin.forms.collectionview,Xamarin.forms,Datatemplate,Visualstatemanager,Xamarin.forms.collectionview,我试图在CollectionView中自定义单元格的选择颜色,但无论我如何尝试,它始终是难看的灰色 我希望我的项目模板有圆角,但当我选择一个项目时,我会看到它后面有丑陋的灰色方角,如下图所示: 以下是我当前的XAML: <?xml version="1.0" encoding="UTF-8"?> <ContentView xmlns="http://xamarin.com/schemas/2014/forms" xml

我试图在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的一个bug?

我再次尝试让它工作,现在我在StackLayout中有了一个框架。而不是相反的方向。但是没有运气,现在所选项目周围没有角落。很抱歉,我无法让它工作。

我找到了一个困难的解决方案,如果没有一个正确的解决方案,它将不得不这样做

将CollectionView中的选择行为设置为“无”。 将TapGestureRecognitor放入itemTemplate 要模拟选择状态,请在tapGestureRecognizer的事件处理程序中,将发送者强制转换为一个帧或您将手势识别器附加到的任何元素,并打开或关闭帧边框,或根据自己的自定义选择状态外观执行所需的操作。 手动处理CollectionView为响应选择而通常触发的内容。换句话说,如果您可以选择多个项目,您可能会在一个单独的列表中跟踪所选项目,现在您必须从TapGestureRecognitor内部执行此操作。
这是错误的,但它是有效的,有时这就是你应该做的。

我很兴奋能尝试这个。对你有用吗?这对我来说没有任何改变。请尝试在帧中更改CollectionView。文档在这里。它改变了框架的背景颜色,但没有删除丑陋的灰色选择框什么平台?无论如何,我认为一个解决办法是将一个框架包装在另一个框架内。外部框架应该是正方形,没有圆角,并给它你想要的背景色。内部框架具有圆角和透明背景。不确定如何使用VisualStateManager和select进行设置,但这可能会给您一个想法。如果你能做到这一点,请添加答案,不要编辑问题;在下面添加显示工作代码的实际答案。这将帮助其他人。48小时后,选择您自己的答案。问题是否在ios中?@ToolMakerSteve我需要背景透明。@LeoZhu MSFT是的,它在ios中iOS@LeMotJuiced-不清楚灰色是从哪里来的-可能不是因为被选中。如果将CornerRadius设置为原始(未选定)按钮状态,会发生什么情况?如果你看到周围有灰色,那么你必须尝试不同的实验来找出灰色的来源。如果您没有看到灰色,那么您的标题是正确的-这是所选状态的问题。在这种情况下,我不知道如何修复它。抑制损坏的内置行为!好主意。