Xaml VisualStateManager无法在Xamarin表单的CollectionView中使用SelectionChanged

Xaml VisualStateManager无法在Xamarin表单的CollectionView中使用SelectionChanged,xaml,xamarin.forms,collectionview,visualstatemanager,Xaml,Xamarin.forms,Collectionview,Visualstatemanager,在集合视图中添加SelectionChanged函数时,VisualStateManager不起作用 <ContentPage.Resources> <Style TargetType="StackLayout"> <Setter Property="VisualStateManager.VisualStateGroups"> <VisualStateGroupLis

在集合视图中添加
SelectionChanged
函数时,
VisualStateManager
不起作用

<ContentPage.Resources>
    <Style TargetType="StackLayout">
        <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>


当我在
CollectionView
SelectionChanged=“OnCollectionViewSelectionChanged”
中添加此代码时,
VisualStateManager
停止工作。有人知道原因吗?

该问题导致
选择更改
无法获取所选元素类型,如
堆栈布局
标签
。您可以使用
TapGestureRecognitor

Xaml:

   <ContentPage.Resources>
    <Style TargetType="StackLayout">
        <Setter Property="VisualStateManager.VisualStateGroups">
            <VisualStateGroupList>
                <VisualStateGroup>
                    <VisualState x:Name="Selected">
                        <VisualState.Setters>
                            <Setter Property="BackgroundColor" Value="Accent" />
                        </VisualState.Setters>
                    </VisualState>
                    <VisualState x:Name="UnSelected">
                        <VisualState.Setters>
                            <Setter Property="BackgroundColor" Value="Blue" />
                        </VisualState.Setters>
                    </VisualState>
                </VisualStateGroup>
            </VisualStateGroupList>
        </Setter>
    </Style>
</ContentPage.Resources>
<ContentPage.Content>
    <StackLayout
        Padding="10"
        HorizontalOptions="FillAndExpand"
        VerticalOptions="FillAndExpand">
        <CollectionView
            x:Name="MenuCollection"
            ItemsSource="{Binding Infos}"
            SelectionChanged="MenuCollection_OnSelectionChanged"
            SelectionMode="Single"
            VerticalScrollBarVisibility="Always">

            <CollectionView.ItemsLayout>
                <GridItemsLayout
                    HorizontalItemSpacing="15"
                    Orientation="Vertical"
                    Span="2"
                    VerticalItemSpacing="15" />
            </CollectionView.ItemsLayout>

            <CollectionView.ItemTemplate>
                <DataTemplate>

                    <StackLayout BackgroundColor="Blue">
                        <Label Text="{Binding Title}" />
                        <StackLayout.GestureRecognizers>
                            <TapGestureRecognizer Tapped="TapGestureRecognizer_Tapped" />
                        </StackLayout.GestureRecognizers>
                    </StackLayout>

                </DataTemplate>
            </CollectionView.ItemTemplate>
        </CollectionView>
    </StackLayout>
</ContentPage.Content>
 public partial class Page4 : ContentPage
{
    public ObservableCollection<Info> Infos { get; set; }
    public Page4()
    {
        InitializeComponent();
        Infos = new ObservableCollection<Info>
    {
        new Info(){ Title="A"},
        new Info(){ Title="B"},
        new Info(){ Title="B"},
        new Info(){ Title="C"},
        new Info(){ Title="D"}
    };

        this.BindingContext = this;
    }
    StackLayout lastElementSelected;
    private void MenuCollection_OnSelectionChanged(object sender, SelectionChangedEventArgs e)
    {      

     

    }

    private void TapGestureRecognizer_Tapped(object sender, EventArgs e)
    {

        if (lastElementSelected != null)
            VisualStateManager.GoToState(lastElementSelected, "UnSelected");

        VisualStateManager.GoToState((StackLayout)sender, "Selected");

        lastElementSelected = (StackLayout)sender;
    }
}
public class Info
{
    public string Title { get; set; }
}

