如何从wpf中的Web浏览器控件获取文档的标题

我在TabControl中创建了一个Web浏览器控件。我想将TabItem的Header设置为Web浏览器的Document标题。 我在WebBrowser的Navigated Event中使用了以下代码

dynamic doc = tabBrowser.Document; //tabBrowser is the name of WebBrowser Control tab.Header = doc.Title; //tab is the name of the Tab Item 

但是这段代码不能正常工作。标题仅针对少数网站进行更改。 如何将tabItem的标题设置为文档的标题值?

这是导航function:

  public void tabBrowser_Navigated(object sender, System.Windows.Navigation.NavigationEventArgs e) { urlTextBox.Text = tabBrowser.Source.ToString(); myHistory.addToHistory(tabBrowser.Source.ToString()); BookMarks.addBookmark(tabBrowser.Source.ToString()); dynamic doc = tabBrowser.Document; tab.Header = doc.Title; } 

在我的代码中,我使用了WebBrowser的LoadCompleted事件。 可能在您的导航事件文档中仍未准备好,文档标题不正确或为空。

 private void MyWebBrowser_LoadCompleted_1(object sender, NavigationEventArgs e) { try { MyTextBox.Text = MyWebBrowser.Source.ToString(); Title_doc.Content = ((dynamic)MyWebBrowser.Document).Title; } catch (Exception ex) { MessageBox.Show(ex.Message); } } 

这个会奏效

 var docTitle = document.getElementsByTagName("title") .Cast() .FirstOrDefault().innerText;