在 Xamarin 表单上使用 WebView:
在文件.xaml添加源:
<WebView HorizontalOptions="Center"
VerticalOptions="Center"
HeightRequest="{Binding HeightImage}"
WidthRequest="{Binding WidthImage}"
Source="{Binding LinkImageGif}" />
在文件.cs添加源:
int imageHeight = HeightImage;
int imageWidth = WidthImage;
var source = new HtmlWebViewSource
{
Html = $"<body\"><img src=\"{_srcImage}\" alt=\"A Gif file\" width=\"{imageWidth}\" height=\"{imageHeight}\" style=\"width: 100%; height: auto;\"/></body>",
BaseUrl = DependencyService.Get<IBaseUrl>().Get()
};
因为Android和iOS的BaseUrl是不同的。所以,使用 Share Project,创建一个依赖。
首先,创建一个Interface:
namespace abcxyz
{
public interface IBaseUrl { string Get(); }
}
在Android的依赖,添加代码:
[assembly: Dependency(typeof(BaseUrlAndroid))]
namespace abcxyz
{
public class BaseUrlAndroid : IBaseUrl
{
public string Get() => "file:///android_asset/";
}
}
(Gif 图片将添加到文件夹:AndroidResource 的资产)
在iOS的依赖,添加代码:
[assembly: Dependency(typeof(BaseUrlOniOs))]
namespace abcxyz
{
public class BaseUrlOniOs : IBaseUrl
{
public string Get()
{
return NSBundle.MainBundle.BundlePath;
}
}
}
(Gif图片将添加到iOS的Resource(BundleResource)文件夹)