我遇到了 WebView 控件的问题。我正在开发 Windows 8 Metro Style 应用程序,我试图在 WebView 的内容中调用脚本,但它总是返回空字符串。
我也尝试将其设为 "WebView.InvokeScript("eval", new string[] { "document.getElementById('contentDiv').offsetHeight" }) 但它的工作原理相同 - 空字符串。我真的没有猜到这里出了什么问题。
public void Sethtml(string html) {
var toSet = string.Format(Style, html);
wv.LoadCompleted += wv_LoadCompleted;
wv.NavigateToString(toSet);
}
void wv_LoadCompleted(object sender, Windows.UI.Xaml.Navigation.NavigationEventArgs e)
{
WebView wv = sender as WebView;
string height = wv.InvokeScript("heightFun", new string[0]);
}
public static readonly string Style = @"<!DOCTYPE html>
<html>
<head>
<title></title>
<style>
body {{
font-family: Segoe UI;
}}
</style>
<script type='text/javascript'>
var heightFun = function() {{
return document.getElementById('contentDiv').offsetHeight;
}}
</script>
</head>
<body>
<div id='contentDiv'>
{0}
</div>
</body>";