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