【问题标题】:How to show SVG string as an Image in Xamarin?如何在 Xamarin 中将 SVG 字符串显示为图像?
【发布时间】:2021-10-09 01:26:20
【问题描述】:

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

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

我已经尝试了几个小时来解决这个问题,所以我非常感谢一些帮助:S

【问题讨论】:

标签: android xamarin svg


【解决方案1】:

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

点赞FFImageLoading

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

对于安卓:

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

然后使用like:

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);

【讨论】:

  • 感谢您的帮助!第二天继续研究后,我最终使用了这个确切的库。我在最终解决方案中使用了SvgImageSource.FromSvgString
猜你喜欢
  • 2018-03-29
  • 2019-03-21
  • 1970-01-01
  • 2016-02-06
  • 1970-01-01
  • 2021-06-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多