瀏覽器的錯誤處理

來源文件:Stackoverflow:Can I detect errors while using a .Net WebBrowser control?

The WebBrowser windows forms control is wrapper around Internet Explorer and it doesn't expose all the functionality of the underlying ActiveX control and particularly the NavigateError event. Here's a workaround:

First add reference to SHDocVw.dll to your project (COM tab of Add Reference window). Then you can do the following to capture errors:

private void button1_Click(object sender, EventArgs e)
{
    SHDocVw.WebBrowser instance = 
(SHDocVw.WebBrowser)webBrowser1.ActiveXInstance;
    instance.NavigateError += 
new SHDocVw.DWebBrowserEvents2_NavigateErrorEventHandler(instance_NavigateError);
    webBrowser1.Navigate("http://www.google.com/foo");
}
void instance_NavigateError(object pDisp, ref object URL, 
  ref object Frame, ref object StatusCode, ref bool Cancel)
{
    // Do whatever you want with the error            
}
	

參考文獻

  1. Stackoverflow:Can I detect errors while using a .Net WebBrowser control? 陳鍾誠 (2011年12月19日),(網頁標題) 瀏覽器的錯誤處理,(網站標題) 免費電子書:C# 程式設計,2011年12月19日,取自 http://cs0.wikidot.com/browsererror ,網頁修改第 2 版。

arrow
arrow

    Johnson峰 發表在 痞客邦 留言(0) 人氣()