我需要檢查Windows Phone應用程序中的WebBrowser控件是否有歷史記錄,以及我如何解決該問題的方法是使用browser.InvokeScript("eval", "if(history.length > 0){ history.go(-1) }");
。我需要使用這個或其他一些方法來設置一個變量,所以我只能在WebBrowser有歷史記錄時才能觸發一個函數。我無法弄清楚如何設置它。使用InvokeScript更改C#變量
我使用完整的代碼是這樣的:
protected override void OnBackKeyPress(System.ComponentModel.CancelEventArgs e)
{
var hasHistory = true;
browser.InvokeScript("eval", "if(history.length > 0){ history.go(-1) }");
if (AppSettings.Default.ExitWarning)
{
if (!hasHistory) {
if (MessageBox.Show("Are you sure you want to exit?", "Exit?", MessageBoxButton.OKCancel) != MessageBoxResult.OK)
{
e.Cancel = true;
}
}
}
}
我得到SystemException的80020101當我使用,由於某種原因。任何想法爲什麼? – JacobTheDev
似乎是因爲它無效的JavaScript。我不熟悉直接JS,只是jQuery,返回函數是如何工作的? – JacobTheDev
'eval'是一個JavaScript內置函數。由於某種原因,即使使用空的eval字符串,它也會拋出異常。 –