2013-07-17 56 views
3

我想調用JavaScript方法在我WebBrowserC#調用JavaScript在web瀏覽器

WebBrowser webBrowser = new WebBrowser(); 
webBrowser.NavigateToString(html); 
webBrowser.LoadCompleted += ((s, r) => 
{ 
     try 
     { 
       object tmp = webBrowser.InvokeScript("GetData"); 
     } 
     catch (Exception exc) 
     { 
     } 
}); 

這是HTML

<html><head> 
<meta http-equiv="content-type" content="text/html; charset=ISO-8859-1"> 
<title></title> 

<script type="text/javascript"> 
    function GetData() 
    { 
     return 50; 
    } 

</script> 

但是,當我調用使用JavaScript:

object tmp = webBrowser.InvokeScript("GetData"); 

我得到Exception

消息:

An unknown error has occurred. Error: 80020006. 

StakeTrace:

at Microsoft.Phone.Controls.NativeMethods.ValidateHResult(Int32 hr) 
    at Microsoft.Phone.Controls.WebBrowserInterop.InvokeScript(String scriptName, String[] args) 
    at Microsoft.Phone.Controls.WebBrowser.InvokeScript(String scriptName) 
    at YouTube.YouTubeExtractor.<>c__DisplayClass1.<ExtractVideoById>b__0(Object s, NavigationEventArgs r) 
+0

上的話題在WebBrowser控件上調用Javascript,請參閱http://stackoverflow.com/a/27468013/262379 –

回答

3

您需要設置WebBrowser.IsScriptEnabledtrue(這是false默認)

+1

這不是WPF WebBrowser控件的屬性。 – Nick