my listview cell dose not load small icon sometimes.

ImKhanImKhan Member ✭✭
edited October 21 in Xamarin.Forms

Dear All
i have a listview in each cell there is an icon, but when i open list some cells show icon and some does not show. this icon has binding on tab it should show same size another icon. (by default it should show Active icon and when tab on that it will be hidden and incative icon should appear) below is xamal code for listview.

 <ListView HorizontalOptions="FillAndExpand" CachingStrategy="RecycleElement" SelectionMode="None"  HasUnevenRows="True" 
BackgroundColor="Transparent" ItemsSource="{Binding Data}" VerticalOptions="FillAndExpand">
                <ListView.ItemTemplate>
                    <DataTemplate>
                        <ViewCell>
                            <Grid Margin="0,5,0,5" BackgroundColor="Transparent">
                                <Grid.ColumnDefinitions>
                                    <ColumnDefinition Width="8*" />
                                    <ColumnDefinition Width="45*" />
                                    <ColumnDefinition Width="30*" />
                                    <ColumnDefinition Width="20*" />
                                </Grid.ColumnDefinitions>
                                <Label VerticalOptions="Center" FontAttributes="Bold" Grid.Column="0" Text="{Binding SerialNumber}" BackgroundColor="Transparent"/>
                                <Label VerticalOptions="Center" FontAttributes="Bold" Grid.Column="1" Text="{Binding Name}" BackgroundColor="Transparent"/>

                                <Frame Grid.Column="2" BackgroundColor="Black" CornerRadius="5" Padding="0"  HeightRequest="36" IsClippedToBounds="true"  >
                                    <Button BackgroundColor="Black" HorizontalOptions="CenterAndExpand" Command="{Binding ProfileCommand}" VerticalOptions="StartAndExpand" TextColor="Yellow" Text="Profile"/>
                                </Frame>

                                <Frame Grid.Column="3" BackgroundColor="Transparent" CornerRadius="5" Padding="0"  HeightRequest="25" IsClippedToBounds="true" IsVisible="{Binding ShowActiveImage}" >
                                    <Image BackgroundColor="Transparent" HorizontalOptions="CenterAndExpand" VerticalOptions="CenterAndExpand" IsVisible="{Binding ShowActiveImage}" Source="{u:ImageResource CricApp.Images.active.png}" Aspect="AspectFit">
                                        <Image.GestureRecognizers>
                                            <TapGestureRecognizer />
                                            <TapGestureRecognizer Command="{Binding PlayerActiveCommand}" />
                                        </Image.GestureRecognizers>
                                    </Image>
                                </Frame>

                                <Frame Grid.Column="3" BackgroundColor="Transparent" CornerRadius="5" Padding="0"  HeightRequest="25" IsClippedToBounds="true" IsVisible="{Binding HideActiveImage}" >
                                    <Image BackgroundColor="Transparent" HorizontalOptions="CenterAndExpand" VerticalOptions="CenterAndExpand" IsVisible="{Binding HideActiveImage}" Source="{u:ImageResource CricApp.Images.inavtive.png}"  Aspect="AspectFit">
                                        <Image.GestureRecognizers>
                                            <TapGestureRecognizer />
                                            <TapGestureRecognizer Command="{Binding PlayerInactiveCommand}" />
                                        </Image.GestureRecognizers>
                                    </Image>
                                </Frame>
                            </Grid>
                        </ViewCell>
                    </DataTemplate>
                </ListView.ItemTemplate>
            </ListView>

Answers

  • igorkr_10igorkr_10 Member ✭✭✭

    Show the method which loads image

  • ImKhanImKhan Member ✭✭

    private void PlayerInactive()
    {
    if (HideActiveImage == true)
    {
    ShowActiveImage = true;
    HideActiveImage = false;
    }
    MessagingCenter.Send(this, MessengerKey.saveplayertodb);
    }

        private void PlayerActive()
        {
            if (ShowActiveImage == true)
            {
    
                HideActiveImage = true;
                ShowActiveImage = false;
    
            }
            MessagingCenter.Send(this, MessengerKey.saveplayertodb);
        }
    
  • igorkr_10igorkr_10 Member ✭✭✭

    Try to change your xaml to:

    <Frame>
        <Image Source="{Binding StatusImage}">
            <Image.GestureRecognizers>
                <TapGestureRecognizer Command="{Binding ChangeStatusCommand}">
            <Image.GestureRecognizers>
        </Image/>
    </Frame>
    

    Your ViewModel for list item must be:

    public class ItemViewModel : INotifyPropertyChanged
    {
        public bool IsActive { get; protected set; }
    
        public ImageSource StatusImage
        {
            get
            {
                if (this.IsActive)
                {
                    return ImageSource.FromFile("active");
                }
                else
                {
                    return ImageSource.FromFile("inactive")
                }
            }
        }
    
        public Command ChangeStatusCommand { get; }
    
        public ItemViewModel()
        {
            this.ChangeStatusCommand=new Command(()=>ChangeStatus());
        }
    
        public void ChangeStatus()
        {
            this.IsActive=!this.IsActive;
            this.OnPropertyChanged(nameof(StatusImage));
        }
    }
    
Sign In or Register to comment.