Xamarin可重用的xaml用户控件和自定义命令

Xamarin可重用的xaml用户控件和自定义命令,xaml,xamarin,user-controls,icommand,Xaml,Xamarin,User Controls,Icommand,首先为我的英语感到抱歉。 我正在使用Xamarin.Form开发一个iOS和Android项目 我希望有一个“xaml用户控件”可以以不同的方式重用,并且我需要使用ICommand使其可单击 这是StackLayoutButton组件: <?xml version="1.0" encoding="utf-8" ?> <StackLayout xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schema

首先为我的英语感到抱歉。 我正在使用Xamarin.Form开发一个iOS和Android项目

我希望有一个“xaml用户控件”可以以不同的方式重用,并且我需要使用ICommand使其可单击

这是StackLayoutButton组件:

<?xml version="1.0" encoding="utf-8" ?>
 <StackLayout xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="Installa.Controls.StackLayoutButton">
  <Image x:Name="Icon" Source="{Binding Icon}" />
  <Label x:Name="Text" Text="{Binding Title}" HorizontalOptions="Center" LineBreakMode="NoWrap" Font="Small" TextColor="Red" />
</StackLayout>
这是viewmodel类:

namespace Installa
{
    public class CalendarioViewModel: BaseViewModel
    {

        public CalendarioViewModel()
        {
            blog = new Activity();
            blog.Link = "www.google.it";
            blog.Title = "Titolo del blog";
            blog.Icon = "logomenu.png";

            facebook = new Activity();
            facebook.Title = "Tito Fbook";
            facebook.Link = "www.facebook.it";
            facebook.Icon = "icon.png";

            ViewName = "nome della view";
            IsLoading = false;    
        }

        Activity blog = null;
        public Activity Blog
        {
            get {return blog;}
        }

        Activity facebook = null;
        public Activity Facebook
        {
            get { return facebook; }
        }

        string viewName = string.Empty;
        public string ViewName
        {
            get { return viewName; }
            set { SetProperty(ref viewName, value); }
        }

        public bool IsWindowsPhone
        {
            get
            {
                return Device.OS == TargetPlatform.WinPhone;
            }
        }

        bool isLoading = false;
        public bool IsLoading
        {
            get { return isLoading; }
            set { SetProperty(ref isLoading, value); }
        }           
    }
}
通过活动,一个简单的类,包括:

    public string Title { get; set; }

    public string Link { get; set; }

    public String Icon { get; set; }
到目前为止,一切正常,但现在我需要实现ICommand接口

在StackLayoutButton c#代码中,我尝试添加:

        var tapGestureRecognizer = new TapGestureRecognizer();
        tapGestureRecognizer.SetBinding(TapGestureRecognizer.CommandProperty, "TapCommand");
        Icon.GestureRecognizers.Add(tapGestureRecognizer)
        Text.GestureRecognizers.Add(tapGestureRecognizer)
此外,我尝试将INotifyPropertyChanged和“OnTapped”方法添加到CalendarioViewModel中

我在Activity.cs中添加了“ICommand tapCommand”和相关的get…但不起作用

我甚至尝试其他..但我无法启用StackLayoutButton组件上的点击

我该怎么办? 我希望能够有一个“可编程”命令…例如,我希望浏览活动的“链接属性”,或者能够打开一个新视图

谢谢你的帮助

更新:

我能够将TapGestureRecognitor添加到xaml用户控件(StackLayoutButton.xaml.cs)中, 但我想用MVVM的方式实现它

using Xamarin.Forms;
namespace Installa.Controls
{
    public partial class StackLayoutButton : StackLayout
    {
        public StackLayoutButton()
        {
            InitializeComponent();


            TapGestureRecognizer tapGestureRecognizer = new TapGestureRecognizer
            {
                Command = new Command(OnMyComponentTapped),
                CommandParameter = "ciao"
            };
            this.Icon.GestureRecognizers.Add(tapGestureRecognizer);
            this.Text.GestureRecognizers.Add(tapGestureRecognizer);
        }


        async void OnMyComponentTapped(object parameter)
        {
            // do action
        }


        public Color TextColor
        {
            get { return this.Text.TextColor; }
            set { this.Text.TextColor = value; }
        }
        public Label TextControl
        {
            get { return this.Text; }
            set { this.Text = value; }
        }
    }
}
有人能给我指路吗


谢谢

这是我如何在xaml视图中添加手势识别器的:

<Label.GestureRecognizers>
    <TapGestureRecognizer Command="{Binding SelectEquipmentCommand}"/>
</Label.GestureRecognizers>
using Xamarin.Forms;
namespace Installa.Controls
{
    public partial class StackLayoutButton : StackLayout
    {
        public StackLayoutButton()
        {
            InitializeComponent();


            TapGestureRecognizer tapGestureRecognizer = new TapGestureRecognizer
            {
                Command = new Command(OnMyComponentTapped),
                CommandParameter = "ciao"
            };
            this.Icon.GestureRecognizers.Add(tapGestureRecognizer);
            this.Text.GestureRecognizers.Add(tapGestureRecognizer);
        }


        async void OnMyComponentTapped(object parameter)
        {
            // do action
        }


        public Color TextColor
        {
            get { return this.Text.TextColor; }
            set { this.Text.TextColor = value; }
        }
        public Label TextControl
        {
            get { return this.Text; }
            set { this.Text = value; }
        }
    }
}
<Label.GestureRecognizers>
    <TapGestureRecognizer Command="{Binding SelectEquipmentCommand}"/>
</Label.GestureRecognizers>
    public ICommand SelectEquipmentCommand
    {
        get
        {
            return fSelectEquipmentCommand ?? (fSelectEquipmentCommand = new Command(async () => await SelectEquipmentTask()));
        }
    }

    private async Task SelectEquipmentTask()
    {

    }