获取 WebView 的内容高度 - Windows Phone 8.1
Get WebView's content height - Windows Phone 8.1
我尝试获取 WebView 中实际 HTML 内容的高度,以便根据内容设置高度。
这是我的脚本,但在调用脚本时我得到一个空字符串。
private async Task LoadHTMLContent(ItemViewModel itemVm)
{
var htmlScript = "<script>function getDocHeight() { " +
"return document.getElementById('pageWrapper').offsetHeight; } </script>";
var htmlConcat = string.Format("<html><head>{0}</head>" +
"<body style=\"margin:0;padding:0;\" " +
">" +
"<div id=\"pageWrapper\" style=\"width:100%;" +
"\">{1}</div></body></html>", htmlScript, itemVm.Model.Content);
webView.NavigationCompleted += webView_NavigationCompleted;
webView.NavigateToString(htmlConcat);
}
async void webView_NavigationCompleted(WebView sender, WebViewNavigationCompletedEventArgs args)
{
string pageContentHeight = await webView.InvokeScriptAsync("getDocHeight", null);
}
好吧,我找到了解决办法。
如果您也想这样做,欢迎在 GitHub 上查看我的解决方案:
https://github.com/romshiri/sizeable-webview
我尝试获取 WebView 中实际 HTML 内容的高度,以便根据内容设置高度。
这是我的脚本,但在调用脚本时我得到一个空字符串。
private async Task LoadHTMLContent(ItemViewModel itemVm)
{
var htmlScript = "<script>function getDocHeight() { " +
"return document.getElementById('pageWrapper').offsetHeight; } </script>";
var htmlConcat = string.Format("<html><head>{0}</head>" +
"<body style=\"margin:0;padding:0;\" " +
">" +
"<div id=\"pageWrapper\" style=\"width:100%;" +
"\">{1}</div></body></html>", htmlScript, itemVm.Model.Content);
webView.NavigationCompleted += webView_NavigationCompleted;
webView.NavigateToString(htmlConcat);
}
async void webView_NavigationCompleted(WebView sender, WebViewNavigationCompletedEventArgs args)
{
string pageContentHeight = await webView.InvokeScriptAsync("getDocHeight", null);
}
好吧,我找到了解决办法。
如果您也想这样做,欢迎在 GitHub 上查看我的解决方案: https://github.com/romshiri/sizeable-webview