【问题标题】:VisualStateManager not work with SelectionChanged in a CollectionView on Xamarin FormsVisualStateManager 不适用于 Xamarin 表单上 CollectionView 中的 SelectionChanged
【发布时间】:2021-01-15 12:00:36
【问题描述】:

当我在集合视图中添加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 停止工作。有谁知道为什么?

【问题讨论】:

    标签: xaml xamarin.forms collectionview visualstatemanager


    【解决方案1】:

    问题导致SelectionChanged 无法获取选定的元素类型,如StackLayoutLabel。你可以改用TapGestureRecognizer

    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; }
    }
    

    截图:

    【讨论】:

      猜你喜欢
      • 2021-10-26
      • 2020-08-12
      • 2020-03-14
      • 2019-04-29
      • 2017-12-21
      • 2017-06-05
      • 2021-12-01
      • 2023-01-24
      • 2021-01-22
      相关资源
      最近更新 更多