如何在 MAUI 中拥有命令(可用)视图或带有内容的按钮?

How to have a Command(able) view or a Button with content in MAUI?

我需要一个可以绑定命令的复合可点击 MAUI 组件。

//

public partial class BtnView : BaseContentView<BtnVm>
{
    public ICommand Command { get; set; }

    public object CommandParameter { get; set; }

    public BtnView() : base()
    {
        InitializeComponent();
    }

    public static BindableProperty CommandProperty { get; set; } = BindableProperty.Create(nameof(Command), typeof(Command), typeof(BtnView));
    public static BindableProperty CommandParameterProperty { get; set; } = BindableProperty.Create(nameof(CommandParameter), typeof(object), typeof(BtnView));
}

这样,我希望命令 属性 可以绑定但是:

No property, BindableProperty, or event found for "Command", or mismatching type between value and property.

我打算如何实现这种组件?

与 Xamarin Forms 一样,Maui 可以attach a TapGestureRecognizer to any visual element. See also TapGestureRecognizer doc

将此添加到 xaml 中的 ContentView

<ContentView.GestureRecognizers>
    <TapGestureRecognizer
        NumberOfTapsRequired="1"
        Tapped="OnTapped">
    </TapGestureRecognizer>
</ContentView.GestureRecognizers>

将此添加到 xaml.cs 中的“隐藏代码”中:

protected void OnTapped(object sender, EventArgs args)
{
    // Do something here.
}