Mvvm Forms模板命令Tap手势内部标签

Mvvm Forms模板命令Tap手势内部标签,mvvm,xamarin.forms,Mvvm,Xamarin.forms,伙计们 我需要关于Xamarin.Forms的帮助 我有一个自定义模板,这个模板包含两个标签。我只需要一个标签就可以得到一个tapperstate命令。 在我的代码中,我的整个模板都接收到tappostate命令 有人能帮我吗 代码ScheduleHeaderTitleTemplate:MyCustomTemplate public partial class ScheduleHeaderTitleTemplate : ContentView { #region Title pu

伙计们

我需要关于Xamarin.Forms的帮助

我有一个自定义模板,这个模板包含两个标签。我只需要一个标签就可以得到一个tapperstate命令。 在我的代码中,我的整个模板都接收到tappostate命令

有人能帮我吗

代码ScheduleHeaderTitleTemplate:MyCustomTemplate

public partial class ScheduleHeaderTitleTemplate : ContentView
{
    #region Title
    public static readonly BindableProperty TitleProperty = BindableProperty.Create(
        propertyName: "Title",
        returnType: typeof(string),
        declaringType: typeof(ScheduleHeaderTitleTemplate),
        defaultValue: string.Empty,
        defaultBindingMode: BindingMode.TwoWay,
        propertyChanged: TitlePropertyChanged
    );
    public string Title
    {
        get { return (string)GetValue(TitleProperty); }
        set { SetValue(TitleProperty, value); }
    }
    private static void TitlePropertyChanged(BindableObject bindable, Object oldValue, Object newValue)
    {
        var scheduleHeaderTitleTemplate = (ScheduleHeaderTitleTemplate)bindable;
        scheduleHeaderTitleTemplate.title.Text = (string)newValue;
    }
    #endregion

    #region Command
    public static readonly BindableProperty CommandProperty = BindableProperty.Create(
        propertyName: nameof(Command),
        returnType: typeof(ICommand),
        declaringType: typeof(Label),
        defaultValue: null
    );
    public ICommand Command
    {
        get { return (ICommand)GetValue(CommandProperty); }
        set { SetValue(CommandProperty, value); }
    }
    #endregion

    public ScheduleHeaderTitleTemplate()
    {
        InitializeComponent();

        var tapGestureRecognizer = new TapGestureRecognizer();
        tapGestureRecognizer.Tapped += (sender, e) =>
        {
            if (Command != null && Command.CanExecute(null))
                Command.Execute(null);
        };

        GestureRecognizers.Add(tapGestureRecognizer);
    }
}
<ContentView.Content>
    <StackLayout Padding="10, 10" Spacing="0" Orientation="Horizontal" BackgroundColor="{StaticResource Primary}">
        <StackLayout Orientation="Horizontal" VerticalOptions="Center">
            <!--TAPGESTURE FOR THIS LABEL -->
            <Label Text="&#xf060;" Margin="0, 0, 15, 0" Style="{StaticResource IconWithFontAwesome}" />

            <Label x:Name="title" Margin="0, 3, 0, 0" Style="{StaticResource ScheduleSubTitle}"/>
        </StackLayout>
    </StackLayout>
</ContentView.Content>
查看ScheduleHeaderTitleTemplate:MyCustomTemplate

public partial class ScheduleHeaderTitleTemplate : ContentView
{
    #region Title
    public static readonly BindableProperty TitleProperty = BindableProperty.Create(
        propertyName: "Title",
        returnType: typeof(string),
        declaringType: typeof(ScheduleHeaderTitleTemplate),
        defaultValue: string.Empty,
        defaultBindingMode: BindingMode.TwoWay,
        propertyChanged: TitlePropertyChanged
    );
    public string Title
    {
        get { return (string)GetValue(TitleProperty); }
        set { SetValue(TitleProperty, value); }
    }
    private static void TitlePropertyChanged(BindableObject bindable, Object oldValue, Object newValue)
    {
        var scheduleHeaderTitleTemplate = (ScheduleHeaderTitleTemplate)bindable;
        scheduleHeaderTitleTemplate.title.Text = (string)newValue;
    }
    #endregion

    #region Command
    public static readonly BindableProperty CommandProperty = BindableProperty.Create(
        propertyName: nameof(Command),
        returnType: typeof(ICommand),
        declaringType: typeof(Label),
        defaultValue: null
    );
    public ICommand Command
    {
        get { return (ICommand)GetValue(CommandProperty); }
        set { SetValue(CommandProperty, value); }
    }
    #endregion

    public ScheduleHeaderTitleTemplate()
    {
        InitializeComponent();

        var tapGestureRecognizer = new TapGestureRecognizer();
        tapGestureRecognizer.Tapped += (sender, e) =>
        {
            if (Command != null && Command.CanExecute(null))
                Command.Execute(null);
        };

        GestureRecognizers.Add(tapGestureRecognizer);
    }
}
<ContentView.Content>
    <StackLayout Padding="10, 10" Spacing="0" Orientation="Horizontal" BackgroundColor="{StaticResource Primary}">
        <StackLayout Orientation="Horizontal" VerticalOptions="Center">
            <!--TAPGESTURE FOR THIS LABEL -->
            <Label Text="&#xf060;" Margin="0, 0, 15, 0" Style="{StaticResource IconWithFontAwesome}" />

            <Label x:Name="title" Margin="0, 3, 0, 0" Style="{StaticResource ScheduleSubTitle}"/>
        </StackLayout>
    </StackLayout>
</ContentView.Content>

这很简单。不要在构造函数的右侧添加TapGestureRecognitor,而是将其添加到要添加的标签中。有两种方法可以做到这一点:

  • 将x:Name属性添加到标签中,例如“lblLink”,然后在构造函数中向其添加手势,如
    lblLink.gestureRecograiners.add(yourgestureRecograiner)

  • 将命令绑定到XAML中的标签。您将看到完全相同的东西,但在XAML中。基本上,您必须将x:Name属性添加到ContentView,并将TapGestureRecognitor添加到标签:

  • 
    
    希望有帮助