C#   发布时间:2022-04-03  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了c# – 从WebBrowser控件中加载的文档中获取标题大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我有一个文本块和一个webbrowser控件.我有一个问题,例如,我的webbrowser导航到google.com.当webbrowser导航到google.com时,我希望文本块将标题更改为google.com.

请帮我用c#实现这个目标.

解决方法

用IE测试

XAML:

<Grid>
    <WebBrowser LoadCompleted="webBrowser1_LoadCompleted" Height="100" HorizontalAlignment="Left" Margin="73,72,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");
}

没有动态,几乎没有任何异常处理:

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);
    }
}

大佬总结

以上是大佬教程为你收集整理的c# – 从WebBrowser控件中加载的文档中获取标题全部内容,希望文章能够帮你解决c# – 从WebBrowser控件中加载的文档中获取标题所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。