ListView with MVVM - TapGestureRecognizer command is not working

IamAmitSrivastavIamAmitSrivastav USMember ✭✭
edited March 2018 in Xamarin.Forms

I'm using MVVM, 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, so I've tried to use the TapGestureRecognizer with no success..
my viewcell is in c# as its bit complex to convert into xaml.

InSide ViewCell---

TapGestureRecognizer taponimgcheckuncheck = new TapGestureRecognizer();
taponimgcheckuncheck.SetBinding(TapGestureRecognizer.CommandProperty,"TapOnLogo");
imgcheckuncheck.GestureRecognizers.Add(taponimgcheckuncheck);

And my viewmodel is like---
public ICommand TapOnLogo{

        get{
            return new Command(() =>
            {
                Application.Current.MainPage.DisplayAlert("TapOnLogo","TapOnLogo","ok");
            });
        }
    }

Posts

  • ClintStLaurentClintStLaurent USUniversity ✭✭✭✭✭

    I'd work it from a different perspective.
    You don't really care about 'tapped'. You really only care about when the selected items changes. You don't care how it gets changed. Most times that will happen by being tapped. But it could also happen programmatically and most times you'd want the same logic to run.

    So if you call your command from within the property you have binded to SelectedItem on the ListView then you're all set.

    MyBlahBlahCommand.Execute(ParamterMostLikelyTheSelectedItem);

  • IamAmitSrivastavIamAmitSrivastav USMember ✭✭

    @ClintStLaurent
    I agree but I don't wanna to do like that Can you please do one thing as JamesMontemagno replied in a post .

    by james ----
    You can, but the problem here is that you are binding to the Command that is in your ViewModel, not on the actual it on the list. You can add the command to your Model or do a reference binding to the page:

    give a x:Name="PageName" in your content page and then do:


    <Label.GestureRecognizers>

    </Label.GestureRecognizers>

  • IamAmitSrivastavIamAmitSrivastav USMember ✭✭

    https://forums.xamarin.com/discussion/57539/listview-with-mvvm-tapgesturerecognizer-not-working
    here you can see i want same but to write in c# as he did in xaml

  • IamAmitSrivastavIamAmitSrivastav USMember ✭✭

    How to pass the page name in command using c#

  • AlessandroCaliaroAlessandroCaliaro ITMember ✭✭✭✭✭

    I use something like

                // Image TRASH
                TapGestureRecognizer tgrTrash = new TapGestureRecognizer();
                tgrTrash.SetBinding(TapGestureRecognizer.CommandProperty, new Binding("BindingContext.TrashCommand", source: this));
                tgrTrash.SetBinding(TapGestureRecognizer.CommandParameterProperty, ".");
    
                Image imageTrash = new Image() { Source = "trash.png" };
                //Label lTrash = new Label { Text = "Trash", VerticalTextAlignment = TextAlignment.Center };
                imageTrash.GestureRecognizers.Add(tgrTrash);
    

    https://github.com/acaliaro/TestBindingWithListView

  • IamAmitSrivastavIamAmitSrivastav USMember ✭✭

    @AlessandroCaliaro Hi,
    I did like same you suggest me but no success.
    I attached my viewcell, my XAML page, and ViewModel here so please do have a look once.

    Find that in viewCell
    TapGestureRecognizer taponimgcheckuncheck = new TapGestureRecognizer();

                        taponimgcheckuncheck.SetBinding(TapGestureRecognizer.CommandProperty,new Binding("BindingContext.TapOnLogo", source:"inboxpageinxaml"));
                        taponimgcheckuncheck.SetBinding(TapGestureRecognizer.CommandParameterProperty,".");
                        imgcheckuncheck.GestureRecognizers.Add(taponimgcheckuncheck);
    
  • AlessandroCaliaroAlessandroCaliaro ITMember ✭✭✭✭✭

    you should post a full repo so one can investigate and do a little debug...

  • DorababuDorababu USMember ✭✭
    edited June 2018

    Hi @IamAmitSrivastav please follow below format

    Reference path means the x:Name of contentpage

  • JontyJonty Member ✭✭

    Add the x:Name of your ContentPage like so:

    <ContentPage x:Name="PageName">

Sign In or Register to comment.