![](/img/trans.png)
[英]Microsoft.Toolkit.Uwp.UI.DataGrid - how to get cell value from button click on the cell
[英]How can I get the HTML from a Microsoft.Toolkit WebView?
I am using Microsoft.Toolkit.Forms.UI.Controls.WebView
, available from https://www.nuget.org/packages/Microsoft.Toolkit.Forms.UI.Controls.WebView
我已将 WebView 添加到我的 WinForms 应用程序中 - 设计器代码:
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;
}
}
一旦网站加载到 WebView 中,我希望能够提取页面的 HTML 并将其存储为字符串或类似内容。
我添加了以下方法,该方法在 WebView 的NavigationCompleted
事件发生时调用。
private async void wvLI_NavigationCompleted(object sender, Microsoft.Toolkit.Win32.UI.Controls.Interop.WinRT.WebViewControlNavigationCompletedEventArgs e)
{
\\ HTML extraction to go here
}
但是,我看不到访问 HTML 的方法-似乎没有属性或类似的东西。 在研究如何执行此操作时,我只能找到与 Android 或 Xamarin 相关的问题和答案 - 这些解决方案都不适用于 Microsoft 工具包 WebView。 有谁知道我怎么能做到这一点?
您可以调用 JavaScript eval()
function 通过调用InvokeScriptAsync
(或InvokeScript
)来获取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
触发时才完全加载
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.