在Xamarin.Froms中创建WebView的扩展以显示GIF。我无法在Android上加载图像

在Xamarin.Froms中创建WebView的扩展以显示GIF。我无法在Android上加载图像,android,xamarin,xamarin.forms,xamarin.android,gif,Android,Xamarin,Xamarin.forms,Xamarin.android,Gif,iOS按预期工作。我也尝试过将.gif添加到资产文件夹(将BaseUrl设置为“file:///android_asset)没有运气。有什么想法吗 imageHtml = "<center><body bgcolor =\"" + GetBackgroundColorHex() + "\"" + heightAndWidth

iOS按预期工作。我也尝试过将.gif添加到资产文件夹(将BaseUrl设置为“file:///android_asset)没有运气。有什么想法吗

imageHtml = "<center><body bgcolor =\""
                        + GetBackgroundColorHex()
                        + "\""
                        + heightAndWidth
                        + ">"
                        + (!string.IsNullOrWhiteSpace(imageFileName)
                            ? "<img src=\"" 
                            + imageFileName 
                            + "\""
                            + heightAndWidth
                            + "/>"
                            : "&nbsp;")
                        + "</body></center>";

            HtmlWebViewSource source = new HtmlWebViewSource()
            {
                Html = imageHtml
            };

            if (Device.RuntimePlatform == Device.Android && !string.IsNullOrWhiteSpace(imageFileName))
            {
                source.BaseUrl = "file:///android_res/drawable/";
            }

            Source = source;

您可以通过资产而不是可绘图文件加载
Android.Webkit.WebView
内容

以下是
Xamarin.Android
代码,用于解析图像以获得其宽度/高度,以确保保留纵横比,并从资产文件夹中显示:

WebView Gif查看器:
注意:
image.gif
位于Android资产(
AndroidResource
)文件夹和iOS资源文件夹(
BundlerSource

将gif文件保存在资产文件夹中。这里的“loading.gif”是文件名

WebView webView=new WebView
{
      Source = new HtmlWebViewSource
      {
          Html = $"<body\"><img src=\"loading.gif\"/></body>"
      }
};
Content = webView;
WebView WebView=新建WebView
{
Source=新的HtmlWebViewSource
{

Html=$"目前,我所有的代码都是针对表单的PCL。这看起来我需要一个针对Android的自定义渲染器。我似乎无法在表单中设置AllowFileAccessFromFileURL。是否可以在PCL中进行设置?@GarrettDanielDeMeyer当你说要创建WebView的扩展时,我假设是自定义平台渲染器。我只是去查看表单WebViewRenderer源代码,他们正在将WebView客户端设置为ChromeClient,如果没有自定义渲染器,它可以正常工作…请参阅我的更新。。。
var src = "out.gif";
var backgroundColor = "#ff0000";
int imageWidth;
int imageHeight;
using (var stream = Assets.Open(src))
using (var options = new BitmapFactory.Options { InJustDecodeBounds = true })
{
    await BitmapFactory.DecodeStreamAsync(stream, null, options);
    imageWidth = options.OutWidth;
    imageHeight = options.OutHeight;
}
var html = $"<body bgcolor={backgroundColor};\"><img src=\"{src}\" alt=\"A Gif file\" width=\"{imageWidth}\" height=\"{imageHeight}\" style=\"width: 100%; height: auto;\"/></body>";
webView.Settings.AllowFileAccessFromFileURLs = true;
webView.LoadDataWithBaseURL("file:///android_asset/", html, "text/html", "UTF-8", "");
var src = "image.gif";
var backgroundColor = "#ff0000";
int imageWidth = 300;
int imageHeight = 200;
var html = $"<body bgcolor={backgroundColor};\"><img src=\"{src}\" alt=\"A Gif file\" width=\"{imageWidth}\" height=\"{imageHeight}\" style=\"width: 100%; height: auto;\"/></body>";
webView.Source = new HtmlWebViewSource
{
    Html = html
};
WebView webView=new WebView
{
      Source = new HtmlWebViewSource
      {
          Html = $"<body\"><img src=\"loading.gif\"/></body>"
      }
};
Content = webView;