從Xamarins docs:
// define a interface
public interface IBaseUrl { string Get(); }
// load html
var html = new HtmlWebViewSource();
html.BaseUrl = DependencyService.Get<IBaseUrl>().Get();
htmlSource.Html = @"<html>
<head>
<link rel=""stylesheet"" href=""default.css"">
</head>
<body>
<h1>Xamarin.Forms</h1>
<p>The CSS and image are loaded from local files!</p>
<img src='Images/XamarinLogo.png'/>
</body>
</html>";
webView.Source = html;
然後,您將需要實現IBaseUrl爲你想支持每個平臺。對於iOS,它看起來像這樣:
[assembly: ExportRenderer (typeof (BaseUrlWebView), typeof (BaseUrlWebViewRenderer))]
namespace WorkingWithWebview.iOS {
public class BaseUrlWebViewRenderer : WebViewRenderer {
public override void LoadHtmlString (string s, NSUrl baseUrl) {
baseUrl = new NSUrl (NSBundle.MainBundle.BundlePath, true);
base.LoadHtmlString (s, baseUrl);
}
}
}
感謝您的回答,但它可以像您對GIF文件顯示的那樣簡單地工作?不要忘記,這個GIF是一個動畫而不是一個簡單的圖像 – Emixam23
我不知道它爲什麼不會。但是你必須測試它才能確定。 – Jason
我稍後再測試,謝謝:) – Emixam23