我有一个textblock和一个webbrowser控件。例如,我有一个问题,我的浏览器导航到google.com。当浏览器导航到google.com时,我希望textblock将标题更改为google.com。从WebBrowser控件中加载的文档中获取标题
请帮我用c#实现这个功能。
我有一个textblock和一个webbrowser控件。例如,我有一个问题,我的浏览器导航到google.com。当浏览器导航到google.com时,我希望textblock将标题更改为google.com。从WebBrowser控件中加载的文档中获取标题
请帮我用c#实现这个功能。
您可以从Web浏览器的Text
属性提取标题标签,只需订阅LoadCompleted
事件
与IE
测试XAML:
<Grid>
<WebBrowser LoadCompleted="webBrowser1_LoadCompleted" Height="100" HorizontalAlignment="Left" Margin="73,72,0,0" Name="webBrowser1" VerticalAlignment="Top" Width="200" />
<Button Content="Go" Click="Button_Click" HorizontalAlignment="Right" VerticalAlignment="Bottom"/>
</Grid>
代码:
private void webBrowser1_LoadCompleted(object sender, NavigationEventArgs e)
{
dynamic doc = webBrowser1.Document;
this.Title = doc.Title;
}
private void Button_Click(object sender, RoutedEventArgs e)
{
webBrowser1.Navigate("http://google.com");
}
没有dynamic
和哈RDLY任何异常处理:
private void webBrowser1_LoadCompleted(object sender, NavigationEventArgs e)
{
Object doc = webBrowser1.Document;
this.Title = GetPropertyValue<string>(doc, "Title");
}
private T GetPropertyValue<T>(object obj, string propertyName)
{
Type objectType = obj.GetType();
PropertyInfo propertyInfo = objectType.GetProperty(propertyName);
Type propertyType = propertyInfo.PropertyType;
if(propertyType == typeof(T))
{
object propertyValue = (T)info.GetValue(obj, null);
return value;
}
else
{
throw new Exception("Property " + propertyName + " is not of type " + T);
}
}
这也不能用绑定来完成吗?像'
如果你的意思是'Title =“{Binding ElementName = webBrowser1,Path = Document.Title}”'那么不会,据我所知,因为文档的Title属性不会引发PropertyChanged,所以绑定不会被更新。 – 2011-04-05 09:41:34
更改了使用LoadCompleted而不是导航的示例 – 2011-04-05 11:12:56
有问题,document.title时财产,仿佛冠军已经设置或改变在JavaScript中,你不会得到一次设置的最新值。
不使用WPF中的默认Web浏览器,而是使用FormsHost,并使用支持作为DocumentTitle属性和DocumentTitleChanged事件的Windows Form Web浏览器。
所以我会建议使用下面的控件,你可以使用更多的功能可用的表单版本。如果您注意,两者的功能完全相同,因为它们都创建了一个win32控件并与其交互。
public class FormsWebBrowser :
System.Windows.Forms.Integration.WindowsFormsHost
{
System.Windows.Forms.WebBrowser Browser =
new System.Windows.Forms.WebBrowser();
public FormsWebBrowser()
{
Child = Browser;
Browser.DocumentTitleChanged +=
new EventHandler(Browser_DocumentTitleChanged);
}
void Browser_DocumentTitleChanged(object sender, EventArgs e)
{
this.DocumentTitle = Browser.DocumentTitle;
}
///<summary>
/// This will let you bind
///</summary>
public string DocumentTitle
{
get { return (string)GetValue(DocumentTitleProperty); }
private set { SetValue(DocumentTitleProperty, value); }
}
public static readonly DependencyProperty DocumentTitleProperty =
DependencyProperty.Register("DocumentTitle", typeof(string),
typeof(FormsWebBrowser), new FrameworkPropertyMetadata(""));
}
这可能需要一些更多的代码来实现一些额外的,但一切都可以通过添加更多的依赖属性和控制逻辑。
这一个将工作
var docTitle = document.getElementsByTagName("title")
.Cast<IHTMLElement>()
.FirstOrDefault().innerText;
只是小错字的修正的答案以上艾尔诺德Weerd,对于.NET 4.5 *)属性名称是IHTMLDocument2_nameProp *)在错字的修正,如果块
没有文本属性。但LoadCompleted比Navigated更好。我会更新我的代码。 – 2011-04-05 11:11:08
oops看起来像我忘了确切的名字。无论如何,Erno的解决方案更好。 – Poma 2011-04-05 11:34:06