简体   繁体   English

Xamarin.Forms:如何设置样式中的“ GestureRecognizers”

[英]Xamarin.Forms : How to set 'GestureRecognizers' in style

This might be a stupid question but I just wanted to know as I am new to Xamarin.forms. 这可能是一个愚蠢的问题,但是我只是想知道,因为我是Xamarin.forms的新手。

Can we set 'GestureRecognizers' in Style. 我们可以在样式中设置“ GestureRecognizers”吗? For example I want to create a style for lable like below... 例如,我想为标签创建样式,如下所示...

<Style TargetType="Label" x:Key="LabelStyle">

    <Setter Property="GestureRecognizers">
        <Setter.Value>
            <TapGestureRecognizer Command="{Binding EditSectionCommand}"/>
        </Setter.Value>
    </Setter>
</Style>

but it shows an compile time error 'Can't resolve GestureRecognizers on Label'. 但显示编译时错误“无法解析Label上的GestureRecognizers”。

Any help is appreciated. 任何帮助表示赞赏。

You can't set this GestureRecognizers property in the style. 您不能在样式中设置此GestureRecognizers属性。 Because GestureRecognizers is not a bindable property in the default Label . 因为GestureRecognizers在默认Label中不是可绑定的属性。

If you do want to use Style to configure the TapGestureRecognizer , you can try to construct a customized label. 如果确实要使用样式来配置TapGestureRecognizer ,则可以尝试构造自定义标签。 Define a bindable command property which handles the TapGestureRecognizer event of the label: 定义一个可绑定的命令属性,该属性处理标签的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);
                                                                });
}

At last you can use this command in the XAML: 最后,您可以在XAML中使用以下命令:

<ContentPage.Resources>
    <Style x:Key="LabelStyle" TargetType="local:CustomLabel">
        <Setter Property="MyCommand" Value="{Binding TapCommand}"/>
    </Style>
</ContentPage.Resources>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM