Label TapGesture 不会触发 Xamarin Forms

问题描述 投票:0回答:1

我试图将

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>

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

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

c# xaml xamarin xamarin.forms
1个回答
2
投票

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

<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
}
© www.soinside.com 2019 - 2024. All rights reserved.