0

我有一个关于 Xamarin.Native SVG 支持文档的问题

在它调用的示例代码中LoadFile

ImageService.Instance
    .LoadFile("image.svg")
    .
    .

为了使它起作用,必须将 image.svg 放在项目的哪个位置?image.svg(内容、AndroidRersource 等)的构建类型是什么?iOS的行为是否相同?

我很难让它工作。我将我的 svg 放在构建类型为AndroidResource.

Android Project
    |
    |
    -> Resources
        |
        |
        -> drawable
            |
            |
            -> image.svg
4

1 回答 1

1

ImageService.Instance.LoadFile:它用于从磁盘加载文件(仅限完整路径)。

您应该使用LoadFileFromApplicationBundle(应用程序包)或LoadCompiledResource(应用程序资源)。

从应用程序包中的文件加载图像。

          /// <summary>
          /// Load an image from a file from application bundle.
          /// eg. assets on Android, compiled resource for other platforms
          /// </summary>
          /// <returns>The new TaskParameter.</returns>
          /// <param name="filepath">Path to the file.</param>
          TaskParameter LoadFileFromApplicationBundle(string filepath);

Xamarin.Android

xml:

  <FFImageLoading.Views.ImageViewAsync
    android:id="@+id/imageView"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"/>

资产:使用 AndroidAsset 的 Build Action

在此处输入图像描述

主要活动:

 var imageView = FindViewById<ImageView>(Resource.Id.imageView);
        var filePath = "sample2.svg";
        ImageService.Instance.LoadFileFromApplicationBundle(filePath).WithCustomDataResolver(new SvgDataResolver(64, 0, true)).Into(imageView);

从应用程序资源的文件中加载图像。使用可绘制文件夹中的资源名称,不带扩展名。

          /// <summary>
          /// Load an image from a file from application resource.
          /// </summary>
          /// <returns>The new TaskParameter.</returns>
          /// <param name="resourceName">Name of the resource in drawable folder without extension</param>
          TaskParameter LoadCompiledResource(string resourceName);

Xamarin.Android

xml:

 <FFImageLoading.Views.ImageViewAsync
    android:id="@+id/imageView"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"/> 

可绘制资源:与 AndroidResource 的 BuildAction

在此处输入图像描述

主要活动:

var filePath = "sample";

ImageService.Instance.LoadCompiledResource(filePath).WithCustomDataResolver(new SvgDataResolver(64, 0, true)).Into(imageView);

在此处输入图像描述

在您提供的链接中,它还显示了从 Xamarin.Forms 上的嵌入式资源加载的方式,您可以检查它。https://github.com/luberda-molinet/FFImageLoading/wiki/SVG-support#xamarinandroid

于 2020-01-08T08:24:04.197 回答