我成功地在我的 VB.net (Visual Studio 2017) 项目中的 WebView2 上显示了一个网站,但无法获取 html 源代码。请告诉我如何获取 html 代码。
我的代码:
Private Sub testbtn_Click(sender As Object, e As EventArgs) Handles testbtn.Click
WebView2.CoreWebView2.Navigate("https://www.microsoft.com/")
End Sub
Private Sub WebView2_NavigationCompleted(sender As Object, e As CoreWebView2NavigationCompletedEventArgs) Handles WebView2.NavigationCompleted
Dim html As String = ?????
End Sub
Run Code Online (Sandbox Code Playgroud)
确实提前感谢您的建议。
如何覆盖右键单击 WebView2 控件时出现的 ContextMenu?
当您右键单击 WebView2 控件时,会出现带有“刷新”、“另存为”等选项的标准上下文菜单。
如何让我自己的 ContextMenuStrip 出现而不是在鼠标右键单击期间出现?
我正在编写这个官方教程:在 WPF (Preview) 中使用 WebView2 入门。
Microsoft Edge (Chromium) Canary channel
在我的Windows 10 pro -ver 1903
..NET Core 3.1 - WPF
在最新版本上创建了一个项目VS2019 - ver16.6.3
。但是,根据教程第 3 步中的说明,当我xmlns:wv2="clr-namespace:Microsoft.Web.WebView2.Wpf;assembly=Microsoft.Web.WebView2.Wpf"
在<Window/>
标记中添加命名空间MainWindow.xaml
并构建 (F5) 应用程序时,我using Microsoft.Web.WebView2.Wpf;
在MainWindow.g.cs
文件行收到以下错误:
错误:
错误 CS0234 命名空间“Microsoft”中不存在类型或命名空间名称“Web”(您是否缺少程序集引用?)
主窗口.xaml:
<Window x:Class="WpfWebView2TEST.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:wv2="clr-namespace:Microsoft.Web.WebView2.Wpf;assembly=Microsoft.Web.WebView2.Wpf"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Grid>
</Grid>
</Window>
Run Code Online (Sandbox Code Playgroud)
问题:错误的原因可能是什么,如何解决?
给定
http://0.0.0.0:5000
并https://0.0.0.0:6000
index.html
https://127.0.0.1:6000/index.html
结果
http://127.0.0.1:5000/index.html
一切正常https://127.0.0.1:6000/index.html
我会收到有关不受信任的证书的错误问题
不应触及 Windows 设置,例如将“localhost”证书标记为“msmc”中受信任的证书或生成自签名证书,因为此 WPF 应用程序应该在不同的计算机上运行。
换句话说,一定有比本文描述的更简单的方法。
红隼
公共类Web服务器 { 公共静态任务运行() { var 配置 = new ConfigurationBuilder().Build(); var url = 新[] { “http://0.0.0.0:7000”, “https://0.0.0.0:8000” }; var 环境 = WebHost .CreateDefaultBuilder(新字符串[0]) .UseConfiguration(配置) .UseUrls(url) .UseContentRoot(Directory.GetCurrentDirectory()) .UseIISIntegration() .UseStartup<WebStartup>(); 返回环境.Build().RunAsync(); } } 公共类 WebStartup { 公共 IConfiguration 配置 …
我试图理解 WebView2 中的架构。使用 WebBrowser,我曾经从 GetElementById 返回的结果中获取属性,如下所示:Document.GetElementById("DropDownList").GetAttribute("selectedIndex")
我知道ExecuteScriptAsync
WebView2 中的 可以运行 JavaScript 并以字符串形式返回结果。但是,它看起来不知道如何从元素获取属性。下面的代码返回一个空值。尽管如此, getElementById 返回正确的结果。
ExecuteScriptAsync("document.getElementById('DropDownList').getAttribute('selectedIndex')")
我的语法不正确吗?如何获取WebView2中的属性?我们是否必须在脚本中编写一个函数并从主机调用它?
谢谢
需要通过WebView2获取下载进度。https://learn.microsoft.com/en-us/dotnet/api/microsoft.web.webview2.core.corewebview2downloadoperation?view=webview2-dotnet-1.0.865-prerelease
async void InitializeAsync()
{
var env = await CoreWebView2Environment.CreateAsync(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData) + @"\Microsoft\EdgeCore\92.0.887.0");
await webView.EnsureCoreWebView2Async(env);
webView.CoreWebView2.DownloadStarting += webView_DownloadStarting;
webView.CoreWebView2.Navigate("downloadURL");
}
private void webView_DownloadStarting(object sender, CoreWebView2DownloadStartingEventArgs e)
{
e.ResultFilePath = filename;
public event EventHandler<object> BytesReceivedChanged; <-- Need this event for print bytes to webView_BytesReceivedChanged
public event EventHandler<object> StateChanged; <-- Need this event for print download state to webView_StateChanged
}
private void webView_BytesReceivedChanged(object sender, CoreWebView2DownloadStartingEventArgs e)
{
Console.WriteLine(e.DownloadOperation.BytesReceived); // Bytes received
Console.WriteLine(e.DownloadOperation.TotalBytesToReceive); // Total bytes to receive
}
private void webView_StateChanged(object sender, …
Run Code Online (Sandbox Code Playgroud) 今天,我决定将一个 VB.net 应用程序中的WebView
控件迁移到另一个控件WebView2
。
我已经安装WebView2
并NuGet
更改了声明和初始化行(我的 VB 程序中只有 4 行,因为我使用 2 个 WebView2)。
当我构建我的应用程序时,Visual Studio 2019
表明该ScriptNotify
事件不是WebView2
!
Private Sub wvSelect_ScriptNotify(
sender As Object,
e As WebViewControlScriptNotifyEventArgs) Handles wvSelect.ScriptNotify
Run Code Online (Sandbox Code Playgroud)
使用旧WebView
控件,该事件是使用windows.external.notify
Javascript 函数生成的。
function clickPlus(e) { window.external.notify("+PLUS:"); }
Run Code Online (Sandbox Code Playgroud)
哪个事件正在取代控制ScriptNotify
中的事件WebView2
?
我有兴趣了解新事件以及如何从 Javascript 调用该事件。
我想为 Webview2 发出的所有请求设置自定义标头。请帮忙。基本上我想在 webview 本身中加载网站,因此我需要在所有请求上收到我的标头。
MainWindow.xaml.cs
using Microsoft.Web.WebView2.Core;
using System;
using System.Windows;
namespace O2C
{
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void webView_CoreWebView2InitializationCompleted(object sender, Microsoft.Web.WebView2.Core.CoreWebView2InitializationCompletedEventArgs e)
{
webView.CoreWebView2.Settings.UserAgent = "O2C-Web";
webView.CoreWebView2.Settings.AreDefaultContextMenusEnabled = false;
webView.CoreWebView2.Settings.AreBrowserAcceleratorKeysEnabled = false;
}
private void WebView_NavigationStarting(object sender, CoreWebView2NavigationStartingEventArgs e)
{
e.RequestHeaders.SetHeader("X-Authorization", "My Auth");
}
}
}
Run Code Online (Sandbox Code Playgroud)
e.RequestHeaders.SetHeader("X-Authorization", "My Auth");
Run Code Online (Sandbox Code Playgroud)
我找到了适合要求的正确事件,但我不知道如何从 xaml 文件或 .cs 文件调用该事件。以下文档表示此事件将满足要求。
但我不知道如何挂钩该事件以及从哪里进行。
我正在使用 WebView2 制作 WPF 应用程序。
\n将有一个安装程序将 WPF 应用程序安装在文件夹中,还将下载网站并将其写入安装目录的子文件夹中。比如这样:
\nInstallation Directory\n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80Website\n\xe2\x94\x82 \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80index.css\n\xe2\x94\x82 \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80index.html\n\xe2\x94\x94\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80WPF Self Contained EXE\n
Run Code Online (Sandbox Code Playgroud)\nWebView2 将使用此加载网站(我认为):webView.CoreWebView2.Navigate(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Website");
这应该加载index.html
及其引用的所有文件,例如index.css
.
现在我主要关心的是如何从 C# 调用 JavaScript 函数。到目前为止,在谷歌搜索后我只找到了 WebView1 的方法。我找不到任何有关从 JavaScript 调用 C# 方法的信息。
\n所以三件事:
\nwebView.CoreWebView2.Navigate(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Website");
从本地文件夹加载网站是否正确?这可能吗?
\n谢谢。
\n我工作的公司开发了一个应用程序,该应用程序使用 WebView2 在应用程序中显示 Edge 浏览器。我们使用的 IDE (Delphi) 提供了一个名为“WebView2Loader.dll”的 dll 与 exe 一起分发。
在查看 WebView2 运行时如何运行时,有很多提到“Evergreen”的东西,它似乎是自动更新到最新和最好版本的 WebView2 运行时。 https://learn.microsoft.com/en-us/microsoft-edge/webview2/concepts/distribution#understand-the-webview2-runtime-and-installer-preview
这就是令人困惑的地方。我的问题是这个 Evergreen 与 WebView2Loader.dll 有何关系?我认为它的工作方式是,加载器 dll 只是用来查找已安装的 WebView2 运行时,而 Evergreen 实际上正在下载运行时。我从这句话中得到了这个指示“WebView2Loader.dll 是一个小组件,可以帮助应用程序在设备上定位 WebView2 运行时或 Microsoft Edge 的不稳定通道。” 通过此链接https://learn.microsoft.com/en-us/microsoft-edge/webview2/how-to/static。
然后我的后续问题是,客户是否必须手动下载 Evergreen,或者如果他们已经安装了 Edge,那么该运行时是否存在?