c# - GestureRecognizers 在 ListView 中不起作用

标签 c# xaml xamarin xamarin.forms

我有一个ListView如下

<ListView
        Grid.Row="0"
        Margin="0"
        x:Name="ItemsListView"
        ItemsSource="{Binding SourceItems}"
        VerticalOptions="FillAndExpand"
        HasUnevenRows="false"
        RefreshCommand="{Binding LoadItemsCommand}"
        IsPullToRefreshEnabled="true"
        IsRefreshing="{Binding IsBusy}"
        ItemSelected="OnItemSelected"
        IsVisible="{Binding ShowListView}"
        RowHeight="55">
        <ListView.ItemTemplate>
            <DataTemplate>
                <ViewCell>
                    <Grid
                        Margin="15,0,0,0"
                        Padding="0"
                        RowSpacing="0"
                        ColumnSpacing="0">
                        <Grid.RowDefinitions>
                            <RowDefinition
                                Height="*" />
                        </Grid.RowDefinitions>
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition
                                Width="1*" />
                            <ColumnDefinition
                                Width="7*" />
                            <ColumnDefinition
                                Width="1*" />
                            <ColumnDefinition
                                Width="1*" />
                        </Grid.ColumnDefinitions>
                        <Image
                            VerticalOptions="CenterAndExpand"
                            HorizontalOptions="CenterAndExpand"
                            HeightRequest="35"
                            WidthRequest="35"
                            Grid.Row="0"
                            Grid.Column="0"
                            Aspect="AspectFit"
                            Source="{Binding Icon}">
                        </Image>
                        <StackLayout
                            VerticalOptions="CenterAndExpand"
                            Spacing="0"
                            CompressedLayout.IsHeadless="true"
                            Margin="15,0,0,0"
                            Grid.Row="0"
                            Grid.Column="1">
                            <Label
                                VerticalTextAlignment="Start"
                                Text="{Binding Name}"
                                FontAttributes="Bold"
                                LineBreakMode="NoWrap"
                                Style="{DynamicResource ListItemTextStyle}"
                                FontSize="16" />
                            <Label
                                VerticalTextAlignment="Start"
                                Text="{Binding Description}"
                                LineBreakMode="NoWrap"
                                Style="{DynamicResource ListItemDetailTextStyle}"
                                FontSize="13" />
                        </StackLayout>
                        <Image
                            Grid.Row="0"
                            Grid.Column="3"
                            HeightRequest="20"
                            WidthRequest="20"
                            VerticalOptions="CenterAndExpand"
                            HorizontalOptions="StartAndExpand"
                            Aspect="AspectFit"
                            Source="{Binding Icon}" />
                        <Image
                            BackgroundColor="Lime"
                            Grid.Row="0"
                            Grid.Column="2"
                            InputTransparent="false"
                            Margin="0,0,10,0"
                            HeightRequest="20"
                            WidthRequest="20"
                            VerticalOptions="CenterAndExpand"
                            HorizontalOptions="StartAndExpand"
                            Aspect="AspectFit"
                            Source="ic_two">
                            <Image.GestureRecognizers>
                                <TapGestureRecognizer
                                    Command="{Binding OnFavouriteCommand}"
                                    CommandParameter="{Binding .}"
                                    NumberOfTapsRequired="1">
                                </TapGestureRecognizer>
                            </Image.GestureRecognizers>
                        </Image>
                    </Grid>
                </ViewCell>
            </DataTemplate>
        </ListView.ItemTemplate>
    </ListView>

在我的 ViewModel 中有

public ICommand OnFavouriteCommand { get; set; }

public MyViewModel()
{           
  OnFavouriteCommand = new Command<Object>(OnFavourite);
}

void OnFavourite(Object ob)
{
  Debug.WriteLine(ob);
}

我在 OnFavourite 上没有得到 Break Point 命中。我不知道我在这里错过了什么?这个想法是让手势识别器附加到每个图像,并仅传递绑定(bind)到该行的项目。

我刚刚注意到,如果我带上

<Image
            BackgroundColor="Lime"
            Grid.Row="1"
            InputTransparent="false"
            Margin="0,0,10,0"
            HeightRequest="20"
            WidthRequest="20"
            VerticalOptions="CenterAndExpand"
            HorizontalOptions="StartAndExpand"
            Aspect="AspectFit"
            Source="ic_favourites">
            <Image.GestureRecognizers>
                <TapGestureRecognizer
                    Command="{Binding OnFavouriteCommand}"
                    CommandParameter="{Binding .}"
                    NumberOfTapsRequired="1">
                </TapGestureRecognizer>
            </Image.GestureRecognizers>
        </Image>

ListView 的外面,断点确实被击中了!。让我挠头……:|

最佳答案

虽然 Adit 写的是真实的,但让我给你一些背景知识。在 DataTemplate 中,单元格(以及它们的子级)的 BindingContext 未设置为父级(即ListView),但对于 ListView.ItemSource 中的每个元素,都会创建一个单元格,并将其 BindingContext 设置为该元素。否则,您将无法将 ViewCell 子级的属性绑定(bind)到您的字段,例如 IconNameDescription

在 WPF 中,可以绑定(bind)到父 DataContext(参见 here),但据我所知,这在 Xamarin.Forms 中是不可能的(参见 here)。因此,您必须明确引用父级,即通过 x:Name="Page" (或您想要给它的任何名称)分配一个名称,然后通过 Source 绑定(bind)中的属性

Command="{Binding Path=BindingContext.OnFavouriteCommand,Source={x:Reference Page}}"

由于您设置的绑定(bind)源不是 PageBindingContext,而是 Page,因此您必须添加 BindingContex 到路径。此外,为了将当前单元格表示的元素传递给命令,您必须设置

CommandParameter="{Binding .}"

它将 CommandParameter 绑定(bind)到单元格引用的元素(您已经这样做了)。

关于c# - GestureRecognizers 在 ListView 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49842211/

相关文章:

c# - 如何在混合模式下对存储库使用依赖注入(inject)

c# - 如何在 Visual Studio 中使用外部库?

c# - 使用这种类型的外部库后出现错误 "Type does not exist in the namespace"

ios - Visual Studio中的配置文件下拉列表与Xcode中可用的配置文件列表不匹配

c# - 在Gstreamer(C#)中使用管道播放音频

c# - HttpClient PutAsync 使用 RestAPI 更新字段

c# - 在 MFC 应用程序中托管 Windows 窗体 (C#) (VC++,VS6.0)

c# - WPF ItemsControl 中的重叠项

android - 让ObtainStyledAttributes在xamarin for Android中正常工作

xamarin - Xamarin 中的网格是否有默认边距或填充?