如何在WPF应用程序中返回javascript对象的JSON

如何在WPF应用程序中返回javascript对象的JSON,wpf,webbrowser-control,Wpf,Webbrowser Control,我有一个带有WebBrowser控件的WPF应用程序 我需要获取javascript对象的JSON字符串,如下所示: var json = this.WebBrowser.InvokeScript("eval", new object[] { "JSON.stringify(window.mainThing)" }); 我正在WebBrowser的LoadCompleted事件上执行此代码 这会引发异常 用户代码未处理System.Runtime.InteropServices.COMExc

我有一个带有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:


window.mainThing={x:1};
WPF具有与WinForms不同的方法签名。试试这个:

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

我找到了解决办法。问题出在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

不,问题出在IE兼容性视图中。我认为JSON在旧版本的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