Xamarin Forms CollectionView TapGestureRecognizer 未在标签上触发 [英] Xamarin Forms CollectionView TapGestureRecognizer not firing on label

查看:26
本文介绍了Xamarin Forms CollectionView TapGestureRecognizer 未在标签上触发的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a XF app with the following collection view defined. The 2nd label has a TapGestureRecognizer that doesn't fire DoSomethingInteresting in the model when I tap the label (trying this on Android). Can someone see what the issue is please?

工作样本可以从这里克隆.

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

推荐答案

我已经下载了你的示例,请看下面的步骤.

I have download your sample, please take a look the following step.

1.为 MainPage bindingContext 绑定 MainpageModel,在 MainPage.cs 中添加这一行.

1.Binding MainpageModel for MainPage bindingContext, add this line in MainPage.cs.

 this.BindingContext = new MainPageModel();

2.将Collectionview命名为collection1,然后修改label命令.

2.Name Collectionview as collection1, then modify label command.

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

然后你可以再试一次.

这篇关于Xamarin Forms CollectionView TapGestureRecognizer 未在标签上触发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