我使用 FFImageLoading 在 ListView 中显示 SVG 图像,如下所示:
1)我有一个包含类别和项目的 XML 文件,我解析它,我有一个 ObservableCollection,每个类别都是一个 ObservableCollection。
2)我有一个自定义的 TintedSvgCachedImage 类,它继承自 SvgCachedImage(只是在可绑定属性上添加一些色调);我将 Source 属性绑定到一个 Category.Label 属性,并使用转换器返回一个 SvgImageSource。
3)如果没有找到相应的嵌入资源,我会捕获异常并返回另一个图像。
当找到图像时,这非常有效。如果没有,我面临两个问题:
1)如果是ListView的最后一个Category,不抛出异常,图片只是“空”,什么都不显示,没有任何错误
2)如果不是最后一个类别,则按预期抛出异常,但替换图像不是我要加载的图像!
我的 XAML 文件:
<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>
和转换器:
// 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;
}
}
问题出在我身上吗?遗漏了什么 ?
还是我必须在 GitHub 上打开一个问题?(我没有找到任何东西,但我没有搜索很多,因为我不确定问题是不是我的问题......)
谢谢,奥利维尔