Xamarin Forms CollectionView TapGestureRecognitor未在标签上激活

Xamarin Forms CollectionView TapGestureRecognitor未在标签上激活,xamarin,xamarin.forms,Xamarin,Xamarin.forms,我有一个定义了以下集合视图的XF应用程序。第二个标签有一个TapGestureRecognitor,当我点击标签时,它不会在模型中激发DoSomethingInteresting(在Android上尝试)。有人能看看问题是什么吗 一个工作样本可以是 XAML <?xml version="1.0" encoding="utf-8" ?> <ContentPage xmlns="http://xamarin.com/schemas/2014/forms"

我有一个定义了以下集合视图的XF应用程序。第二个标签有一个TapGestureRecognitor,当我点击标签时,它不会在模型中激发
DoSomethingInteresting
(在Android上尝试)。有人能看看问题是什么吗

一个工作样本可以是

XAML

<?xml version="1.0" encoding="utf-8" ?>
    <ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
                 xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
                 xmlns:local="clr-namespace:SampleApp"
                 x:Class="SampleApp.MainPage">

        <StackLayout>
            <CollectionView ItemsSource="{Binding GaugeSites}">
                <CollectionView.ItemTemplate>
                    <DataTemplate>
                        <Grid>
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="*" />
                                <ColumnDefinition Width="Auto" />
                            </Grid.ColumnDefinitions>
                            <Grid.RowDefinitions>
                                <RowDefinition Height="40" />
                            </Grid.RowDefinitions>

                            <Label Grid.Column="0"
                                   Text="{Binding Description}"
                                   FontSize="20"
                                   Margin="10"
                                   TextColor="Black"
                                   FontAttributes="Bold" />

                            <Label Grid.Column="1" 
                                   Margin="10"
                                   FontSize="20" 
                                   Text="Click Me!">
                                <Label.GestureRecognizers>
                                    <TapGestureRecognizer Command="{Binding DoSomethingInteresting}" />
                                </Label.GestureRecognizers>
                            </Label>
                        </Grid>
                    </DataTemplate>
                </CollectionView.ItemTemplate>

            </CollectionView>
        </StackLayout>
    </ContentPage>

型号

namespace SampleApp
{
    public class MainPageModel : FreshBasePageModel
    {
        public MainPageModel() : base()
        {
            GaugeSites = new List<GaugeSite>();

            for (var index = 1; index <= 5; index++)
            {
                GaugeSites.Add(new GaugeSite()
                {
                    Description = $"Gauge Site {index}"
                });
            }
        }

        public List<GaugeSite> GaugeSites { get; set; }

        public Command DoSomethingInteresting
        {
            get
            {
                return new Command(() =>
                {

                });
            }
        }
    }

    [AddINotifyPropertyChangedInterface]
    public class GaugeSite
    {
        public string Description { get; set; }
    }
}
namespace SampleApp
{
公共类MainPageModel:FreshBasePageModel
{
public MainPageModel():base()
{
GaugeSites=新列表();
对于(var指数=1;指数
{
});
}
}
}
[AddInnotifyPropertyChangedInterface]
公共级高仪
{
公共字符串说明{get;set;}
}
}

我已经下载了您的示例,请查看以下步骤

1.绑定MainpageModel对于MainPageBindingContext,请在MainPage.cs中添加此行

 this.BindingContext = new MainPageModel();
2.将Collectionview命名为collection1,然后修改label命令

 <CollectionView x:Name="collection1" ItemsSource="{Binding GaugeSites}">
        <CollectionView.ItemTemplate>
            <DataTemplate>
                <Grid>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="*" />
                        <ColumnDefinition Width="Auto" />
                    </Grid.ColumnDefinitions>
                    <Grid.RowDefinitions>
                        <RowDefinition Height="40" />
                    </Grid.RowDefinitions>

                    <Label
                        Grid.Column="0"
                        Margin="10"
                        FontAttributes="Bold"
                        FontSize="20"
                        Text="{Binding Description}"
                        TextColor="Black" />

                    <Label
                        Grid.Column="1"
                        Margin="10"
                        FontSize="20"
                        Text="Click Me!">
                        <Label.GestureRecognizers>
                            <TapGestureRecognizer Command="{Binding BindingContext.DoSomethingInteresting, Source={x:Reference collection1}}" />
                        </Label.GestureRecognizers>
                    </Label>
                </Grid>
            </DataTemplate>
        </CollectionView.ItemTemplate>

    </CollectionView>


然后你可以再试一次。

也许这是多余的,但我会抓住机会。如其他答案所示,您需要将TapGestureRecognitor中的源设置为CollectionView的名称。但是,了解哪个GaugeSite实例与TabgestureRecognitor被点击的CollectionView项相关联可能很有用。要添加此信息,请添加CommandParameter,即

    <Label.GestureRecognizers>
        <TapGestureRecognizer Command="{Binding BindingContext.DoSomethingInteresting, 
                              CommandParameter="{Binding}"
                              Source={x:Reference YourCollectionName}}" />
    </Label.GestureRecognizers>

希望这有帮助

标签的绑定上下文是一个单独的GaugeSite,它没有DoSomethingInteresting命令。
DoSomethingInteresting = new Command<GaugeSite>((a) => DoSomething(a));
void DoSomething(GaugeSite gaugeSite)
{
   // ....
}