0

我有一个带有 WebBrowser 控件的 WPF 应用程序

我需要获取 javascript 对象的 JSON 字符串,如下所示:

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

我正在 WebBrowser 的 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>
4

2 回答 2

0

WPF WebBrowser.InvokeScript具有与 WinForms HtmlDocument.InvokeScript不同的方法签名。尝试这个:

var json = this.WebBrowser.InvokeScript("eval", "JSON.stringify(window.mainThing)"); 
于 2013-09-06T07:18:14.943 回答
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
于 2013-09-06T08:09:39.973 回答