C# WebBrowser 控件不触发 DocumentCompleted 事件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/711040/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
C# WebBrowser control not firing the DocumentCompleted event
提问by user42931
I have a program that is using the C# WebBrowser control and it needs to detect which page is loaded to determine what to do next. The program works fine on most of the employee's computers at the office, but it does not work on some.
我有一个使用 C# WebBrowser 控件的程序,它需要检测加载了哪个页面以确定下一步要做什么。该程序在办公室员工的大多数计算机上都可以正常工作,但在某些计算机上却无法正常工作。
I have determined the problem is that the documentCompleted
event is not firing on those computers it does not work on.
我已经确定问题是该documentCompleted
事件没有在那些它不起作用的计算机上触发。
This program does use threads to process data, and the webbrowser.navigate
call is made through a delegate. But I have also changed the code to not use delegates for the navigate action with no change in the result.
该程序确实使用线程来处理数据,并且webbrowser.navigate
调用是通过委托进行的。但我也更改了代码,不使用委托进行导航操作,结果没有变化。
I also made a separate program that has just a basic WebBrowser
and debug textfield
, and the DocumentCompleted
event doesfire when using that program.
我还制作了一个单独的程序,它只有一个 basicWebBrowser
和 debug textfield
,并且在使用该程序时会触发该DocumentCompleted
事件。
NOTE: The computers that it is not firing on are older PCs with single core/thread processors.
注意:它不启动的计算机是具有单核/线程处理器的旧 PC。
I am out of ideas on this one, any help would be appreciated.
我对此一无所知,任何帮助将不胜感激。
回答by Erich Mirabal
If it is a threading issue, make sure you are calling Application.DoEvents(). I've had problems with WebBrowser not working right when I failed to do that.
如果是线程问题,请确保您正在调用 Application.DoEvents()。当我未能做到这一点时,我遇到了 WebBrowser 无法正常工作的问题。
回答by Chris Doggett
While trying to solve this question, I originally had my WebBrowser control in a using() block, and it was disposing itself before the DocumentCompleted event could fire. Are you doing something similar, or is the WebBrowser on a form that's staying open?
在尝试解决这个问题时,我最初在 using() 块中有我的 WebBrowser 控件,并且它在 DocumentCompleted 事件可以触发之前自行处理。您是否正在做类似的事情,或者表单上的 WebBrowser 是否保持打开状态?
回答by CodeBlock
@Pavel L:
@帕维尔L:
The problem here is you used a web browser control from mshtml.dll but .NET framework does not include this file. The solution for this is simply copy mshtml.dll to your app dir or set 'Copy local' property of Microsoft.mshtml to True.
这里的问题是您使用了来自 mshtml.dll 的 Web 浏览器控件,但 .NET 框架不包含此文件。解决方案是简单地将 mshtml.dll 复制到您的应用程序目录或将 Microsoft.mshtml 的“复制本地”属性设置为 True。
Sorry for my bad english :D
对不起我的英语不好:D
回答by tbolon
As explained by CodeBlock, this seems to be related by the installation state of Microsoft.mshtml.dll
正如 CodeBlock 所解释的,这似乎与安装状态有关 Microsoft.mshtml.dll
We've got customers where the Microsoft.mshtml.dll
is not present in GAC (nor in computer), and then the WebBrowser
component never fires any event.
我们的客户Microsoft.mshtml.dll
在 GAC 中(也不在计算机中)中不存在 ,然后该WebBrowser
组件从不触发任何事件。
By using Reflector in the WebBrowser
class, the DocumentComplete
event is raised by a subclass named WebBrowserEvent
, which implement a private interface DWebBrowserEvents2
.
通过在WebBrowser
类中使用 Reflector ,该DocumentComplete
事件由名为 的子类引发,该子类WebBrowserEvent
实现了一个私有接口DWebBrowserEvents2
。
This interface is a ComImport
of {34A715A0-6587-11D0-924A-0020AFC7AC4D}
, which, I suppose, is related to Microsoft.mshtml.dll.
这个接口是一个ComImport
of {34A715A0-6587-11D0-924A-0020AFC7AC4D}
,我想它与 Microsoft.mshtml.dll 相关。
So our solution was to install the Office 2003 Redistributable Primary Interop Assemblies, which install the DLL on Program Files then register it on the GAC.
所以我们的解决方案是安装Office 2003 Redistributable Primary Interop Assemblies,它在 Program Files 上安装 DLL,然后在 GAC 上注册它。
Note : Don't pay attention to the .NET Framework 1.1 required or office required, it just copies some dlls.
注意:不要关注需要.NET Framework 1.1 或Office 需要,它只是复制一些dll。
Note 2 : The 2007 packageseems to include the same dll.
注 2:2007 软件包似乎包含相同的 dll。
回答by Pavel L.
Well I'd like to share even more simple solution rather than installing a package.
好吧,我想分享更简单的解决方案,而不是安装软件包。
As it was stated before the DocumentComplete event somehow depends on Microsoft.mshtml.dll file. You can find it on a machine with Visual Studio installed. So it needs to be copied and installed to the target machine. Here's the description of the process:
正如在 DocumentComplete 事件之前所说的那样,以某种方式依赖于 Microsoft.mshtml.dll 文件。您可以在安装了 Visual Studio 的机器上找到它。因此需要将其复制并安装到目标机器上。下面是这个过程的描述:
- Place the Microsoft.mshtml.dll file into the "%ProgramFiles%\Microsoft.NET\Primary Interop Assemblies" folder.
- Then drag and drop it into the "%SystemRoot%\Assembly" folder.
- Register the library with RegAsm.exe utility (which is located in the appropriate subfolder of "%windir%\Microsoft.NET\Framework").
- 将 Microsoft.mshtml.dll 文件放入“%ProgramFiles%\Microsoft.NET\Primary Interop Assemblies”文件夹中。
- 然后将其拖放到“%SystemRoot%\Assembly”文件夹中。
- 使用 RegAsm.exe 实用程序(位于“%windir%\Microsoft.NET\Framework”的相应子文件夹中)注册库。
P.S. May be it may be done in a better way but this solution works and I hope it'll be useful for you.
PS 也许它可以以更好的方式完成,但此解决方案有效,我希望它对您有用。
回答by Stefan Steiger
In order for the documentCompleted-Event to fire,
the visible property of the web browser needs to be set to true.
为了触发 documentCompleted-Event,
需要将 Web 浏览器的可见属性设置为 true。
It can also be on visible, if you have multiple screens.
If you have > 1 screen, switch off all but the main screen, this should resolve the problem.
Alternatively, move the window to the 2nd screen.
如果您有多个屏幕,它也可以是可见的。
如果您有 > 1 个屏幕,请关闭除主屏幕之外的所有屏幕,这应该可以解决问题。
或者,将窗口移动到第二个屏幕。
回答by Reno
Make sure webbrowser.Visible=true;
this works for me, I had similar problems previously.
确保webbrowser.Visible=true;
这对我有用,我以前遇到过类似的问题。