我正在使用
System.Windows.Forms.WebBrowser
C# 中用于显示某些页面的控件。当用户单击不存在的页面的 url 时,我想做一些自定义工作。
基本上我想在浏览器显示以下消息时设置一些值
The page cannot be displayed
The page you are looking for is currently unavailable. The Web site might be experiencing technical difficulties
如何获取状态以便区分加载页面和错误页面?
最佳答案
如果将 WebBrowser 转换为底层 ActiveX 实现,则可以访问 NavigateError 事件。
注意:您需要添加对 SHDocVw 的引用。令人困惑的是,这是在名称为“Microsoft Internet Controls”的 COM 选项卡中,路径为 c:\windows\system32\ieframe.dll
private void button1_Click(object sender, EventArgs e)
{
//Note: you need to wait until the ActiveXInstance property is initialised.
var axWebBrowser = (SHDocVw.WebBrowser)webBrowser1.ActiveXInstance;
axWebBrowser.NavigateError += axWebBrowser_NavigateError;
webBrowser1.Url = new Uri("http://www.thisisnotavaliddomain.com");
}
void axWebBrowser_NavigateError(object pDisp, ref object URL, ref object Frame, ref object StatusCode, ref bool Cancel)
{
//handle your error
}
关于c# - 如何在 System.Windows.Forms.WebBrowser 中判断加载的页面是错误页面还是有效页面?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8987691/