代码隐藏:

   <ContentPage.Resources>
    <Style TargetType="StackLayout">
        <Setter Property="VisualStateManager.VisualStateGroups">
            <VisualStateGroupList>
                <VisualStateGroup>
                    <VisualState x:Name="Selected">
                        <VisualState.Setters>
                            <Setter Property="BackgroundColor" Value="Accent" />
                        </VisualState.Setters>
                    </VisualState>
                    <VisualState x:Name="UnSelected">
                        <VisualState.Setters>
                            <Setter Property="BackgroundColor" Value="Blue" />
                        </VisualState.Setters>
                    </VisualState>
                </VisualStateGroup>
            </VisualStateGroupList>
        </Setter>
    </Style>
</ContentPage.Resources>
<ContentPage.Content>
    <StackLayout
        Padding="10"
        HorizontalOptions="FillAndExpand"
        VerticalOptions="FillAndExpand">
        <CollectionView
            x:Name="MenuCollection"
            ItemsSource="{Binding Infos}"
            SelectionChanged="MenuCollection_OnSelectionChanged"
            SelectionMode="Single"
            VerticalScrollBarVisibility="Always">

            <CollectionView.ItemsLayout>
                <GridItemsLayout
                    HorizontalItemSpacing="15"
                    Orientation="Vertical"
                    Span="2"
                    VerticalItemSpacing="15" />
            </CollectionView.ItemsLayout>

            <CollectionView.ItemTemplate>
                <DataTemplate>

                    <StackLayout BackgroundColor="Blue">
                        <Label Text="{Binding Title}" />
                        <StackLayout.GestureRecognizers>
                            <TapGestureRecognizer Tapped="TapGestureRecognizer_Tapped" />
                        </StackLayout.GestureRecognizers>
                    </StackLayout>

                </DataTemplate>
            </CollectionView.ItemTemplate>
        </CollectionView>
    </StackLayout>
</ContentPage.Content>
 public partial class Page4 : ContentPage
{
    public ObservableCollection<Info> Infos { get; set; }
    public Page4()
    {
        InitializeComponent();
        Infos = new ObservableCollection<Info>
    {
        new Info(){ Title="A"},
        new Info(){ Title="B"},
        new Info(){ Title="B"},
        new Info(){ Title="C"},
        new Info(){ Title="D"}
    };

        this.BindingContext = this;
    }
    StackLayout lastElementSelected;
    private void MenuCollection_OnSelectionChanged(object sender, SelectionChangedEventArgs e)
    {      

     

    }

    private void TapGestureRecognizer_Tapped(object sender, EventArgs e)
    {

        if (lastElementSelected != null)
            VisualStateManager.GoToState(lastElementSelected, "UnSelected");

        VisualStateManager.GoToState((StackLayout)sender, "Selected");

        lastElementSelected = (StackLayout)sender;
    }
}
public class Info
{
    public string Title { get; set; }
}
公共部分类第4页:内容页
{
公共可观测集合信息{get;set;}
公共页4()
{
初始化组件();
Infos=新的可观察集合
{
新信息(){Title=“A”},
新信息(){Title=“B”},
新信息(){Title=“B”},
新信息(){Title=“C”},
新信息(){Title=“D”}
};
this.BindingContext=this;
}
StackLayout lastElementSelected;
private void MenuCollection_OnSelectionChanged(对象发送者,SelectionChangedEventArgs e)
{      
}
私有无效TapGestureRecognitor_(对象发送方,事件参数e)
{
如果(lastElementSelected!=null)
VisualStateManager.gostate(lastElementSelected,“UnSelected”);
VisualStateManager.GoToState((StackLayout)发送方,“选定”);
lastElementSelected=(StackLayout)发送方;
}
}
公共类信息
{
公共字符串标题{get;set;}
}
屏幕截图:

   <ContentPage.Resources>
    <Style TargetType="StackLayout">
        <Setter Property="VisualStateManager.VisualStateGroups">
            <VisualStateGroupList>
                <VisualStateGroup>
                    <VisualState x:Name="Selected">
                        <VisualState.Setters>
                            <Setter Property="BackgroundColor" Value="Accent" />
                        </VisualState.Setters>
                    </VisualState>
                    <VisualState x:Name="UnSelected">
                        <VisualState.Setters>
                            <Setter Property="BackgroundColor" Value="Blue" />
                        </VisualState.Setters>
                    </VisualState>
                </VisualStateGroup>
            </VisualStateGroupList>
        </Setter>
    </Style>
