我正在构建一个使用 Xamarin.Forms WebView
的应用程序 . 在Android,iOS和UWP上一切正常 . 但有时在WebView的 HtmlSource
中有一个图像没有找到问题所在 .
我想在 WebView
中显示这个html:
<p><img src="https://www.loi.nl/~/media/images/logos/vakgebieden/ipma.jpg" alt="Img did not load" /></p>
此代码在UWP上正常工作并显示正确的图像 . 但是,当在Android或iOS上运行完全相同的代码时,图像无法找出 WebView
无法在Android或iOS上加载此图像的原因 .
这些图像DID在每个平台上都成功加载:https://www.rabobank.nl/static/generic/css/images/s14/rabobank-logo.png http://www.learnit.nl/static/nl/gratiscursus/photoshop/8/ph_07.jpg https://www.volkswagen.nl/~/media/Volkswagen/Images/Modellen/nieuwe%20up/up-hero-menu2.ashx?bc=White&as=0&h=310&w=860 http://www.volkswagen.nl/~/media/Volkswagen/Images/Modellen/nieuwe%20up/up-hero-menu2.ashx?bc=White&as=0&h=310&w=860
我用来显示WebView的代码:
var browser = new WebView();
var htmlSource = new HtmlWebViewSource();
htmlSource.Html = @"<html><head> <link rel=""stylesheet"" href=""adefault.css""> </head><body>" + c.HtmlSource + "</body></html>";
browser.Source = htmlSource;
htmlSource.BaseUrl = DependencyService.Get<IBaseURL>().Get();
browser.VerticalOptions = LayoutOptions.FillAndExpand;
browser.HorizontalOptions = LayoutOptions.FillAndExpand;
browser.WidthRequest = 100;
browser.HeightRequest = 1000;
在这种情况下, c.HtmlSource
是包含在此问题开头显示的 <p>
的代码 .
有人可以告诉我为什么这个特定的图像没有加载到Android和iOS平台上的Xamarin.Forms WebView中?
在iOS上实现IBaseURL的类的代码:
[assembly: Dependency (typeof (BaseUrl_iOS))]
namespace LOI.iOS
{
public class BaseUrl_iOS : IBaseURL
{
public string Get()
{
return NSBundle.MainBundle.BundlePath;
}
}
}
在Android上实现IBaseURL的类的代码:
[assembly: Dependency (typeof (BaseUrl_Android))]
namespace LOI.Droid
{
public class BaseUrl_Android : IBaseURL
{
public string Get()
{
return "file:///android_asset/";
}
}
}
IBaseURL
界面:
public interface IBaseURL
{
string Get();
}