텍스트 블록과 웹 브라우저 컨트롤이 있습니다. 문제가 있습니다. 예를 들어, 내 웹 브라우저가 google.com으로 이동합니다. 웹 브라우저가 google.com으로 이동했을 때 텍스트 블록에서 google.com으로 제목을 변경하고 싶습니다.WebBrowser 컨트롤에로드 된 문서에서 제목 가져 오기
C#을 사용하여이 문제를 해결하도록 도와주십시오.
텍스트 블록과 웹 브라우저 컨트롤이 있습니다. 문제가 있습니다. 예를 들어, 내 웹 브라우저가 google.com으로 이동합니다. 웹 브라우저가 google.com으로 이동했을 때 텍스트 블록에서 google.com으로 제목을 변경하고 싶습니다.WebBrowser 컨트롤에로드 된 문서에서 제목 가져 오기
C#을 사용하여이 문제를 해결하도록 도와주십시오.
당신은
IE
으로 테스트XAML LoadCompleted
이벤트에 가입, 웹 브라우저의 Text
속성에서 제목 태그를 추출 할 수 있습니다 :
<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 속성이 PropertyChanged를 발생시키지 않아서 바인딩이 업데이트되지 않기 때문에'Title = "{Binding ElementName = webBrowser1, Path = Document.Title}"' –
Navigated 대신 LoadCompleted를 사용하는 변경된 예 –
제목을 설정하거나 자바 스크립트로, 한 번 설정 한 최신 값을받지 않습니다 변경 한 것처럼 문제가 document.title이 속성에 있습니다.
WPF에서 기본 웹 브라우저를 사용하는 대신 FormsHost를 사용하고 우리는 DocumentTitle 및 DocumentTitleChanged 이벤트로 속성을 지원하는 Windows Form의 웹 브라우저를 사용합니다.
다음 컨트롤을 사용하는 것이 좋으며 양식 버전에서 더 많은 기능을 사용할 수 있습니다. 주의 깊게 알아 차리면 두 함수 모두 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(""));
}
그것은 약간의 여분을 구현하기 위해 좀 더 많은 코드가 필요 수도 있지만 모든 것이 더 종속성 속성을 추가하고 논리를 제어함으로써 가능하다.
이 사람은 *) 속성 이름은 IHTMLDocument2_nameProp 입니다 .NET 4.5 를 들어, ERNO 드 Weerd 위의 대답을
var docTitle = document.getElementsByTagName("title")
.Cast<IHTMLElement>()
.FirstOrDefault().innerText;
그냥 작은 오타 픽스 업을 작동 *)에 오타에 고정 블록
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);
}
}
경우
Text 속성이 없습니다. 그러나 LoadCompleted는 Navigated보다 낫습니다. 내 코드를 업데이트 할 것입니다. –
정확한 이름을 잊어 버린 것 같습니다. 어쨌든 Erno의 해결책이 더 좋습니다. – Poma