我是新來的這種HTML/JavaScript風格的應用程序,我需要從WebBrowser控件返回一些基本的導航和性能數據到我的應用程序。我最初的設置有一個放置在XAML中的WebBrowser控件,C#中的一些事件處理程序,然後嘗試使用InvokeScript從C#調用JavaScript函數,然後通過ScriptNotify獲取數據。目前我無法從JavaScript獲取數據。我使用的JavaScript來自於一個在Web瀏覽器中顯示數據的示例,我試圖修改它,以便可以使用C#將相關的部分綁定到文本塊中。如何從JavaScript返回數據到C#
MainPage.xaml中
<phone:WebBrowser x:Name="Browser" Visibility="Collapsed"
IsScriptEnabled="True"
Loaded="Browser_Loaded"
Navigated="Browser_Navigated"
NavigationFailed="Browser_NavigationFailed"
ScriptNotify="Browser_ScriptNotify"/>
MainPage.xaml.cs中
private string MainUri = "/Html/index.html";
private void Browser_Loaded(object sender, RoutedEventArgs e)
{
//tell the browser the URL to navigate to
Browser.Navigate(new Uri(MainUri, UriKind.Relative));
}
private void Browser_ScriptNotify(object sender, NotifyEventArgs e)
{
TestResultTextBlock.Text = e.Value.ToString();
}
//When textblock is tapped, run the javascript function.
private void CheckTextBlock_Tap(object sender, System.Windows.Input.GestureEventArgs e)
{
object result1 = Browser.InvokeScript("printIt");
}
的index.html(持有的JavaScript)
<!DOCTYPE html>
<html>
<head>
<title></title>
<script type="text/javascript">
function printIt() {
// show page load performance - show a few important bits, not everything
var performanceHTML = "";
if (featureDetect.supported) {
if (featureDetect.unprefixed) {
var t = window.performance.timing;
} else if (featureDetect.prefixed == "ms") {
var t = window.msPerformance.timing;
} else if (featureDetect.prefixed == "webkit") {
var t = window.webkitPerformance.timing;
} else {
alert("Unexpected error while fetching navigation timing data.");
return;
}
var start = t.navigationStart;
var sortable = [];
var maxTime = 0;
var order = ['navigationStart', 'redirectStart', 'redirectEnd',
'fetchStart', 'domainLookupStart', 'domainLookupEnd', 'connectStart',
'secureConnectionStart', 'connectEnd', 'requestStart', 'responseStart',
'responseEnd', 'unloadEventStart', 'unloadEventEnd', 'domLoading', 'domInteractive',
'msFirstPaint', 'domContentLoadedEventStart', 'domContentLoadedEventEnd',
'domContentLoaded', 'domComplete', 'loadEventStart', 'loadEventEnd'];
var perfEvents = Object.keys(t).length ? Object.keys(t) : Object.keys(Object.getPrototypeOf(t));
perfEvents.forEach(function (e) {
if (t[e] && t[e] > 0) {
duration = t[e] - start;
sortable.push([e, duration]);
if (duration > maxTime) {
maxTime = duration;
}
}
});
sortable.sort(function (a, b) {
return a[1] !== b[1] ? a[1] - b[1] : order.indexOf(a[0]) - order.indexOf(b[0]);
});
// If you wanted to log the data to the console, sortable is what you want
// console.log(sortable.toString());
// wrapping in a table simply for layout reasons
performanceHTML = "<table><thead><tr><th scope=\"col\" id=\"xyz\">Property</th><th scope=\"col\" id=\"xyz\">Value</th></tr>";
sortable.forEach(function (s) {
performanceHTML = performanceHTML + "<tr><td>" + s[0] + "</td><td>" + s[1] + "ms</td>";
});
performanceHTML = performanceHTML + "</table>";
}
document.getElementById('performance_div').innerHTML = performanceHTML;
document.getElementById('performance_div').style.visibility = 'visible';
}
</script>
請注意,我是非常新的HTML/JavaScript,這是我第一個使用它的應用程序。我非常熟悉XAML/C#,這正是我想用來呈現javascript函數的結果。我應該怎麼做才能正確設置它?
哦,是的。我的問題是在哪裏放置這個返回字符串值。我相信我的錯誤也是一些數據值返回double而不是字符串,所以一個簡單的'.toString()'固定。我接受你的答案,非常感謝。 – Matthew