我遇到了WebView控件的问题。我正在开发Windows 8 Metro Style应用程序,我试图在WebView的内容中调用脚本,但它总是返回空字符串。我尝试使它成为“WebView.InvokeScript(”eval“,new string [] {”document.getElementById('contentDiv')。offsetHeight“}),但它的作用相同 - 空字符串。我真的没有任何猜测什么错在这里WebView.InvokeScript始终返回空字符串
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>";
对于那些有simmilar问题 - 如果你想从JavaScript传递变量 - 你必须调用.toString()方法 - 否则将引发异常。从window.external.notify返回的值将可用于从WebView.ScriptNotify事件处理程序获取。标记为答案。 – TrN