c# - WebBrowser 文档转换无效

标签 c# .net winforms webbrowser-control

我的页面上有一个 WebBrowser 控件。要在浏览器控件中显示的页面上触发一些 javascript,我调用 this.myBrowser.Document.InvokeScript("Test");

这适用于我的 IE7 开发箱,但在单独的测试机器上我收到以下错误:

Exception Type: System.InvalidCastException
Message: Specified cast is not valid.
Source: System.Windows.Forms
Stack Trace: 
   at System.Windows.Forms.UnsafeNativeMethods.IHTMLDocument2.GetLocation()
   at System.Windows.Forms.WebBrowser.get_Document()
   at InteriorHealth.EmbeddedBrowser.frmMain.CardRead(String strData) in E:\Develop\TestProject\frmMain.cs:line 265
   at MyTest.frmMain.prtCardReader_DataReceived(Object sender, SerialDataReceivedEventArgs e) in E:\Develop\TestProject\frmMain.cs:line 355

测试机运行IE6。虽然我不确定,但我推测可能是 IE 版本的差异导致错误的原因。这听起来对吗?我该如何解决这个问题?

frmMain.cs 的第 265 行是:

 this.webKiosk.Document.InvokeScript(ConfigurationState.CardReader.Error.FireJavaScriptMethod);

frmMain.cs 的第 355 行是:

CardRead(strCardData_m);

我认为 WebBrowser 对象上的 Document 属性调用引发了异常。

最佳答案

想通了。我的调用是从串行端口数据接收事件调用的,该事件不在主 GUI 线程上执行。我使用了 this.Invoke() 方法调用来让我的代码在主 GUI 线程上执行,现在一切都很好!

关于c# - WebBrowser 文档转换无效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/315938/

相关文章:

c# - 从 IEnumerable<KeyValuePair<>> 重新创建字典

c# - 在 Visual Studio 中使用 dotConnect Postgresql 时出现 PgSqlParameter 错误

c# - 在 C# 中开发许可证 - 我从哪里开始?

c# - 当我尝试使用 visual c# 连接 SQL Server 时收到此错误消息

c# - 在 C# 中以编程方式调整图像大小

c# - 在 ContinueWith 中重新抛出先前的异常

c# - 类库、 Entity Framework 代码优先和 Microsoft.SqlServer.Types

c# - 使用 GCloud 模拟器的 Google Cloud PubSub V1

c# - 将缓存数据存储在本地

c# - 绑定(bind)到字典的 DataGridView