</ContentPage.Resources>
<ContentPage.Content>
    <StackLayout
        Padding="10"
        HorizontalOptions="FillAndExpand"
        VerticalOptions="FillAndExpand">
        <CollectionView
            x:Name="MenuCollection"
            ItemsSource="{Binding Infos}"
            SelectionChanged="MenuCollection_OnSelectionChanged"
            SelectionMode="Single"
            VerticalScrollBarVisibility="Always">

            <CollectionView.ItemsLayout>
                <GridItemsLayout
                    HorizontalItemSpacing="15"
                    Orientation="Vertical"
                    Span="2"
                    VerticalItemSpacing="15" />
            </CollectionView.ItemsLayout>

            <CollectionView.ItemTemplate>
                <DataTemplate>

                    <StackLayout BackgroundColor="Blue">
                        <Label Text="{Binding Title}" />
                        <StackLayout.GestureRecognizers>
                            <TapGestureRecognizer Tapped="TapGestureRecognizer_Tapped" />
                        </StackLayout.GestureRecognizers>
                    </StackLayout>

                </DataTemplate>
            </CollectionView.ItemTemplate>
        </CollectionView>
    </StackLayout>
</ContentPage.Content>
 public partial class Page4 : ContentPage
{
    public ObservableCollection<Info> Infos { get; set; }
    public Page4()
    {
        InitializeComponent();
        Infos = new ObservableCollection<Info>
    {
        new Info(){ Title="A"},
        new Info(){ Title="B"},
        new Info(){ Title="B"},
        new Info(){ Title="C"},
        new Info(){ Title="D"}
    };

        this.BindingContext = this;
    }
    StackLayout lastElementSelected;
    private void MenuCollection_OnSelectionChanged(object sender, SelectionChangedEventArgs e)
    {      

     

    }

    private void TapGestureRecognizer_Tapped(object sender, EventArgs e)
    {

        if (lastElementSelected != null)
            VisualStateManager.GoToState(lastElementSelected, "UnSelected");

        VisualStateManager.GoToState((StackLayout)sender, "Selected");

        lastElementSelected = (StackLayout)sender;
    }
}
public class Info
{
    public string Title { get; set; }
}

该问题导致
SelectionChanged
无法获取所选元素类型,如
StackLayout
Label
。您可以使用
TapGestureRecognitor

Xaml:

   <ContentPage.Resources>
    <Style TargetType="StackLayout">
        <Setter Property="VisualStateManager.VisualStateGroups">
            <VisualStateGroupList>
                <VisualStateGroup>
                    <VisualState x:Name="Selected">
                        <VisualState.Setters>
                            <Setter Property="BackgroundColor" Value="Accent" />
                        </VisualState.Setters>
                    </VisualState>
                    <VisualState x:Name="UnSelected">
                        <VisualState.Setters>
                            <Setter Property="BackgroundColor" Value="Blue" />
                        </VisualState.Setters>
                    </VisualState>
                </VisualStateGroup>
            </VisualStateGroupList>
        </Setter>
    </Style>
</ContentPage.Resources>
<ContentPage.Content>
    <StackLayout
        Padding="10"
        HorizontalOptions="FillAndExpand"
        VerticalOptions="FillAndExpand">
        <CollectionView
            x:Name="MenuCollection"
            ItemsSource="{Binding Infos}"
            SelectionChanged="MenuCollection_OnSelectionChanged"
            SelectionMode="Single"
            VerticalScrollBarVisibility="Always">

            <CollectionView.ItemsLayout>
                <GridItemsLayout
                    HorizontalItemSpacing="15"
                    Orientation="Vertical"
                    Span="2"
                    VerticalItemSpacing="15" />
            </CollectionView.ItemsLayout>

            <CollectionView.ItemTemplate>
                <DataTemplate>

                    <StackLayout BackgroundColor="Blue">
                        <Label Text="{Binding Title}" />
                        <StackLayout.GestureRecognizers>
                            <TapGestureRecognizer Tapped="TapGestureRecognizer_Tapped" />
                        </StackLayout.GestureRecognizers>
                    </StackLayout>

                </DataTemplate>
            </CollectionView.ItemTemplate>
        </CollectionView>
    </StackLayout>
