2013-09-05 68 views
0

我有一個WPF應用程序使用WebBrowser控件如何在WPF應用程序

我需要一個JavaScript對象的JSON字符串,像這樣返回一個JavaScript對象的JSON:

var json = this.WebBrowser.InvokeScript("eval", new object[] { "JSON.stringify(window.mainThing)" }); 

我在Web瀏覽器的LoadCompleted事件

,這將引發異常

System.Runtime.InteropServices.COMException執行此代碼是由未處理我們呃代碼

HResult=-2147352319 
    Message=Exception from HRESULT: 0x80020101 
    Source=WindowsBase 
    ErrorCode=-2147352319 
    StackTrace: 
     at MS.Internal.Interop.HRESULT.ThrowIfFailed(String message) 
     at System.Windows.Controls.WebBrowser.InvokeScript(String scriptName, Object[] args) 

這裏的JavaScript中的頁面:

<script type="text/javascript"> 
    window.mainThing = { x : 1 }; 
</script> 

回答

0

WPF WebBrowser.InvokeScript具有的WinForms HtmlDocument.InvokeScript不同的方法簽名。試試這個:

var json = this.WebBrowser.InvokeScript("eval", "JSON.stringify(window.mainThing)"); 
+1

不,問題是出在IE的兼容性視圖。我認爲JSON在舊版IE中不可用。檢查這個:http://caniuse.com/#search=JSON –

0

我找到了解決方案。問題是在IE的兼容性視圖 我需要設置一些註冊表項

Windows Registry Editor Version 5.00 

[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Internet Explorer\Main\FeatureControl\FEATURE_BROWSER_EMULATION] 
"vshost.exe"=dword:00002328 
"vshost32.exe"=dword:00002328 
"MyAppName.exe"=dword:00002328 
"MyAppName.vshost.exe"=dword:00002328 

[HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\Internet Explorer\Main\FeatureControl\FEATURE_BROWSER_EMULATION] 
"vshost.exe"=dword:00002328 
"vshost32.exe"=dword:00002328 
"MyAppName.exe"=dword:00002328 
"MyAppName.vshost.exe"=dword:00002328 

[HKEY_CURRENT_USER\Software\Microsoft\Internet Explorer\Main\FeatureControl\FEATURE_BROWSER_EMULATION] 
"vshost.exe"=dword:00002328 
"vshost32.exe"=dword:00002328 
"MyAppName.exe"=dword:00002328 
"MyAppName.vshost.exe"=dword:00002328