我遇到了问题,试图从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);
}
}
这个代码不给我任何错误,但我从来没有看到第二个消息框。我确实看到了第一个(“测试”消息),所以程序正在进入该代码块。
你需要做投HTMLDocument的。 – Mau 2010-07-13 15:40:07
请向我们展示您的代码。 – SLaks 2010-07-13 15:42:16