如何在 Xamarin 中将 SVG 字符串显示为图像?

How to show SVG string as an Image in Xamarin?

所以简单的想法是,我将 SVG 数据作为从 Internet 获取的字符串。 我想在我的应用程序中将其显示为图标。有什么办法可以做到这一点吗?

我见过无数示例,其中 SVG 数据位于应用程序目录中的文件中,然后显示该文件,但这不是我要找的。在 http 请求之后,我确实拥有 XML 格式的数据,我只需要将其转换为图像或屏幕上可见的其他内容。

几个小时以来,我一直在努力寻找解决方案,所以非常感谢您的帮助 :S

Android不支持直接用ImageView显示svg,可以用一些常用的第三方控件显示SVG。

喜欢FFImageLoading

Xamarin.FFImageLoading.Svg nuget 包添加到平台项目。使用 ImageService 和 svg 数据解析器来显示 svg 图像。

对于Android:

<ImageView
    android:id="@+id/image_view"
    android:layout_width="match_parent"
    android:layout_height="match_parent"/>

然后像这样使用:

var svgString = @"<svg><rect width=""30"" height=""30"" style=""fill:blue"" /></svg>";
ImageView imageView = FindViewById<ImageView>(Resource.Id.image_view);
ImageService.Instance
    .LoadString(svgString)
    .LoadingPlaceholder(placeHolderPath)
    .WithCustomDataResolver(new SvgDataResolver(64, 0, true))
    .WithCustomLoadingPlaceholderDataResolver(new SvgDataResolver(64, 0, true))
    .Into(imageView);