我有一个带有 webbrowser 的表单。事件:Navigating、Navigated 和 DocumentCompleted 已附加。
这是我告诉它后得到的 .Navigate(new Url("http://google.com"));
20:42:42:036: 1. Navigating to: http://google.com/
20:42:42:545: 2. Navigated: http://www.google.com/
20:42:42:854: 3. Navigating to: about:blank
20:42:43:002: 4. Navigated: about:blank
20:42:43:004: 5. Loaded: about:blank
20:42:43:158: 6. Loaded: http://www.google.com/
有人能解释一下为什么我得到这个 about:blank 东西吗?
代码只是显示每个事件处理程序的e.Url参数。
请您参考如下方法:
似乎自动导航到 about:blank 是 WebBrowser 对象用来确保 HTML 对象在尝试加载您请求的页面之前有效的一种已知的“安全毯”,如 this MSDN article 中所述:
[...] the IWebBrowser2::Navigate2 method is used to navigate to the about:blank page. Navigating to this empty page ensures that MSHTML is loaded and that the HTML elements are available through the Dynamic HTML (DHTML) Object Model.
此外,DocumentCompleted 事件应该起作用。您确定您的事件没有做任何其他事情吗?
我试过这个简单的代码,它按预期工作:
namespace CSharpWindowsPractice
{
using System;
using System.Windows.Forms;
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void webBrowser1_Navigated(object sender, WebBrowserNavigatedEventArgs e)
{
this.listBox1.Items.Add("Navigated to: " + e.Url);
}
private void webBrowser1_Navigating(object sender, WebBrowserNavigatingEventArgs e)
{
this.listBox1.Items.Add("Navigating to: " + e.Url);
}
private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
this.listBox1.Items.Add("DocumentCompleted: " + e.Url);
}
private void button1_Click(object sender, EventArgs e)
{
this.webBrowser1.Navigate(new Uri(@"http://google.com"));
}
}
}
按下按钮后,您将获得以下内容:
