Timothy Belvin
Timothy Belvin

Reputation: 83

Tap gesture with Command Parameters

I have a listview with an ItemTemplate defined as follows:

<ListView.ItemTemplate>
                <DataTemplate>
                    <ViewCell>
                        <StackLayout>
                            <Grid>
                                <Grid.RowDefinitions>
                                    <RowDefinition Height="Auto"/>
                                </Grid.RowDefinitions>
                                <Grid.ColumnDefinitions>
                                    <ColumnDefinition Width="0.7*" />
                                    <ColumnDefinition Width="0.3*" />
                                </Grid.ColumnDefinitions>
                                <Label Text="{Binding AddressTypeName}" Grid.Column="0"/>
                                <Label Text="&#xE74D;"
                                       Grid.Column="1"
                                       FontFamily="{StaticResource SegoeMDL2Assets}" FontSize="Medium" HorizontalOptions="End">
                                    <Label.GestureRecognizers>
                                        <TapGestureRecognizer Tapped="OnDelete_Tapped" CommandParameter="{Binding .}"/>
                                    </Label.GestureRecognizers>
                                </Label>

                            </Grid>
                        </StackLayout>
                    </ViewCell>
                </DataTemplate>

            </ListView.ItemTemplate>

My code behind file handles the OnDelete_Tapped as follows:

      public void OnDelete_Tapped(object sender, EventArgs e)
    {
        viewModel.DeleteCommand.Execute(e);

        viewModel.LoadAddressTypesCommand.Execute(true);
    }

The EventArgs e indeed returns a EventArgs object that has the correct object in it (in my case, the correct AddressType object).

The ViewModel, has this defined:

       public ICommand DeleteCommand => new Command<AddressType>(DeleteCommandExecute);

    void DeleteCommandExecute(AddressType address)
    {
        if (IsBusy)
            return;
        IsBusy = true;

        try
        {
            DataStore.DeleteAddressTypeAsync(address.Id);
        }
        catch (Exception ex)
        {
            Debug.WriteLine(ex);
        }
        finally
        {
            IsBusy = false;
        }
    }

The DeleteCommand NEVER executes. I can step through the code, but it never calls the DeleteCommand in my viewModel. The LoadAddressTypesCommand runs properly - not only from the code behind but elsewhere I have that command bound as a Command to my viewModel. Any ideas what I am doing wrong? Thanks in advance.

Upvotes: 0

Views: 1618

Answers (1)

Leo Zhu
Leo Zhu

Reputation: 14956

you could try this:

in xaml:

<TapGestureRecognizer Tapped="OnDelete_Tapped" CommandParameter="{Binding .}"/>

then e.Parameter will be whatever you set in the CommandParameter.

public void OnDelete_Tapped(object sender, TappedEventArgs e)
{
    var addressType = (e.Parameter) as AddressType;
    viewModel.DeleteCommand.Execute(addressType );
    viewModel.LoadAddressTypesCommand.Execute(true);
}

or directly Using ICommand

<TapGestureRecognizer
        Command="{Binding DeleteCommand}"
        CommandParameter="{Binding .}" />

Upvotes: 1

Related Questions