C# 如何从Microsoft.Toolkit WebView获取HTML?
我使用的是Microsoft.Toolkit.Forms.UI.Controls.WebView,可从 我已将WebView添加到我的WinForms应用程序-设计器代码中:C# 如何从Microsoft.Toolkit WebView获取HTML?,c#,winforms,C#,Winforms,我使用的是Microsoft.Toolkit.Forms.UI.Controls.WebView,可从 我已将WebView添加到我的WinForms应用程序-设计器代码中: namespace Testing { partial class TestForm { /// <summary> /// Required designer variable. /// </summary> priv
namespace Testing
{
partial class TestForm
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.pnlWebPage = new System.Windows.Forms.Panel();
this.webView = new Microsoft.Toolkit.Forms.UI.Controls.WebView();
this.pnlWebPage.SuspendLayout();
((System.ComponentModel.ISupportInitialize)(this.webView)).BeginInit();
this.SuspendLayout();
//
// pnlWebPage
//
this.pnlWebPage.Controls.Add(this.webView);
this.pnlWebPage.Dock = System.Windows.Forms.DockStyle.Fill;
this.pnlWebPage.Location = new System.Drawing.Point(0, 101);
this.pnlWebPage.Margin = new System.Windows.Forms.Padding(4);
this.pnlWebPage.Name = "pnlWebPage";
this.pnlWebPage.Size = new System.Drawing.Size(1205, 624);
this.pnlWebPage.TabIndex = 3;
//
// webView
//
this.webView.Dock = System.Windows.Forms.DockStyle.Fill;
this.webView.Location = new System.Drawing.Point(0, 0);
this.webView.Margin = new System.Windows.Forms.Padding(4);
this.webView.MinimumSize = new System.Drawing.Size(27, 25);
this.webView.Name = "webView";
this.webView.Size = new System.Drawing.Size(1205, 624);
this.webView.Source = new System.Uri("https://www.bbc.com", System.UriKind.Absolute);
this.webView.TabIndex = 0;
this.webView.NavigationCompleted += new System.EventHandler<Microsoft.Toolkit.Win32.UI.Controls.Interop.WinRT.WebViewControlNavigationCompletedEventArgs>(this.webView_NavigationCompleted);
//
// TestForm
//
this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 16F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(1505, 725);
this.Controls.Add(this.pnlWebPage);
this.Margin = new System.Windows.Forms.Padding(4);
this.Name = "TestForm";
this.Text = "TestForm";
this.pnlWebPage.ResumeLayout(false);
((System.ComponentModel.ISupportInitialize)(this.webView)).EndInit();
this.ResumeLayout(false);
}
#endregion
private System.Windows.Forms.Panel pnlWebPage;
private Microsoft.Toolkit.Forms.UI.Controls.WebView webView;
}
}
但是,我看不到访问HTML的方法-似乎没有它的属性或类似属性。在研究如何做到这一点时,我只能找到与Android或Xamarin相关的问题和答案,而这些问题和答案中没有一个适用于Microsoft Toolkit WebView的解决方案。有人知道我如何做到这一点吗?您可以通过调用
InvokeScriptAsync
(或InvokeScript
)来调用JavaScripteval()
函数,以获得outerHTML
(或innerHTML
或任何您需要的内容),如下所示:
private async void wvLI_NavigationCompleted(object sender, Microsoft.Toolkit.Win32.UI.Controls.Interop.WinRT.WebViewControlNavigationCompletedEventArgs e)
{
string html = await webView.InvokeScriptAsync("eval", new string[] { "document.documentElement.outerHTML;" });
}
重要的是,这会出现在NavigationCompleted
事件中,而不是,比如说,DOMContentLoaded
,因为页面只有在NavigationCompleted
触发时才会完全加载?
private async void wvLI_NavigationCompleted(object sender, Microsoft.Toolkit.Win32.UI.Controls.Interop.WinRT.WebViewControlNavigationCompletedEventArgs e)
{
string html = await webView.InvokeScriptAsync("eval", new string[] { "document.documentElement.outerHTML;" });
}