WPF (vb.net) 从网页到 label/text 块的文本
WPF (vb.net) Text from web page to label/text block
我想从我的 HTML 页面读取文本并将其显示到我的 WPF 应用程序中的 Label 或 TextBlock 中。我该怎么做?
使用 Windows 表格我是这样做的:
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
WebBrowser1.Navigate("C:\Users\Test\Desktop\Webseite\test.htm")
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim ImageInhalt As String
Label1.Text = WebBrowser1.Document.GetElementById("mytext").GetAttribute("value")
ImageInhalt = WebBrowser1.Document.GetElementById("mytext2").GetAttribute("value")
If ImageInhalt.Contains("STOP") Then
PictureBox1.Image = My.Resources._stop
End If
End Sub
End Class
HTML 页 (test.htm)
<input type="text" name="userInfoNav" value="Hallo"/><br/>"
现在非常感谢您的帮助
您必须将 URL 或文件的 HTML 内容加载到 mshtml.HTMLDocument
中。然后,您可以使用类似 JavaScript 的语法处理 DOM 对象。
需要参考 Microsoft.mshtml.dll。
C#
partial class MainWindow : Window
{
private mshtml.HTMLDocument HtmlDocument { get; set; }
public async Task SetTextBoxTextAsync(string filePath, string id)
{
await UpdateHtmlDocumentAsync(filePath);
string value = GetHtmlElementValueById(id);
this.TextBlock.Text = value;
}
private async Task UpdateHtmlDocumentAsync(string filePath)
{
string htmlContent = await File.ReadAllLinesAsync(filePath);
this.HtmlDocument = new HTMLDocument();
(this.HtmlDocument as IHTMLDocument2).write(htmlContent);
}
private string GetHtmlElementValueById(string elementId)
=> this.HtmlDocument.getElementById(elementId).innerText;
}
VB.NET
Partial Class MainWindow
Inherits Window
Private Property HtmlDocument As mshtml.HTMLDocument
Private Async Function SetTextBoxTextAsync(ByVal filePath As String, ByVal id As String) As Task
Await UpdateHtmlDocumentAsync(filePath)
Dim value As String = GetHtmlElementValueById(id)
Me.TextBlock.Text = value
End Function
Public Async Function UpdateHtmlDocumentAsync(ByVal filePath As String) As Task
Dim htmlContent As String = Await File.ReadAllLinesAsync(filePath)
Me.HtmlDocument = New HTMLDocument()
(TryCast(Me.HtmlDocument, IHTMLDocument2)).write(htmlContent)
End Function
Public Function GetHtmlElementValueById(ByVal elementId As String) As String
Return Me.HtmlDocument.getElementById(elementId).innerText
End Function
End Class
备注
如果您对呈现内容感兴趣,请使用 WebBrowser
控件。它有一个 Document
属性,您可以将其转换为 mshtml.HTMLDocument
。由于 WebBrowser
是一个重实例,因此只有在打算渲染 HTML 时才应使用它。否则如上所示手动构建 mshtml.HTMLDocument
。
我想从我的 HTML 页面读取文本并将其显示到我的 WPF 应用程序中的 Label 或 TextBlock 中。我该怎么做?
使用 Windows 表格我是这样做的:
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
WebBrowser1.Navigate("C:\Users\Test\Desktop\Webseite\test.htm")
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim ImageInhalt As String
Label1.Text = WebBrowser1.Document.GetElementById("mytext").GetAttribute("value")
ImageInhalt = WebBrowser1.Document.GetElementById("mytext2").GetAttribute("value")
If ImageInhalt.Contains("STOP") Then
PictureBox1.Image = My.Resources._stop
End If
End Sub
End Class
HTML 页 (test.htm)
<input type="text" name="userInfoNav" value="Hallo"/><br/>"
现在非常感谢您的帮助
您必须将 URL 或文件的 HTML 内容加载到 mshtml.HTMLDocument
中。然后,您可以使用类似 JavaScript 的语法处理 DOM 对象。
需要参考 Microsoft.mshtml.dll。
C#
partial class MainWindow : Window
{
private mshtml.HTMLDocument HtmlDocument { get; set; }
public async Task SetTextBoxTextAsync(string filePath, string id)
{
await UpdateHtmlDocumentAsync(filePath);
string value = GetHtmlElementValueById(id);
this.TextBlock.Text = value;
}
private async Task UpdateHtmlDocumentAsync(string filePath)
{
string htmlContent = await File.ReadAllLinesAsync(filePath);
this.HtmlDocument = new HTMLDocument();
(this.HtmlDocument as IHTMLDocument2).write(htmlContent);
}
private string GetHtmlElementValueById(string elementId)
=> this.HtmlDocument.getElementById(elementId).innerText;
}
VB.NET
Partial Class MainWindow
Inherits Window
Private Property HtmlDocument As mshtml.HTMLDocument
Private Async Function SetTextBoxTextAsync(ByVal filePath As String, ByVal id As String) As Task
Await UpdateHtmlDocumentAsync(filePath)
Dim value As String = GetHtmlElementValueById(id)
Me.TextBlock.Text = value
End Function
Public Async Function UpdateHtmlDocumentAsync(ByVal filePath As String) As Task
Dim htmlContent As String = Await File.ReadAllLinesAsync(filePath)
Me.HtmlDocument = New HTMLDocument()
(TryCast(Me.HtmlDocument, IHTMLDocument2)).write(htmlContent)
End Function
Public Function GetHtmlElementValueById(ByVal elementId As String) As String
Return Me.HtmlDocument.getElementById(elementId).innerText
End Function
End Class
备注
如果您对呈现内容感兴趣,请使用 WebBrowser
控件。它有一个 Document
属性,您可以将其转换为 mshtml.HTMLDocument
。由于 WebBrowser
是一个重实例,因此只有在打算渲染 HTML 时才应使用它。否则如上所示手动构建 mshtml.HTMLDocument
。