</ContentPage.Content>
 public partial class Page4 : ContentPage
{
    public ObservableCollection<Info> Infos { get; set; }
    public Page4()
    {
        InitializeComponent();
        Infos = new ObservableCollection<Info>
    {
        new Info(){ Title="A"},
        new Info(){ Title="B"},
        new Info(){ Title="B"},
        new Info(){ Title="C"},
        new Info(){ Title="D"}
    };

        this.BindingContext = this;
    }
    StackLayout lastElementSelected;
    private void MenuCollection_OnSelectionChanged(object sender, SelectionChangedEventArgs e)
    {      

     

    }

    private void TapGestureRecognizer_Tapped(object sender, EventArgs e)
    {

        if (lastElementSelected != null)
            VisualStateManager.GoToState(lastElementSelected, "UnSelected");

        VisualStateManager.GoToState((StackLayout)sender, "Selected");

        lastElementSelected = (StackLayout)sender;
    }
}
public class Info
{
    public string Title { get; set; }
}

代码隐藏:

   <ContentPage.Resources>
    <Style TargetType="StackLayout">
        <Setter Property="VisualStateManager.VisualStateGroups">
            <VisualStateGroupList>
                <VisualStateGroup>
                    <VisualState x:Name="Selected">
                        <VisualState.Setters>
                            <Setter Property="BackgroundColor" Value="Accent" />
                        </VisualState.Setters>
                    </VisualState>
                    <VisualState x:Name="UnSelected">
                        <VisualState.Setters>
                            <Setter Property="BackgroundColor" Value="Blue" />
                        </VisualState.Setters>
                    </VisualState>
                </VisualStateGroup>
            </VisualStateGroupList>
        </Setter>
    </Style>
</ContentPage.Resources>
<ContentPage.Content>
    <StackLayout
        Padding="10"
        HorizontalOptions="FillAndExpand"
        VerticalOptions="FillAndExpand">
        <CollectionView
            x:Name="MenuCollection"
            ItemsSource="{Binding Infos}"
            SelectionChanged="MenuCollection_OnSelectionChanged"
            SelectionMode="Single"
            VerticalScrollBarVisibility="Always">

            <CollectionView.ItemsLayout>
                <GridItemsLayout
                    HorizontalItemSpacing="15"
                    Orientation="Vertical"
                    Span="2"
                    VerticalItemSpacing="15" />
            </CollectionView.ItemsLayout>

            <CollectionView.ItemTemplate>
                <DataTemplate>

                    <StackLayout BackgroundColor="Blue">
                        <Label Text="{Binding Title}" />
                        <StackLayout.GestureRecognizers>
                            <TapGestureRecognizer Tapped="TapGestureRecognizer_Tapped" />
                        </StackLayout.GestureRecognizers>
                    </StackLayout>

                </DataTemplate>
            </CollectionView.ItemTemplate>
        </CollectionView>
    </StackLayout>
</ContentPage.Content>
 public partial class Page4 : ContentPage
{
    public ObservableCollection<Info> Infos { get; set; }
    public Page4()
    {
        InitializeComponent();
        Infos = new ObservableCollection<Info>
    {
        new Info(){ Title="A"},
        new Info(){ Title="B"},
        new Info(){ Title="B"},
        new Info(){ Title="C"},
        new Info(){ Title="D"}
    };

        this.BindingContext = this;
    }
    StackLayout lastElementSelected;
    private void MenuCollection_OnSelectionChanged(object sender, SelectionChangedEventArgs e)
    {      

     

    }

