简体   繁体   English

C#Web浏览器控件阻止父级的Load事件

[英]C# Web Browser Control blocks parent's Load event

This may come across as incredibly stupid, but I cannot figure out if: 这可能会变得非常愚蠢,但是我无法确定是否:

  • I am an idiot 我是一个白痴
  • I have misunderstood something 我误会了
  • The MS Web Browser control is bugged MS Web浏览器控件存在错误

I prefer to think that it is the latter. 我更倾向于认为是后者。

I have a Web Browser control in a WinForms user control. 我在WinForms用户控件中有一个Web浏览器控件。 It has been added to the control at design time, and in theory, in the Load event of the control it should navigate to Google. 它已在设计时添加到控件中,理论上,在控件的Load事件中,它应导航到Google。

Seems straightforward. 似乎很简单。

However. 然而。

public partial class TVHost : UserControl
{
    public TVHost()
    {
        InitializeComponent();
    }

    private void TVHost_Load(object sender, EventArgs e)
    {     
        webBrowser1.Navigate("http://google.co.uk");  
    }
}

This doesn't work. 这行不通。 No error, just nothing. 没错,没事。 Inserting a breakpoint/debug lines shows me that the Load event doesn't even get called. 插入一个断点/调试行告诉我Load事件甚至没有被调用。

I decided at this point to check that the Load event is being set correctly in the Designer.cs file. 我此时决定检查在Designer.cs文件中是否正确设置了Load事件。

 this.Load += new System.EventHandler(this.TVHost_Load);

Seems legit. 似乎是合法的。

If I remove the web browser control from the form, the load event fires. 如果我从表单中删除Web浏览器控件,则会触发load事件。

I don't understand this one bit, how can a control prevent a method which uses it from firing in the first place? 我一点都不明白,控件如何才能阻止使用它的方法首先触发?

Moving on, I found this: http://social.msdn.microsoft.com/Forums/en-US/Vsexpressvcs/thread/d6e427b2-9cc9-4318-bb05-11363025e3f7/ 继续前进,我发现了这一点: http : //social.msdn.microsoft.com/Forums/en-US/Vsexpressvcs/thread/d6e427b2-9cc9-4318-bb05-11363025e3f7/

TL;DR for the link is as follows: "Load won't work if you have a webbrowser on the form which is set to Visible = true" 链接的TL; DR如下:“如果表单上的Web浏览器设置为Visible = true,则加载将不起作用”

So sure as hell, if I change the default visibility of the webbrowser to false, the load event of the control fires. 可以肯定的是,如果将Web浏览器的默认可见性更改为false,则会触发控件的load事件。 I can work around the problem by setting the visibility of the browser in the load event. 我可以通过在load事件中设置浏览器的可见性来解决此问题。

private void TVHost_Load(object sender, EventArgs e)
{
    webBrowser1.Visible = true;
    webBrowser1.Navigate("http://google.co.uk");
}

Very odd. 很奇怪。

Whilst this "fix" works, I find it incredibly hacky and was wondering if anybody has any explaination for this behaviour? 尽管此“修复程序”有效,但我发现它非常难以置信,并且想知道是否有人对此行为有任何解释?

Amazingly I have found this bug in MS Connect, left over from 2005 - http://connect.microsoft.com/VisualStudio/feedback/details/116535/when-adding-a-webbrowser-control-to-a-user-control-the-load-will-not-fire# 令人惊讶的是,我在MS Connect中发现了该错误,该错误是2005年遗留下来的-http://connect.microsoft.com/VisualStudio/feedback/details/116535/when-adding-a-webbrowser-control-to-a-user-control加载不会触发#

From the discussion in the Connect bug you linked to: Connect错误的讨论中,您链接到:

For now, if you want to get the Load event to fire, you can set the URL property of the WebBrowser control in the property grid. 现在,如果要触发Load事件,可以在属性网格中设置WebBrowser控件的URL属性。 The URL can be anything you want, even about:blank if you don't want it to start with a page loaded. 该URL可以是您想要的任何内容,即使您不希望它以加载的页面开头也可以是空白。

So if you go into the designer and set the WebBrowser 's Url property to the string about:blank (which tells the WebBrowser to load an empty page), then your user control should start getting its Load event again. 因此,如果您进入设计器并将WebBrowserUrl属性设置为字符串about:blank (它告诉WebBrowser加载空白页),则您的用户控件应再次开始获取其Load事件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM