Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/xamarin/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Xaml 表单:如何设置';手势识别器&x27;时髦_Xaml_Xamarin_Xamarin.forms_Xamarin.ios - Fatal编程技术网

Xaml 表单:如何设置';手势识别器&x27;时髦

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

这可能是一个愚蠢的问题,但我只是想知道,因为我是Xamarin.forms的新手

我们可以设置“手势识别器”的样式吗。例如,我想为标签创建一个样式,如下所示

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

尝试创建一个从
标签继承的自定义控件,并将
标签识别器添加到自定义控件中。