c# - 标签 TapGesture 不会触发 Xamarin Forms

标签 c# xaml xamarin xamarin.forms

我试图将 TapGestureRecognizer 添加到 Label

 SliderAbout.GestureRecognizers.Add(new TapGestureRecognizer
 {
     Command = new Command(() => OpenAboutAppAsync()),
 });

SliderAbout 是我的 Label,它在 Xaml 中设置并且可以正常工作。

 <ScrollView Grid.Row="1">
                <Grid VerticalOptions="Start" HorizontalOptions="Start" Margin="20,20,0,0">
                    <Grid.RowDefinitions>
                        <RowDefinition Height="60"/>
                    </Grid.RowDefinitions>

                    <Label x:Name="SliderAbout" 
                           Text="Über die APP" 
                           Grid.Row="2"  
                           TextColor="White" 
                           FontFamily="Open Sans Light" 
                           FontSize="Medium"/>    
                </Grid>
            </ScrollView>

代码也会运行(我把它放在类构造函数中)

但是当我点击标签时该方法不会触发...为什么它不会触发?

最佳答案

如果您使用 mvvm 方法,您可以将手势识别器直接添加到 xaml 和代码隐藏或 View 模型中的操作。在你的情况下,它会是这样的:

<Label x:Name="SliderAbout" Text="Über die APP" Grid.Row="2"  TextColor="White" FontFamily="Open Sans Light" FontSize="Medium">
    <Label.GestureRecognizers>
        <TapGestureRecognizer Tapped="OnLabelTapped" />
    </Label.GestureRecognizers>
</Label>

在你的代码后面

private void OnLabelTapped (object sender, EventArgs e)
{
    //Your code here
}

关于c# - 标签 TapGesture 不会触发 Xamarin Forms,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45376745/

相关文章:

c# - 有没有办法在 Windows 窗体的 LinkLabel 控件中放置多个链接

c# - 使用 mysql 在 C# 中插入可空 int

wpf - DataTemplate 中的 ComboBox ItemsSource 绑定(bind)

c# - 绑定(bind) Button.IsEnabled() 属性无法正常工作

c# - android设备上的套接字连接拒绝错误

xamarin - 如何在 Release模式下调试

c# - 缓存 'Priority' 和 'AbsoluteExpiration' 是如何协同工作的?

c# - 允许 CON,URL 中的保留关键字

xaml - 使 Xamarin Forms 默认编译所有 XAML

ios - Codesign 在 "replacing existing signature"后返回未知错误