我试图将 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/