0

I use FFImageLoading to show SVG images in a ListView like this :

1) I have an XML file with categories and items, I parse it, I have an ObservableCollection, each Category is an ObservableCollection.

2) I have a custom TintedSvgCachedImage class which inherits from SvgCachedImage (just adding some tint color on a bindable property) ; I bind the Source property to a a Category.Label property, and use a converter to return an SvgImageSource.

3) If the corresponding embedded resource is not found, I catch the Exception and I return another image.

This works very well when the image is found. When not, I face 2 issues :

1) If it is the last Category in the ListView, no Exception is thrown and the images is just "empty", nothing is displayed, without any error

2) If it is not the last Category, an Exception is thrown as expected, but the replacing image is not the one I wanted to load !

My XAML file :

<ListView.GroupHeaderTemplate>
                    <DataTemplate x:DataType="models:Category">
                        <ViewCell Height="50">
                            <StackLayout Orientation="Horizontal">
                                <ffsvgimg:TintedSvgCachedImage Source="{Binding Label, Converter={StaticResource CategoryNameToSvgImageResource}}"
                                                               TintColor="Accent" />

                                <Label Text="{Binding Name, Converter={StaticResource StringCaseConverter}, ConverterParameter=U}"
                                       Padding="15, 0" VerticalOptions="CenterAndExpand"
                                       FontSize="12" FontAttributes="Bold" Opacity="0.75" />
                            </StackLayout>
                        </ViewCell>
                    </DataTemplate>
                </ListView.GroupHeaderTemplate>

And the converter :

    // Convert Category Name (or Label) to SVG image resource to be displayed in ListView
    public class CategoryNameToSvgImageResourceConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value.GetType() != typeof(string))
                throw new FormatException("CategoryNameToSvgImageResource: argument value is not of type String.");

            try
            {
                return SvgImageSource.FromResource("CardioCALC.Resources." + value.ToString() + ".svg");
            }

            catch (Exception)
            {
                return SvgImageSource.FromResource("CardioCALC.Resources.HeartFailure.svg");
            }
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return value;
        }
    }

Does the problem come from me ? Missing something ?

Or do I have to open an issue on GitHub ? (I did not find any, but I did not search a lot as I am not sure the problem is not my bad...)

Thanks, Olivier

Galactose
  • 175
  • 3
  • 8
  • Did you add a breakpoint in the Convert to debug step by step to see if it returns the right imageSoure you want? – nevermore Apr 21 '20 at 03:07
  • Thanks for your answer. I made tests and the Exception is not thrown, the program continues in the try { } block and no code from the catch { } block is read. – Galactose Apr 21 '20 at 08:44
  • So, is the `"CardioCALC.Resources." + value.ToString() + ".svg"` the right image name you want when the try { } block executed? – nevermore Apr 21 '20 at 09:26
  • Yes and it works well. The problem is that when there is no image with the right name : no exception is thrown and the try { } block continues... – Galactose Apr 21 '20 at 12:56
  • Can you try to add a placeholder image when there is no image? Also, share us a Minimal, Reproducible Example would be better for us to solve the issue. – nevermore Apr 22 '20 at 02:40

0 Answers0