繁体   English   中英

如何清除 xamarin 中的 collectionview 选择

[英]how to clear collectionview selection in xamarin

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

我遇到的问题:如果您点击 item#1,那么它会将 item#1 发送到 page2,这部分工作正常。 但是在第 2 页上,如果您点击返回按钮,然后再次点击第 1 项.. 没有任何反应,它不会 go 到第 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方法在单击时会运行两次。 为什么? 这是我所有的代码

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

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

这是一个对我有用的简单解决方案。 设置为 null 并没有成功,它导致了各种奇怪的东西。

  private void CollectionView_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        var itemselected = e.CurrentSelection[0] as ProductsItem;
       if (itemselected != null)
        Shell.Current.GoToAsync(nameof(SelectedProductPage));

       //Setting the selected item to an emptyviewproperty after navigation
        CollectionList.SelectedItem = SelectableItemsView.EmptyViewProperty;
    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM