如何从 wpf 中的 Web 浏览器控件获取文档的标题 [英] How to get the Document's Title from a Web Browser control in wpf
问题描述
我在 TabControl 中创建了一个 Web 浏览器控件.我想将 TabItem 的标题设置为 Web 浏览器的文档标题.我在 WebBrowser 的 Navigated Event 中使用了以下代码
I have created a Web Browser control in a TabControl.I want to set the Header of the TabItem to the Document's title of the Web Browser. I used the following code in the Navigated Event of the WebBrowser
dynamic doc = tabBrowser.Document; //tabBrowser is the name of WebBrowser Control
tab.Header = doc.Title; //tab is the name of the Tab Item
但是这段代码不能正常工作.标题仅针对少数站点更改.如何将 tabItem 的标题设置为文档的标题值?
But this piece of code doesn't work as it should.The header changes only for a few site. How can i set the Header of the tabItem to the Document's Title Value?
这是导航功能:
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 事件.可能在您的导航事件文档中仍未准备好并且文档标题不正确或为空.
In my code I used LoadCompleted event of WebBrowser. Probably in your Navigated Event document still not ready and document title not right or empty.
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);
}
}
这篇关于如何从 wpf 中的 Web 浏览器控件获取文档的标题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!