Xaml 表单:如何设置';手势识别器&x27;时髦
这可能是一个愚蠢的问题,但我只是想知道,因为我是Xamarin.forms的新手 我们可以设置“手势识别器”的样式吗。例如,我想为标签创建一个样式,如下所示Xaml 表单:如何设置';手势识别器&x27;时髦,xaml,xamarin,xamarin.forms,xamarin.ios,Xaml,Xamarin,Xamarin.forms,Xamarin.ios,这可能是一个愚蠢的问题,但我只是想知道,因为我是Xamarin.forms的新手 我们可以设置“手势识别器”的样式吗。例如,我想为标签创建一个样式,如下所示 <Style TargetType="Label" x:Key="LabelStyle"> <Setter Property="GestureRecognizers"> <Setter.Value> <TapGestureRecognizer Com
<Style TargetType="Label" x:Key="LabelStyle">
<Setter Property="GestureRecognizers">
<Setter.Value>
<TapGestureRecognizer Command="{Binding EditSectionCommand}"/>
</Setter.Value>
</Setter>
</Style>
但它显示了一个编译时错误“无法解析标签上的手势识别器”
非常感谢您的帮助。您不能在样式中设置此
手势识别器
属性。因为手势识别器
在默认的标签中不是可绑定的属性
如果确实要使用样式配置TapGestureRecognitor
,可以尝试构造自定义标签。定义一个可绑定的命令属性,该属性处理标签的TapGestureRecognizer
事件:
public class CustomLabel : Label
{
public ICommand MyCommand
{
get
{
return (ICommand)GetValue(MyCommandProperty);
}
set
{
SetValue(MyCommandProperty, value);
}
}
public static readonly BindableProperty MyCommandProperty = BindableProperty.Create(nameof(MyCommand), typeof(ICommand), typeof(CustomLabel), null,
propertyChanged: (bindable, oldValue, newValue) =>
{
// Add a Tap gesture to this label, and its command is the bindableproperty we add above
var control = (CustomLabel)bindable;
control.GestureRecognizers.Clear();
TapGestureRecognizer tap = new TapGestureRecognizer();
tap.Command = newValue as ICommand;
control.GestureRecognizers.Add(tap);
});
}
最后,您可以在XAML中使用此命令:
<ContentPage.Resources>
<Style x:Key="LabelStyle" TargetType="local:CustomLabel">
<Setter Property="MyCommand" Value="{Binding TapCommand}"/>
</Style>
</ContentPage.Resources>
尝试创建一个从
标签继承的自定义控件,并将标签识别器添加到自定义控件中。