    private void TapGestureRecognizer_Tapped(object sender, EventArgs e)
    {

        if (lastElementSelected != null)
            VisualStateManager.GoToState(lastElementSelected, "UnSelected");

        VisualStateManager.GoToState((StackLayout)sender, "Selected");

        lastElementSelected = (StackLayout)sender;
    }
}
public class Info
{
    public string Title { get; set; }
}
公共部分类第4页:内容页
{
公共可观测集合信息{get;set;}
公共页4()
{
初始化组件();
Infos=新的可观察集合
{
新信息(){Title=“A”},
新信息(){Title=“B”},
新信息(){Title=“B”},
新信息(){Title=“C”},
新信息(){Title=“D”}
};
this.BindingContext=this;
}
StackLayout lastElementSelected;
private void MenuCollection_OnSelectionChanged(对象发送者,SelectionChangedEventArgs e)
{      
}
私有无效TapGestureRecognitor_(对象发送方,事件参数e)
{
如果(lastElementSelected!=null)
VisualStateManager.gostate(lastElementSelected,“UnSelected”);
VisualStateManager.GoToState((StackLayout)发送方,“选定”);
lastElementSelected=(StackLayout)发送方;
}
}
公共类信息
{
公共字符串标题{get;set;}
}
屏幕截图:

   <ContentPage.Resources>
    <Style TargetType="StackLayout">
        <Setter Property="VisualStateManager.VisualStateGroups">
            <VisualStateGroupList>
                <VisualStateGroup>
                    <VisualState x:Name="Selected">
                        <VisualState.Setters>
                            <Setter Property="BackgroundColor" Value="Accent" />
                        </VisualState.Setters>
                    </VisualState>
                    <VisualState x:Name="UnSelected">
                        <VisualState.Setters>
                            <Setter Property="BackgroundColor" Value="Blue" />
                        </VisualState.Setters>
                    </VisualState>
                </VisualStateGroup>
            </VisualStateGroupList>
        </Setter>
    </Style>
</ContentPage.Resources>
<ContentPage.Content>
    <StackLayout
        Padding="10"
        HorizontalOptions="FillAndExpand"
        VerticalOptions="FillAndExpand">
        <CollectionView
            x:Name="MenuCollection"
            ItemsSource="{Binding Infos}"
            SelectionChanged="MenuCollection_OnSelectionChanged"
            SelectionMode="Single"
            VerticalScrollBarVisibility="Always">

            <CollectionView.ItemsLayout>
                <GridItemsLayout
                    HorizontalItemSpacing="15"
                    Orientation="Vertical"
                    Span="2"
                    VerticalItemSpacing="15" />
            </CollectionView.ItemsLayout>

            <CollectionView.ItemTemplate>
                <DataTemplate>

                    <StackLayout BackgroundColor="Blue">
                        <Label Text="{Binding Title}" />
                        <StackLayout.GestureRecognizers>
                            <TapGestureRecognizer Tapped="TapGestureRecognizer_Tapped" />
                        </StackLayout.GestureRecognizers>
                    </StackLayout>

                </DataTemplate>
            </CollectionView.ItemTemplate>
        </CollectionView>
    </StackLayout>
</ContentPage.Content>
 public partial class Page4 : ContentPage
{
    public ObservableCollection<Info> Infos { get; set; }
    public Page4()
    {
        InitializeComponent();
        Infos = new ObservableCollection<Info>
    {
        new Info(){ Title="A"},
        new Info(){ Title="B"},
        new Info(){ Title="B"},
        new Info(){ Title="C"},
        new Info(){ Title="D"}
    };

        this.BindingContext = this;
    }
    StackLayout lastElementSelected;
    private void MenuCollection_OnSelectionChanged(object sender, SelectionChangedEventArgs e)
    {      

     

    }

    private void TapGestureRecognizer_Tapped(object sender, EventArgs e)
    {

        if (lastElementSelected != null)
            VisualStateManager.GoToState(lastElementSelected, "UnSelected");

        VisualStateManager.GoToState((StackLayout)sender, "Selected");

        lastElementSelected = (StackLayout)sender;
    }
}
public class Info
{
    public string Title { get; set; }
}