sinsedrix
sinsedrix

Reputation: 4795

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

I need a composite clickable MAUI component on which I can bind a command.

//

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));
}

This way, I hoped Command property to be bindable but:

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

How am I meant to implement this kind of component?

Upvotes: 4

Views: 2832

Answers (1)

ToolmakerSteve
ToolmakerSteve

Reputation: 21340

Like Xamarin Forms, Maui can attach a TapGestureRecognizer to any visual element. See also TapGestureRecognizer doc.

Add this to the ContentView in xaml:

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

Add this to the "code behind" in xaml.cs:

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

Upvotes: 3

Related Questions