Quantcast
Channel: Xamarin.Forms — Xamarin Community Forums
Viewing all articles
Browse latest Browse all 77050

ListView with MVVM - TapGestureRecognizer not working

$
0
0

Hi,

I'm trying to go the MVVM route and avoid doing stuff directly in the code-behind, I'm finding that trying to detect a row being clicked to invoke a command painful to implement. It would seem that there's no ItemSelected that takes a Command (but it's okay if I wanted to invoke an action in my code-behind), which is a shame, so I've tried to use the TapGestureRecognizer with no success...

XAML...

        <ListView 
            CachingStrategy="RecycleElement"
            ItemsSource="{Binding FilteredMessages}" 
            HasUnevenRows = "true"
            SelectedItem = "{Binding SelectedMessage}">


            <ListView.ItemTemplate>
              <DataTemplate>
                <ViewCell>
                    <StackLayout Orientation="Vertical">
                        <StackLayout Orientation="Horizontal">
                            <Label Text="{Binding Scheduled}"/>
                        </StackLayout>
                        <StackLayout Orientation="Horizontal">
                            <Label Text="{Binding Message}"/>
                            <Label.GestureRecognizers>
                                <TapGestureRecognizer Command="{Binding TapCommand}"></TapGestureRecognizer>
                            </Label.GestureRecognizers>
                        </StackLayout>
                    </StackLayout>
                </ViewCell>
              </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>

The View Model...

    public ContactDetailViewModel()
    {
    ...
        TapCommand = new Command(HandleAction);
    }


    void HandleAction (object obj)
    {
        Console.WriteLine("***** DOESNT GET CALLED :-( *****");
    }

I've tried to put the GestureRecognizers at different layers in the xaml (i.e. at the StackLayout, ViewCell and ListView), but none invoke my callback method, but it would appear that it's capturing the event, because the cell doesn't go grey; which is the default behaviour when I click the cell.

Any ideas what I'm overlooking here?

I could fallback to using an ItemSelected in the code-behind and delegate a call through to the ViewModel, but that's not clean.

I appreciate any insights.

Thanks,
Rob.


Viewing all articles
Browse latest Browse all 77050

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>