How to both execute Command and Button click in Xamarin


I have:

  <TapGestureRecognizer NumberOfTapsRequired"1" Command"{Binding Source{RelativeSource AncestorType{x:Type locals:OneViewModel}}, 
PathOneTappedView}" CommandParameter"{Binding .}" />

<Button x:Name"bt_one" Clicked"bt_one_Clicked"/>

When I do Grid Tap, Command and bt_one_Clicked execute concurrently? Thank you


When I do Grid Tap, Command and bt_one_Clicked execute concurrently?

Yes, you can add the button’s clicked code in your grid tap event when tapping your grid.

You can refer to the following code:


<?xml version"1.0" encoding"utf-8" ?>
<ContentPage xmlns""
             xmlns:x"" xmlns:formapp1"clr-namespace:FormApp1"

            <Grid WidthRequest"300" HeightRequest"600" BackgroundColor"Yellow">

                    <TapGestureRecognizer NumberOfTapsRequired"1"  Command"{Binding OneTappedViewCommand}"  />

            <Button x:Name"bt_one" Text"one button" Command"{Binding BtnOneClickedCommand}"/>


 public class OneViewModel: INotifyPropertyChanged
    public ICommand OneTappedViewCommand { private set; get; }

    public ICommand BtnOneClickedCommand { private set; get; }

    public OneViewModel() {

        OneTappedViewCommand  new Command(GridTapped);
        BtnOneClickedCommand new Command(btnOneClickedMthod);

    private void GridTapped()
        System.Diagnostics.Debug.WriteLine("----111------> GridTapped is triggered......");

        //add one clicked method here 

    private void btnOneClickedMthod()
        System.Diagnostics.Debug.WriteLine("----222------> GridTapped is triggered......");

    bool SetProperty<T>(ref T storage, T value, [CallerMemberName] string propertyName  null)
        if (Object.Equals(storage, value))
            return false;

        storage  value;
        return true;

    protected void OnPropertyChanged([CallerMemberName] string propertyName  null)
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));

    public event PropertyChangedEventHandler PropertyChanged;


In model OneViewModel, you can add bt_one event(btnOneClickedMthod) in grid tap function GridTapped.

Answered By – Jessie Zhang -MSFT

Leave a Comment