我有一個xamarin.forms應用程序,它使用HybridWebView(https://developer.xamarin.com/guides/xamarin-forms/custom-renderer/hybridwebview/)來顯示一個html文檔。我使用baseUrl將本地css和js加載到文檔中。此代碼在iOS 9.x上運行良好。xamarin.forms webview baseurl無法在設備上工作(iOS 10.1)
但是,我升級到iOS 10後,代碼在iOS模擬器上工作,但不在實際設備上。在設備上,它不會從本地存儲裝載css。
這裏是我正在使用的測試代碼。它適用於模擬器,但不適用於設備。
async void Handle_TestButtonClicked (object sender, System.EventArgs e)
{
string css = @"
body {
background-color: powderblue;
}
h1 {
color: blue;
}
p {
color: red;
}
";
var folder = await AppGlobals.AppStorage.GetContentFolder();
string filename = "styles.css";
await AppGlobals.AppStorage.WriteToFile (folder, filename, css);
string html = @"<!DOCTYPE html>
<html>
<head>
<link rel=""stylesheet"" href=""styles.css"">
</head>
<body>
<h1>This is a heading</h1>
<p>This is a paragraph.</p>
</body>
</html>";
this.DocumentView.Html = html;
}
在HybridWebView,我使用下面的代碼來設置的baseUrl
if (Element.Html != null) {
string baseFolderPath = await AppStorage.GetInstance().GetContentFolderPath();
var baseUrl = new NSUrl (baseFolderPath, true);
Control.LoadHtmlString (Element.Html, baseUrl);
}
我使用PCLStorage圖書館讀/寫本地存儲。