首页 > 解决方案 > IOS中xamarin.forms中MR.grid(在listview内)的长按事件调用两次

问题描述

我正在使用 MR.Gestures 插件来处理单击和长按事件。

<ListView Grid.Row="1"  x:Name="lstProducts" HasUnevenRows="True" SeparatorVisibility="None" BackgroundColor="Transparent" IsPullToRefreshEnabled="True" Refreshing="lstProducts_Refreshing">
                    <ListView.ItemTemplate>
                        <DataTemplate >
                            <ViewCell>
                                <ViewCell.View>
                                    <Grid>
                                        <Grid.Margin>
                                            <OnIdiom x:TypeArguments="Thickness" Phone="10" Tablet="20"/>
                                        </Grid.Margin>
                                        <Grid.ColumnDefinitions>
                                            <ColumnDefinition Width="*"/>
                                            <ColumnDefinition Width="*"/>
                                        </Grid.ColumnDefinitions>
                                        <Grid.RowDefinitions>
                                            <RowDefinition Height="{Binding grdRowHeight}"/>
                                        </Grid.RowDefinitions>
                                        <mr:Grid x:Name="grd_left_product" Grid.Column="0" Grid.Row="0" ClassId="{Binding Left_attachedFilePath}" Style="{StaticResource MostOuterGridForBorder}"
                                                 Tapped="grd_left_product_Tapped" 
                                                 LongPressed="grd_left_product_DoubleTapped">
                                            <Grid Style="{StaticResource OuterGrid}">
                                                <ffimageloading:CachedImage Source="{Binding Left_thumbnailImage}" Style="{StaticResource ThumbNailImageStyle}" />
                                                <StackLayout VerticalOptions="EndAndExpand" >
                                                    <custom:CustomWrapLabel Text="{Binding Left_name}" Style="{StaticResource CustomWrapLabelStyle}"/>
                                                </StackLayout>
                                                <StackLayout IsVisible="{Binding Left_IsChecked}" Style="{StaticResource MultipleDownloadStack}">
                                                    <ffimageloading:CachedImage Source="check_ic.png" Style="{StaticResource MultipleDownloadCheckIcon}" />
                                                </StackLayout>
                                            </Grid>
                                        </mr:Grid>
                                        <mr:Grid x:Name="grd_right_product" Grid.Column="1" Grid.Row="0" ClassId="{Binding Right_attachedFilePath}" Style="{StaticResource MostOuterGridForBorder}" IsVisible="{Binding Right_grd_product_visibility}"
                                                 Tapped="grd_right_product_Tapped" 
                                                 LongPressed="grd_right_product_DoubleTapped">
                                            <Grid Style="{StaticResource OuterGrid}">
                                                <ffimageloading:CachedImage Source="{Binding Right_thumbnailImage}" Style="{StaticResource ThumbNailImageStyle}" />
                                                <StackLayout VerticalOptions="EndAndExpand" >
                                                    <custom:CustomWrapLabel Text="{Binding Right_name}" Style="{StaticResource CustomWrapLabelStyle}" />
                                                </StackLayout>
                                                <StackLayout IsVisible="{Binding Right_IsChecked}" Style="{StaticResource MultipleDownloadStack}">
                                                    <ffimageloading:CachedImage Source="check_ic.png" Style="{StaticResource MultipleDownloadCheckIcon}" />
                                                </StackLayout>
                                            </Grid>
                                        </mr:Grid>
                                    </Grid>
                                </ViewCell.View>
                            </ViewCell>
                        </DataTemplate>
                    </ListView.ItemTemplate>
                </ListView>

在下面的函数调用服务后,将我的数据分成两列并返回可观察的集合。

private ObservableCollection<ProductData> BifurcationInLeftRight(List<ResponseDetail> lstResponseDetail)
        {
            lstcollectionproductDatas = new ObservableCollection<ProductData>();
            for (int i = 0; i < lstResponseDetail.Count; i++)
            {
                var items = new ProductData();
                items.Left_IsChecked = false;
                items.Right_IsChecked = false;
                items.Left_productId = lstResponseDetail[i].productId;
                items.Left_name = lstResponseDetail[i].name;
                items.Left_attachedFilePath = lstResponseDetail[i].attachedFilePath;
                items.Left_attachedLink = lstResponseDetail[i].attachedLink;
                items.Left_thumbnailImage = lstResponseDetail[i].thumbnailImage;

                i++;
                if (i < lstResponseDetail.Count)
                {
                    items.Right_productId = lstResponseDetail[i].productId;
                    items.Right_name = lstResponseDetail[i].name;
                    items.Right_attachedFilePath = lstResponseDetail[i].attachedFilePath;
                    items.Right_attachedLink = lstResponseDetail[i].attachedLink;
                    items.Right_thumbnailImage = lstResponseDetail[i].thumbnailImage;
                    items.Right_grd_product_visibility = true;
                }
                else
                {
                    items.Right_productId = null;
                    items.Right_name = null;
                    items.Right_attachedFilePath = null;
                    items.Right_attachedLink = null;
                    items.Right_thumbnailImage = null;
                    items.Right_grd_product_visibility = false;

                    items.grdRowHeight = ThumbNailHeight;
                    lstcollectionproductDatas.Add(items);
                    break;
                }
                items.grdRowHeight = ThumbNailHeight;
                lstcollectionproductDatas.Add(items);
            }
            return lstcollectionproductDatas;
        }

长按我检查了项目并重新绑定了我的整个项目源。“grd_left_product_DoubleTapped”和“grd_right_product_DoubleTapped”是我长按的事件

private void grd_left_product_DoubleTapped(object sender, MR.Gestures.LongPressEventArgs e)
        {
            try
            {
                var productId = sender as Grid;
                if (!string.IsNullOrEmpty(productId.ClassId) && lstcollectionproductDatas != null && lstcollectionproductDatas.Count > 0)
                {
                    var selectedObject = lstcollectionproductDatas.Where(d => d.Left_attachedFilePath == productId.ClassId).FirstOrDefault();
                    if (selectedObject != null)
                    {
                        int index = lstcollectionproductDatas.IndexOf(selectedObject);
                        if (!selectedObject.Left_IsChecked)
                            lstcollectionproductDatas[index].Left_IsChecked = true;
                        else
                            lstcollectionproductDatas[index].Left_IsChecked = false;
                    }
                    lstProducts.ItemsSource = null;
                    lstProducts.ItemsSource = lstcollectionproductDatas;
                }
                else
                    new ShowSnackbar(AppResources.Validation_FileNotFound_Message);
            }
            catch (Exception)
            {
                new ShowSnackbar(AppResources.Validation_Exception);
            }
        }

问题 :

此代码在 android 中运行良好,但在 IOS 中,当我长按一个项目时,它会检查但当我长按不同行中的另一个项目时,长按事件调用两次。

第一次,为之前的 Checked 项目,第二次为我们要检查的项目。这就是为什么已经检查过的第一次变为未检查的原因。

我的问题是为什么长按事件在 MR.Gestures 中调用了两次?

共有三行(索引 - 0,1,2)

  1. 索引 2 中的左侧项目已选中

  2. 现在我长按 index-1 的左侧项目。

  3. 长按事件调用两次。

  4. 第一次为 index-2 并取消选中所选项目。

  5. 第二次为 index-1 并检查。

标签: xamarinxamarin.formsxamarin.iosmr.gestures

解决方案


你看过这个吗? 来自 GitHub 的免费 G​​estureSample 应用程序

longpressed 事件在开始和结束时调用一次,可以区分。当状态等于结束时直接返回,并在状态等于开始时做一些事情(如刷新 UI)。


推荐阅读