0
votes

I have a dynamically created ListView. I would like if the image is not found it skips that row. I'm not sure if that is doable, but I only want to display item with images.

         <ListView.ItemTemplate>
            <DataTemplate>
                 <ViewCell>
                    <StackLayout x:Name="{Binding Name}" 
                                 Orientation="Horizontal"
                                 Padding="5">
                        <Image x:Name="categoryImage" Source="{Binding ImageID, Converter={StaticResource converter}}" 
                               Aspect="AspectFit" 
                               WidthRequest="130"/>
                        <Label x:Name="categoryLabel" Text="{Binding Description}"/>
                   </StackLayout> 
                </ViewCell>
            </DataTemplate>
        </ListView.ItemTemplate>
    </ListView>

Value converter

 public class ImagePostValueConverter : IValueConverter
    {
        public int source { get; set; }

        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            var source = (int)value;
            if (source == 0)
                return null;
            var imageSource = ImageSource.FromResource($"ERPProject.AllImages.MyImages.ea_{source }.png", typeof(ImagePostValueConverter).GetTypeInfo().Assembly);
            return imageSource;

        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }

I am pulling these images from a folder in my PCL library. Not really sure how to accomplish this.

EDIT: SOLVED if anyone has this problem I figured out a working solution. Lucas Zhang helped me come to this solution but I tweaked it to work for me. Essentially I did create a whole new valueconverter and Binded the true or false to the IsVisible attribute for the stacklayout. However sine I am passing in the ImageID which is the ID of the record, i just use the recordID as the ImageID as well that is how my images are named.. (e.g. image_ID.png so Image_1.png) 0 doesn't exist like assumed in Lucas' answer. I used Assembly.GetExecutingAssembly().GetManifestResourceStream(myPathtoImage); put that in a variable and then see if the variable is null. If it is then I return false.else return true and display the item in the listview.

1
you need to pre-process your data and remove any items that have a missing imageJason

1 Answers

1
votes

You could binding the IsVisible

<StackLayout x:Name="{Binding Name}" 
             Orientation="Horizontal"
             Padding="5"
             IsVisible="Binding ImageID, Converter={StaticResource IsVisibleConverter}}"
             >
                        <Image x:Name="categoryImage" Source="{Binding ImageID, Converter={StaticResource converter}}" 
                               Aspect="AspectFit" 
                               WidthRequest="130"/>
                        <Label x:Name="categoryLabel" Text="{Binding Description}"/>
</StackLayout>

in Converter

public class IsVisibleConverter: IValueConverter
    {
        public int source { get; set; }

        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            var source = (int)value;
            if (source == 0)
                return false;
            else
                return true;

        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }