0
votes

I am new with xamarin, I am facing an issue in my xamarin forms project. I have a Label and 2 images inside listview-viewcell. I want to change the label text onclick of images. Increase the label text value by 1 if click like and decrease by 1 if click unlike. I use the label x:name, but it is not accessible in class. How can i fix this?

    <StackLayout>
        <ListView>
            <ListView.ItemTemplate>
                <DataTemplate>
                    <ViewCell>
                    <StackLayout>
                        <Label 
                            Text="{Binding likeCount}"
                            x:Name="likecount"/> 
                         <Image Source="like.png">
                               <Image.GestureRecognizers>
                                    <TapGestureRecognizer
                                        Tapped="LikeOrUnlikeTweet"
                                        CommandParameter="{Binding .}"
                                        NumberOfTapsRequired="1" /> 
                                </Image.GestureRecognizers>
                            </Image
                            <Image Source="unlike.png">
                               <Image.GestureRecognizers>
                                    <TapGestureRecognizer
                                        Tapped="LikeOrUnlikeTweet"
                                        CommandParameter="{Binding .}"
                                        NumberOfTapsRequired="1" /> 
                                </Image.GestureRecognizers>
                            </Image
                        </StackLayout>
                    </ViewCell>
                </DataTemplate>
            </ListView.ItemTemplate>
          </ListView>           
    </StackLayout>

Anybody please suggest a solution with working code. Thanks in advance

2
How are you binding your list? If you are binding the Text of your label to a viewmodel property (likeCount) correctly, then updating likeCount should update the text for the Label. Please post some more code.David Conlisk

2 Answers

0
votes

First of you need to create command in your VM and need to change as follow:

 <TapGestureRecognizer
      Tapped="{Binding Source={x:Reference YourListName}, Path=BindingContext.LikeOrUnlikeTweetCommand}"
      CommandParameter="{Binding .}"
      NumberOfTapsRequired="1" />

ViewModel Code

public ICommand LikeOrUnlikeTweetCommand { get; }

Initialise in constructor

LikeOrUnlikeTweetCommand = new Command<YourModelDto>(LikeOrUnlikeTweet);

Function code in this you change your count value and update list.

void LikeOrUnlikeTweet(YourModelDto yourModelDto)
{
        ....
}
0
votes

I would take one of 2 paths for doing this:

  1. Use an ObservableCollection as ItemsSource for your list, obvious when you change properties of an item they would be reflected in the list. What the list view would do is it would replace the old item with the new one modified with your new values. You can make a search about what an ObservableCollection collection is.

  2. My actual way, as im fighting low performance of android with listviews, and this makes it: you can create a custom class for ViewCell, avoid low-speed bindings, set all properties inside your custom ViewCell by code. When the cell bindigcontext changes more likely the ItemsSource item is being assigned to your cell, so you can now take its value ans set it up.

    protected override void OnBindingContextChanged()
    {
        SetupCell(); //assign values to named controls manually
        base.OnBindingContextChanged();
    }
    
        public void SetupCell()
        {
            var item = BindingContext as YouItemClass;
            if (item == null) return;
    
            txtTitle.Text = item.Name;
    

    //etc ...