从 WebBrowser 控件中加载的文档获取标题

发布于 2024-10-29 22:57:34 字数 128 浏览 2 评论 0原文

我有一个文本块和一个网络浏览器控件。我遇到问题,例如,我的网络浏览器导航到 google.com。当网络浏览器导航到 google.com 时,我希望文本块将标题更改为 google.com。

请帮助我使用 C# 实现这一点。

I have a textblock and a webbrowser control. I have a problem, for example, my webbrowser navigates to google.com. I want the textblock to change the title to google.com when webbrowser has navigated to google.com.

Please help me achieve this using c#.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

梦回旧景 2024-11-05 22:57:34

使用 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 并且几乎没有任何异常处理:

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

Tested with 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>

Code:

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

Without dynamic and hardly any exception handling:

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);
    }
}
ゃ人海孤独症 2024-11-05 22:57:34

您可以从webBrowser的Text属性中提取标题标签,只需订阅LoadCompleted事件

You can extract title tag from webBrowser's Text property, just subscribe to LoadCompleted event

葬﹪忆之殇 2024-11-05 22:57:34

Document.Title 属性存在问题,就像在 javascript 中设置或更改标题一样,设置后您将无法获得最新值。

我们没有使用 WPF 中的默认 Web 浏览器,而是使用 FormsHost,并且使用 Windows 窗体的 Web 浏览器,它支持 DocumentTitle 属性和 DocumentTitleChanged 事件。

因此,我建议使用以下控件,您可以使用表单版本中提供的更多功能。如果您仔细观察,两者的功能在性能方面完全相同,因为两者都创建 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(""));
}

它可能需要更多的代码来实现一些额外的功能,但通过添加更多的依赖属性和控制逻辑,一切皆有可能。

There is problem with Document.Title property, as if title has been set or changed as in javascript, you will not get the latest value once set.

Instead of using default Web Browser in WPF, we are using FormsHost and we use Windows Form's Web Browser which supports property as DocumentTitle and an event of DocumentTitleChanged.

So I will suggest using following control and you can use more functionality available in forms version. If you notice carefully, both function exactly same performance wise as both create a win32 control and interact with it.

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

It might need some more code to implement little extra but everything is possible by adding more dependency properties and controlling the logic.

§普罗旺斯的薰衣草 2024-11-05 22:57:34

这个可以用

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

This one will work

var docTitle = document.getElementsByTagName("title")
        .Cast<IHTMLElement>()
        .FirstOrDefault().innerText;    
百善笑为先 2024-11-05 22:57:34

对于 .NET 4.5,Erno de Weerd 上面的答案只有一些拼写错误修复
*) 属性名称是 IHTMLDocument2_nameProp
*) 修复 if 块中的拼写错误

  private void webBrowser1_LoadCompleted(object sender, NavigationEventArgs e)
{
    Object doc = webBrowser1.Document;
    this.Title = GetPropertyValue<string>(doc, "IHTMLDocument2_nameProp");
}

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 alue = (T)propertyInfo.GetValue(obj, null);   
        return value;
    }
    else
    {
        throw new Exception("Property " + propertyName + " is not of type " + T);
    }
}

Just little typo fixups for answer above Erno de Weerd, for .NET 4.5
*) property name is IHTMLDocument2_nameProp
*) fix in typo in if block

  private void webBrowser1_LoadCompleted(object sender, NavigationEventArgs e)
{
    Object doc = webBrowser1.Document;
    this.Title = GetPropertyValue<string>(doc, "IHTMLDocument2_nameProp");
}

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 alue = (T)propertyInfo.GetValue(obj, null);   
        return value;
    }
    else
    {
        throw new Exception("Property " + propertyName + " is not of type " + T);
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文