CollectionView中的TapGestureRecognizer

时间:2019-12-02 20:15:59

标签: xamarin.forms xamarin.ios

我创建了一个collectionview,并在此控件内使用TapGestureRecognizer创建了一个网格,但是在iOS上,该命令未触发...在android上运行良好。 第二个问题是,此网格内的按钮也不起作用。(在android上,一切正常。 这是控件:

<CollectionView ItemsSource="{Binding Claims}"
                    SelectionMode="None"
                    SelectedItem="{Binding SelectedClaim}">
        <CollectionView.ItemsLayout>
            <LinearItemsLayout ItemSpacing="5"
                               Orientation="Vertical"/>
        </CollectionView.ItemsLayout>
        <CollectionView.ItemTemplate>
            <DataTemplate>
                <Frame Padding="0"
                       InputTransparent="True"
                       Margin="0">
                    <Frame Margin="10,5"
                           Padding="5"
                           InputTransparent="True"
                           CornerRadius="10"
                           HeightRequest="160"
                           HasShadow="True"
                           BorderColor="Gray"
                           Visual="Default">

                        <Grid VerticalOptions="Center">
                            <d:Grid.BindingContext>
                                <models:Claim/>
                            </d:Grid.BindingContext>

                            <Grid.GestureRecognizers>
                                <TapGestureRecognizer Command="{Binding Source={RelativeSource AncestorType={x:Type viewmodels:ClaimsOverViewViewModel}}, Path=ViewCardCommand}"
                                                      CommandParameter="{Binding}"/>
                            </Grid.GestureRecognizers>


                            <Grid.RowDefinitions>
                                <RowDefinition Height="75"/>
                                <RowDefinition Height="4"/>
                                <RowDefinition Height="Auto"/>
                            </Grid.RowDefinitions>

                            <Grid.ColumnDefinitions>
                                <ColumnDefinition/>
                                <ColumnDefinition/>
                            </Grid.ColumnDefinitions>

                            <StackLayout Grid.Row="0"
                                         InputTransparent="True"
                                         Grid.Column="0"
                                         Grid.ColumnSpan="2">

                                <StackLayout.GestureRecognizers>
                                    <TapGestureRecognizer Command="{Binding Source={RelativeSource AncestorType={x:Type viewmodels:ClaimsOverViewViewModel}}, Path=ViewCardCommand}"
                                                          CommandParameter="{Binding}"/>
                                </StackLayout.GestureRecognizers>

                                <Label Text="Entwurf"
                                       TextColor="Red"
                                       FontSize="Medium"
                                       Margin="0,-20,0,0"
                                       IsVisible="{Binding IsNotValid}"
                                       HorizontalTextAlignment="Start"
                                       VerticalTextAlignment="Start"/>

                                <Label Text="{Binding PolicyNumber}"
                                       FontAttributes="Bold"
                                       FontSize="Large"
                                       VerticalTextAlignment="Start"
                                       HorizontalTextAlignment="Start"/>

                                <Label Text="{Binding CustomerName}"
                                       FontSize="Small"
                                       VerticalTextAlignment="End"
                                       HorizontalTextAlignment="Start"/>

                            </StackLayout>

                            <BoxView Grid.Row="1"
                                     Grid.Column="0"
                                     Grid.ColumnSpan="2"
                                     BackgroundColor="Gray"
                                     InputTransparent="True"
                                     HeightRequest="2"
                                     HorizontalOptions="Fill"/>

                            <Label Grid.Row="2"
                                   Grid.Column="0"
                                   Grid.ColumnSpan="2"
                                   Text="{Binding Date}"
                                   VerticalTextAlignment="Start"
                                   VerticalOptions="Fill"
                                   HorizontalOptions="Fill"/>

                            <StackLayout Grid.Column="1"
                                         InputTransparent="True">

                                <Button Text="Löschen"
                                        Command="{Binding Source={RelativeSource AncestorType={x:Type viewmodels:ClaimsOverViewViewModel}}, Path=DeleteCommand}"
                                        CommandParameter="{Binding}"
                                        BorderColor="Red"
                                        CornerRadius="10"
                                        VerticalOptions="Center"
                                        BorderWidth="3"
                                        BackgroundColor="White"
                                        WidthRequest="80"
                                        HeightRequest="30"
                                        Padding="0"
                                        Margin="0,-10,0,10"
                                        HorizontalOptions="End"/>

                                <Button Text="Senden"
                                        Command="{Binding Source={RelativeSource AncestorType={x:Type viewmodels:ClaimsOverViewViewModel}}, Path=SendClaimCommand}"
                                        CommandParameter="{Binding}"
                                        BorderColor="Green"
                                        CornerRadius="10"
                                        VerticalOptions="Center"
                                        BorderWidth="3"
                                        BackgroundColor="White"
                                        WidthRequest="80"
                                        HeightRequest="30"
                                        Padding="0"
                                        Margin="0"
                                        HorizontalOptions="End"/>
                            </StackLayout>

                        </Grid>
                    </Frame>

                </Frame>
            </DataTemplate>
        </CollectionView.ItemTemplate>

    </CollectionView>

我已经在不同的地方(堆栈布局,网格,框架)尝试了拍打识别器,并且还尝试了IpnutTransparent-Property ...

1 个答案:

答案 0 :(得分:0)

我找到了解决方案。 问题是,网格或Stacklayout中的元素隐藏了Stacklayout / Grid的GestureRecognizer。解决此问题的方法是,禁用具有属性“ InputTransparent”的子元素的GestureRecognizer,如下所示:

<StackLayout Grid.Row="0"
                                         Grid.Column="0"
                                         Grid.ColumnSpan="2">
                                <StackLayout.GestureRecognizers>
                                    <TapGestureRecognizer NumberOfTapsRequired="2"
                                                              Command="{Binding Source={RelativeSource AncestorType={x:Type
                                    viewmodels:ClaimsOverViewViewModel}}, Path=ShowDetailsCommand}"
                                                              CommandParameter="{Binding}"
                                                              />
                                </StackLayout.GestureRecognizers>

                                <Label Text="Entwurf"
                                       TextColor="Red"
                                       FontSize="Medium"
                                       Margin="0,-20,0,0"
                                       IsVisible="{Binding IsNotValid}"
                                       HorizontalTextAlignment="Start"
                                       VerticalTextAlignment="Start"
                                       InputTransparent="True"/>

                                <Label Text="{Binding PolicyNumber}"
                                       FontAttributes="Bold"
                                       FontSize="Large"
                                       VerticalTextAlignment="Start"
                                       HorizontalTextAlignment="Start"
                                       InputTransparent="True">
                                </Label>

                                <Label Text="{Binding CustomerName}"
                                       FontSize="Small"
                                       VerticalTextAlignment="End"
                                       HorizontalTextAlignment="Start"
                                       InputTransparent="True"/>

                            </StackLayout>