tap gesture recognizer tapped ItemTappedEventArgs not working

ABloxityABloxity Member ✭✭✭

I have this error that the object is not referenced

And my TapGestureRecognizer is inside a Stacklayout and is inside a listview
in my XAML.

help me please.!!!

Best Answer

Answers

  • ABloxityABloxity Member ✭✭✭

    Yes, it brings in NULL the syntax:

    var itemPublicacion = e.Item as Publicaciones;

  • AdamMeaneyAdamMeaney USMember ✭✭✭✭✭

    I don't know that your signature on this method is correct.

    Tapped doesn't use ItemTappedEvent args.

    I would suggest a Command with a CommandParameter might better suit your needs, depending on what exactly you are looking for from this object.

  • ABloxityABloxity Member ✭✭✭

    I want to see the ID of the item that I select through the tap gesture recognizer Tapped on the Display

  • ABloxityABloxity Member ✭✭✭

    @AdamMeaney
    And how can it be done?

    will have an example

  • igorkr_10igorkr_10 Member ✭✭✭✭

    Why do you need TapGestureReccognizer? Maybe ItemTapped event of ListView is enough for you?

  • ABloxityABloxity Member ✭✭✭

    @igorkr_10
    I need it because I am doing the Like function (and register that like with the publication id, user id and date) & dislike in the TapGestureRecognizer stacklayout within my listview.

  • ABloxityABloxity Member ✭✭✭

    @igorkr_10
    And this event that you say ListTapped ItemTapped, if it works brings me the id, but only when I press it in the whole list, not in the stacklayout

  • ABloxityABloxity Member ✭✭✭

    ListView ItemTapped*

  • igorkr_10igorkr_10 Member ✭✭✭✭
    edited October 2019

    @ABloxity said:
    @igorkr_10
    I need it because I am doing the Like function (and register that like with the publication id, user id and date) & dislike in the TapGestureRecognizer stacklayout within my listview.

    Try Command.
    <TapGestureRecognizer Command={Binding LikeCommand} />
    This command will be executed directly in your listItem
    Don't forget to create Command in your listItem:

    public Command LikeCommand { get; }
    public ListItem()
    {
        this.LikeCommand=new Command(async ()=>this.ExecuteLike());
    }
    public async Task ExecuteLike()
    {
        ...
    }
    
  • igorkr_10igorkr_10 Member ✭✭✭✭
    edited October 2019

    I already posted Liker control for you in another thread. Just use it.
    Add Command to that control:

    public static readonly BindableProperty CommandProperty = BindableProperty.Create(nameof(Command), typeof(Command), typeof(Liker), default(Command), BindingMode.TwoWay);
    
            public Command Command
            {
                get { return (Command)GetValue(StatusProperty); }
                set { SetValue(StatusProperty, value); }
            }
    

    In the end of OnStatusChanged method add code:

    if (liker.Command != null)
    {
        liker.Command.Execute(null);
    }
    

    In your listItem add command that I described below. You will know about Like status by binding

    Finally, your xaml must be:

    <ViewCell>
        <StackLayout>
        ...
            <Liker Status={Binding LikeStatus} Command={Binding LikeCommand}/>
        ...
        </StackLayout>
    </ViewCell>
    
  • ABloxityABloxity Member ✭✭✭

    If i used ItemTapped then i get the selected item in my xaml.cs file not in ViewModel. I want to read it in viewmodel

  • igorkr_10igorkr_10 Member ✭✭✭✭

    @ABloxity said:
    I want to read it in viewmodel

    Yes. You can do this by commands. Whats problem?

  • AdamMeaneyAdamMeaney USMember ✭✭✭✭✭

    Or you can bind the SelectedItem of the List to your ViewModel property for the Selected thing, and do something on property set.

  • ABloxityABloxity Member ✭✭✭

    @AdamMeaney said:

    this my functions.

    ItemSelected="{Binding ListaPublicacionesPerfil}"


    <Button.Triggers>








    </Button.Triggers>

                                                </Button>
    

    public ICommand LikePublicacion
    {
    get
    {
    try
    {
    return new Command(async (ite) =>
    {
    var idpublicacion = (ite as Publicaciones);
    PublicacionesServices publicacionesServices = new PublicacionesServices();

                        if(IsLiked =! IsLiked)
                        {
                            await publicacionesServices.SalvaMeGusta(idpublicacion.idpublicacion, Settings.IdUsuario, fecha);
                            await Application.Current.MainPage.DisplayAlert(
                                             "Bien",
                                             "Le has dado Me Gusta",
                                             "Aceptar");
                        }
                        else
                        {
                            await Application.Current.MainPage.DisplayAlert(
                                             "Error",
                                             "Se elimino el MG",
                                             "Aceptar");
                        }
                    });
                }
                catch (Exception ex)
                {
                    throw ex;
                }
            }
        }
    
  • AdamMeaneyAdamMeaney USMember ✭✭✭✭✭

    ItemSelected="{Binding ListaPublicacionesPerfil}"

    Should be more

    SelectedItem="{Binding ListaPublicacionesPerfil}"

    and your VM

    private Publicaciones _ListaPublicacionesPerfil;
    public Publicaciones ListaPublicacionesPerfil
    {
        get => _ListaPublicacionesPerfil;
        set 
        {
            _ListaPublicacionesPerfil = value;
            OnPropertyChanged();
    
            DoCommandMethod();
        }
    }
    

    As an fyi, your Command should not be newed up in the getter, you should declare it and then assign it so you only have one instance, not as many as you have references to the Command.

  • ABloxityABloxity Member ✭✭✭

    @AdamMeaney said:

    Only when I click on one, all the buttons are changed

  • ABloxityABloxity Member ✭✭✭

    this error comes out when binating item Selected

  • AdamMeaneyAdamMeaney USMember ✭✭✭✭✭
    <ListView
        ItemsSource="{Binding ListaPublicacionesPerfil}" >
        <ListView.ItemTemplate>
            <DataTemplate>
                <ViewCell>
                    <StackLayout>
                        // Some Views on the cell
                        <Button Command="{Binding LikePublicacion" />
                    </StackLayout>
                </ViewCell>
            </DataTemplate>
        </ListView.ItemTemplate>
    </ListView>
    

    This says the LikePublicacion command is on your Data object, not your ViewModel. If so, you don't need a CommandParameter, as you are the object you are looking to mark liked already.

    If that is the case, stop passing in a command parameter of what looks to be the BindingContext of your cell, and just use that.

    Basically, I think you need to dump the Publicaciones class in here and whatever object that it is a list of some I can more easily determine how your class structure works, to tell you where the mistake is being made.

    Use three ` to mark your code blocks, like

    '''
    code here
    '''

  • ABloxityABloxity Member ✭✭✭

    @AdamMeaney said:
    you don't see my friend

  • igorkr_10igorkr_10 Member ✭✭✭✭

    And what? Any results?

  • ABloxityABloxity Member ✭✭✭

    @igorkr_10

    not yet, the same result still appears I click on an item button, and I paint the other buttons on the other items

  • AdamMeaneyAdamMeaney USMember ✭✭✭✭✭

    @AdamMeaney said:

    Basically, I think you need to dump the Publicaciones class in here and whatever object that it is a list of some I can more easily determine how your class structure works, to tell you where the mistake is being made.

  • ABloxityABloxity Member ✭✭✭

    These are my values ​​to change the styles in the trigger button

    in my viewmodel

    and again view model
    and in the button command LikePublicacion

    I do the conditions where I click the button brings me the id item of the publication

    and the other if the trigger is the same or different from the Boolean value, paint me only the one that is clicked, but pass it on and all the other also

  • AdamMeaneyAdamMeaney USMember ✭✭✭✭✭

    Right, so the GUESS here because you won't post a class, just unrelated snippets of code, is that your Publicaciones class is a list of Publicacion objects?

    Where your Publicacion is not the thing with this like, but your Page's ViewModel is the object being like toggled on, that all of these are bound to, not in a nice list.

    Your like Command is on some object, which is either the VM or the Publicacion object, and toggles the like status of the idPublicacion, which is something you haven't shown anywhere and no one knows what it is is, which seems to be a Publicacions object, which is your list source, so probably a whole list.

    For instance, if IsLiked is on your VM, and your buttons are in a list, you are Binding every Button to the Same IsLiked property, explaining why they don't work.

    Your code snippets are not full enough to confirm that, as there is no idea of which class things are in, what those classes look like, or the BindingContext of the list or this Button that you keep posting.

    YOU NEED MORE CODE, organized in files, to help conclusively. Currently we are guessing.

    LIKELY DIAGNOSIS:
    Your IsLiked is on your ViewModel, and every Button is bound to the same instance of your ViewModel, rather than their own Models.

  • ABloxityABloxity Member ✭✭✭

    @AdamMeaney said:
    Right, so the GUESS here because you won't post a class, just unrelated snippets of code, is that your Publicaciones class is a list of Publicacion objects?

    Where your Publicacion is not the thing with this like, but your Page's ViewModel is the object being like toggled on, that all of these are bound to, not in a nice list.

    Your like Command is on some object, which is either the VM or the Publicacion object, and toggles the like status of the idPublicacion, which is something you haven't shown anywhere and no one knows what it is is, which seems to be a Publicacions object, which is your list source, so probably a whole list.

    For instance, if IsLiked is on your VM, and your buttons are in a list, you are Binding every Button to the Same IsLiked property, explaining why they don't work.

    Your code snippets are not full enough to confirm that, as there is no idea of which class things are in, what those classes look like, or the BindingContext of the list or this Button that you keep posting.

    YOU NEED MORE CODE, organized in files, to help conclusively. Currently we are guessing.

    LIKELY DIAGNOSIS:
    Your IsLiked is on your ViewModel, and every Button is bound to the same instance of your ViewModel, rather than their own Models.

    Yes, I have my IsLike in my viewmodel along with the ICommand LikePublicacion
    and every Button is bound to the same instance of your ViewModel, I do not use my model, only in the ICommand LikePublicacion, to bring the id of the publication

  • ABloxityABloxity Member ✭✭✭

    @AdamMeaney said:
    Right, so the GUESS here because you won't post a class, just unrelated snippets of code, is that your Publicaciones class is a list of Publicacion objects?

    Where your Publicacion is not the thing with this like, but your Page's ViewModel is the object being like toggled on, that all of these are bound to, not in a nice list.

    Your like Command is on some object, which is either the VM or the Publicacion object, and toggles the like status of the idPublicacion, which is something you haven't shown anywhere and no one knows what it is is, which seems to be a Publicacions object, which is your list source, so probably a whole list.

    For instance, if IsLiked is on your VM, and your buttons are in a list, you are Binding every Button to the Same IsLiked property, explaining why they don't work.

    Your code snippets are not full enough to confirm that, as there is no idea of which class things are in, what those classes look like, or the BindingContext of the list or this Button that you keep posting.

    YOU NEED MORE CODE, organized in files, to help conclusively. Currently we are guessing.

    LIKELY DIAGNOSIS:
    Your IsLiked is on your ViewModel, and every Button is bound to the same instance of your ViewModel, rather than their own Models.

    Only what I need is that the button trigger does not paint me or change the style of the other buttons in those of more publications

  • AdamMeaneyAdamMeaney USMember ✭✭✭✭✭

    @ABloxity said:
    Yes, I have my IsLike in my viewmodel along with the ICommand LikePublicacion
    and every Button is bound to the same instance of your ViewModel, I do not use my model, only in the ICommand LikePublicacion, to bring the id of the publication

    So as I said, if everyone binds to the same object, they will all update when you change that object.

    You need to have the models for each of those buttons have an IsLiked, and toggle that instead of one like for the whole list.

    Only what I need is that the button trigger does not paint me or change the style of the other buttons in those of more publications

    If that was all you needed, we wouldn't be having this discussion. We are currently debating the level of MVVM knowledge you have, as that is why your things aren't working.

    That's why we wanted to see more code, as you are doing MVVM for this list a bit weird.

  • ABloxityABloxity Member ✭✭✭

    @AdamMeaney said:

    @ABloxity said:
    Yes, I have my IsLike in my viewmodel along with the ICommand LikePublicacion
    and every Button is bound to the same instance of your ViewModel, I do not use my model, only in the ICommand LikePublicacion, to bring the id of the publication

    So as I said, if everyone binds to the same object, they will all update when you change that object.

    You need to have the models for each of those buttons have an IsLiked, and toggle that instead of one like for the whole list.

    Only what I need is that the button trigger does not paint me or change the style of the other buttons in those of more publications

    If that was all you needed, we wouldn't be having this discussion. We are currently debating the level of MVVM knowledge you have, as that is why your things aren't working.

    That's why we wanted to see more code, as you are doing MVVM for this list a bit weird.

  • ABloxityABloxity Member ✭✭✭

    @AdamMeaney said:

    @ABloxity said:
    Yes, I have my IsLike in my viewmodel along with the ICommand LikePublicacion
    and every Button is bound to the same instance of your ViewModel, I do not use my model, only in the ICommand LikePublicacion, to bring the id of the publication

    So as I said, if everyone binds to the same object, they will all update when you change that object.

    You need to have the models for each of those buttons have an IsLiked, and toggle that instead of one like for the whole list.

    Only what I need is that the button trigger does not paint me or change the style of the other buttons in those of more publications

    If that was all you needed, we wouldn't be having this discussion. We are currently debating the level of MVVM knowledge you have, as that is why your things aren't working.

    That's why we wanted to see more code, as you are doing MVVM for this list a bit weird.

    I can not paste the entire code, some do not look, I better put it in a zip

  • AdamMeaneyAdamMeaney USMember ✭✭✭✭✭

    Well my summary of reading that code is you need to read a good few MVVM tutorials.

    You seem to be mixing the responsibilities of a few things together, and it is causing you some issues.

    A good rule of thumb is that this ViewModel shouldn't have Forms in it at all.

    But you have ListaPublicacionesPerfil, which is a List of Publicaciones. Everything in your ItemTemplate, aside from a few commands potentially, should be bound to a Publicaciones object then.

    You want the ListaPublicacionesPerfil to be an ObservableCollection of an object that has OnPropertyChanged for its properties if you are gonna change them, has the IsLiked on it so it is one like per Publicaciones object, and then if possible I like to have the command for do the liking on that object, but am ok with you Binding those ones back to your ViewModel.

    When you like a post, you should toggle that Publicaciones IsLiked, after doing whatever server side code you need to run.

  • ABloxityABloxity Member ✭✭✭

    and how can I link it to the publications object.

    If I like each one I will have the id of the publication, only that button will be activated and not the others. But as he says, he's throwing it away and activates all

  • AdamMeaneyAdamMeaney USMember ✭✭✭✭✭

    The Publications objects are the source of the data for your List.

    On pushing the button in them, if you pass the Button's BindingContext as a CommandParameter to the ViewModel Command for liking, you have the object you are. Either that or pass the ID and look up the item from your list on the ViewModel.

    Then change that Publications IsLiked, and only that one will change, rather than the VM IsLiked.

Sign In or Register to comment.