我遇到了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