2010-07-13 117 views
5

我遇到了问题,试图从C#中的WebBrowser获取文档标题。它在VB.NET中工作正常,但它不会给我任何C#中的属性。C#:如何从WebBrowser元素获取文档标题?

当我输入MyBrowser.Document。,我得到的唯一选项是4个方法:Equals,GetHashCode,GetType和ToString - 没有属性。

我认为这是因为我必须先将文档分配给新的实例,但我找不到VB.NET中存在的HTMLDocument类。

基本上我想要做的是每次WebBrowser加载/重新加载页面时返回Document.Title。

有人可以帮忙吗?这将非常感谢!

这里是我此刻的代码...

private void Link_Click(object sender, RoutedEventArgs e) 
{ 
    WebBrowser tempBrowser = new WebBrowser(); 
    tempBrowser.HorizontalAlignment = HorizontalAlignment.Left; 
    tempBrowser.Margin = new Thickness(-4, -4, -4, -4); 
    tempBrowser.Name = "MyBrowser"; 
    tempBrowser.VerticalAlignment = VerticalAlignment.Top; 
    tempBrowser.LoadCompleted += new System.Windows.Navigation.LoadCompletedEventHandler(tempBrowser_LoadCompleted); 

    tempTab.Content = tempBrowser; // this is just a TabControl that contains the WebBrowser 

    Uri tempURI = new Uri("http://www.google.com"); 
    tempBrowser.Navigate(tempURI); 
} 

private void tempBrowser_LoadCompleted(object sender, EventArgs e) 
{ 
    if (sender is WebBrowser) 
    { 
     MessageBox.Show("Test"); 
     currentBrowser = (WebBrowser)sender; 
     System.Windows.Forms.HtmlDocument tempDoc = (System.Windows.Forms.HtmlDocument)currentBrowser.Document; 
     MessageBox.Show(tempDoc.Title); 
    } 
} 

这个代码不给我任何错误,但我从来没有看到第二个消息框。我确实看到了第一个(“测试”消息),所以程序正在进入该代码块。

+0

你需要做投HTMLDocument的。 – Mau 2010-07-13 15:40:07

+0

请向我们展示您的代码。 – SLaks 2010-07-13 15:42:16

回答

2

您没有使用Windows窗体WebBrowser控件。我认为你得到了ieframe.dll的COM包装,它的名字是AxWebBrowser。通过在解决方案资源管理器窗口中打开参考节点来验证。如果你看到AxSHDocVw,那么你得到了错误的控制。这是相当不友好的,它只是给你一个不透明的Document属性的接口指针。你确实只会得到默认的对象类成员。

查看工具箱。选择WebBrowser而不是“Microsoft Web Browser”。

+0

Hans,解决方案资源管理器显示SHDocVw。我的应用程序是在WPF中,我只用默认的WebBrowser控件。我是否需要添加“使用System.Windows.Forms”引用并使用该WebBrowser? – 2010-07-13 17:35:13

+0

那么,就是这样。检查此:http://social.msdn.microsoft.com/forums/en-US/wpf/thread/e60b671e-84e6-40a4-a37a-e0a8610aef44 – 2010-07-13 17:39:51

+0

谢谢,我会查看链接,看看我能做些什么。然而,我觉得奇怪的是,我对当前的代码做了更多的工作,并没有发现任何错误,但是我没有得到我想要做的事情。你可以看看我上面编辑的文章中的代码,看看有没有什么东西可以与它一起看?这可能是不可能的,但我认为如果是这样的话,它会给我一个错误信息。 – 2010-07-13 18:18:17

0
string title = ((HTMLDocument)MyBrowser.Document).Title 

或者

HTMLDocument Doc = (HTMLDocument)MyBrowser.Document.Title ; 
string title = doc.Title; 
4

添加参考Microsoft.mshtml

添加事件接收器LoadCompleted

webbrowser.LoadCompleted += new LoadCompletedEventHandler(webbrowser_LoadCompleted); 

然后,你将有文件没有问题,没有被加载,以便读取值回了

void webbrowser_LoadCompleted(object sender, NavigationEventArgs e) 
    { 
     // Get the document title and display it 
     if (webbrowser.Document != null) 
     { 
      mshtml.IHTMLDocument2 doc = webbrowser.Document as mshtml.IHTMLDocument2; 
      Informative.Text = doc.title; 
     } 
    } 
0

LoadCompleted不会触发。你应该使用Navigated事件处理程序来代替它。

webBrowser.Navigated += new NavigatedEventHandler(WebBrowser_Navigated); 

(...) 

private void WebBrowser_Navigated(object sender, NavigationEventArgs e) 
{ 
     HTMLDocument doc = ((WebBrowser)sender).Document as HTMLDocument; 

     foreach (IHTMLElement elem in doc.all) 
     { 
      (...) 
     } 
     // you may have to dispose WebBrowser object on exit 
} 
0

最后用效果很好:

using System.Windows.Forms; 

...

WebBrowser CtrlWebBrowser = new WebBrowser(); 

...

CtrlWebBrowser.Document.Title = "Hello World"; 
MessageBox.Show(CtrlWebBrowser.Document.Title);