2011-04-05 68 views

回答

1

您可以從Web瀏覽器的Text屬性提取標題標籤,只需訂閱LoadCompleted事件

+0

沒有文本屬性。但LoadCompleted比Navigated更好。我會更新我的代碼。 – 2011-04-05 11:11:08

+0

oops看起來像我忘了確切的名字。無論如何,Erno的解決方案更好。 – Poma 2011-04-05 11:34:06

6

與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); 
    } 
} 
+0

這也不能用綁定來完成嗎?像''然後將窗口的上下文設置到瀏覽器或類似的東西?或者你可以命名WebBrowser,而不是設置窗口的DataContext。 – Alxandr 2011-04-05 08:40:13

+0

如果你的意思是'Title =「{Binding ElementName = webBrowser1,Path = Document.Title}」'那麼不會,據我所知,因爲文檔的Title屬性不會引發PropertyChanged,所以綁定不會被更新。 – 2011-04-05 09:41:34

+0

更改了使用LoadCompleted而不是導航的示例 – 2011-04-05 11:12:56

0

有問題,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("")); 
} 

這可能需要一些更多的代碼來實現一些額外的,但一切都可以通過添加更多的依賴屬性和控制邏輯。

0

這一個將工作

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

只是小錯字的修正的答案以上艾爾諾德Weerd,對於.NET 4.5 *)屬性名稱是IHTMLDocument2_nameProp *)在錯字的修正,如果塊

​​
相關問題