gpt4 book ai didi

c# - 是否可以检测导航事件中是否加载了 IFRAME?

转载 作者:行者123 更新时间:2023-11-30 16:13:23 25 4
gpt4 key购买 nike

使用 WebBrowser control , 我目前没有办法检测 Navigating event 是否存在由于主页的 URL 正在更改,或者是否加载了 IFRAME 而被触发。

无论我将哪个属性放入 IFRAME,Frame 参数始终为空。

我的问题:

是否有机会/技巧从 Navigating 事件处理程序中确定事件是否由 IFRAME 触发?

最佳答案

找到解决方法:

我必须实现额外的 DWebBrowserEvents2 interfacethis Code Project article 中所述获得额外的事件。

然后,我在 BeforeNavigate2 event 中收到通知更多信息。

我发现 pDisp 是一个 IWebBrowser2 interfaceParent property该界面的 NULL 对于顶部窗口和非 NULL 对于 (I)FRAMES。

这对我来说已经足够了,所以我可以写:

public class BrowserExtendedNavigatingEventArgs : CancelEventArgs
{
// ...

public bool IsInsideFrame
{
get
{
var wb = AutomationObject as UnsafeNativeMethods.IWebBrowser2;
return wb != null && wb.Parent != null;
}
}

// ...
}

请注意,您必须确保从创建 Web 浏览器控件的线程调用 IsInsideFrame 属性,否则您可能会遇到访问冲突异常,just as described here :

"...All IE COM objects, including the webbrowser control, are Single Threaded Apartment objects. Which means, among other things, you can only access them from the thread on which they were created. If you need to use them from other threads, you need to marshall them using the GIT or CoMarshallInterThreadInterfaceInStream() and related functions..."

我希望这也能帮助其他人。


替代方案

I got AccessViolationExceptions在访问 wb.Parent 时,虽然我认为我做的一切都是正确的,但我正在寻找替代解决方案。

我在 this Code Project article 中找到了它.

基本上文章使用了另一种方式来确定框架。它通过比较对 Web 浏览器控件的引用来实现。

伪代码:

public void BeforeNavigate2(
object pDisp,
ref object url,
ref object flags,
ref object targetFrameName,
ref object postData,
ref object headers,
ref bool cancel)
{
// ...

var isTopFrame = _browser._axIWebBrowser2.Equals(pDisp);

// ...
}

到目前为止,我的测试运行成功,我真的希望我不再遇到这些可怕的访问冲突异常。

关于c# - 是否可以检测导航事件中是否加载了 IFRAME?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21774360/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com