【问题标题】:how to clear collectionview selection in xamarin如何清除xamarin中的collectionview选择
【发布时间】:2021-05-28 17:02:14
【问题描述】:

在第 1 页上,我有一个带有文本/字符串项目列表的集合视图。如果您点击一个项目,它将获取当前点击的文本/字符串并发送到 Page#2。听起来很简单

我遇到的问题:如果您点击 item#1,那么它会将 item#1 发送到 page2,这部分工作正常。但是在第 2 页,如果您点击返回按钮,然后再次点击第 1 项.. 比什么都没有发生,它不会转到第 2 页

修复:我认为我需要以某种方式清除点击选择,然后将项目发送到第 2 页。但我不知道该怎么做

在第 1 页上,我有一个简单的收藏视图。集合视图包含文本/字符串列表

        <CollectionView ItemsSource="{Binding MyListItem}"
                        SelectionMode="Single"
                        SelectionChanged="CollectionView_SelectionChanged">
            <CollectionView.ItemTemplate>
                <DataTemplate>
                    <ContentView>
                        <!-- Body -->
                        <Grid Padding="0">
                            <Frame CornerRadius="3" BorderColor="#f2f4f5" HasShadow="True">
                                <StackLayout Orientation="Horizontal">
                                    <Image Source="icon_about"
                                               WidthRequest="25"  />
                                    <StackLayout VerticalOptions="Center">
                                        <Label VerticalOptions="Center"
                                                    FontSize="16" 
                                                    Text="{Binding .}" />
                                    </StackLayout>
                                </StackLayout>
                            </Frame>

处理选择的后端代码是:

    private async void CollectionView_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        var previous = e.PreviousSelection.FirstOrDefault();
        var current = e.CurrentSelection.FirstOrDefault();

        var route = $"{ nameof(Page2) }?URLCardType={current}";
        await Shell.Current.GoToAsync(route);
        
        //clear selection 
        ((CollectionView)sender).SelectedItem = null;
    }

更新 ((CollectionView)sender).SelectedItem = null; 修复了清除所选项目的问题,但CollectionView_SelectionChanged 方法是单击运行两次。为什么?这是我所有的代码

【问题讨论】:

  • 将SelectedItem设为空
  • @Jason,感谢您回复我。我将 selectedItem 添加到空代码,但没有值传递到下一页。我也注意到单击,我的方法CollectionView_SelectionChanged 运行两次。这可能就是为什么将 current 设置为 null
  • 试着把它放在导航之后
  • 那行得通。但是CollectionView_SelectionChanged 仍然会在单击时运行两次。所以它试图去 page2 两次

标签: c# xamarin xamarin.forms collectionview


【解决方案1】:

@jason 谢谢,这对我有用。我只需要检查选择项是否为空而不是什么都不做

  private async void CollectionView_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
         var MyCollectionView = sender as CollectionView;  
            if (MyCollectionView.SelectedItem == null)
                return;

        var previous = e.PreviousSelection.FirstOrDefault();
        var current = e.CurrentSelection.FirstOrDefault();

        var route = $"{ nameof(Page2) }?URLCardType={current}";
        await Shell.Current.GoToAsync(route);
        
        //clear selection 
       MyCollectionView.SelectedItem = null;
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-04-02
    • 2020-10-18
    • 2012-07-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多