基于 Chromium Edge 的 WebView2 不起作用

Chromium Edge-based WebView2 does not work

本文关键字:WebView2 不起作用 Edge Chromium 基于      更新时间:2023-10-16

我已经按照 WebView2 入门(开发人员预览版(中的所有说明创建了一个使用 Microsoft Edge (Chromium( 的应用程序。但是,它无法找到 Edge 浏览器。我也尝试了示例应用程序(这个和这个(,但结果相同。我已经为 32 位和 64 位构建了应用程序。

我从调用CreateWebView2EnvironmentWithDetails()中得到的是错误0x80070002,这是ERROR_FILE_NOT_FOUND(系统找不到指定的文件。

HRESULT hr = CreateWebView2EnvironmentWithDetails(nullptr, nullptr, nullptr,
Callback<IWebView2CreateWebView2EnvironmentCompletedHandler>(
[hWnd](HRESULT result, IWebView2Environment* env) -> HRESULT {
// Create a WebView, whose parent is the main window hWnd
env->CreateWebView(hWnd, Callback<IWebView2CreateWebViewCompletedHandler>(
[hWnd](HRESULT result, IWebView2WebView* webview) -> HRESULT {
if (webview != nullptr) {
webviewWindow = webview;
}
// Resize WebView to fit the bounds of the parent window
RECT bounds;
GetClientRect(hWnd, &bounds);
webviewWindow->put_Bounds(bounds);
// Schedule an async task to navigate to Bing
webviewWindow->Navigate(L"https://www.bing.com/");
return S_OK;
}).Get());
return S_OK;
}).Get());
if (!SUCCEEDED(hr))
{
if (hr == HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND))
{
MessageBox(
nullptr,
L"Couldn't find Edge installation. "
"Do you have a version installed that's compatible with this "
"WebView2 SDK version?",
nullptr, MB_OK);
}
else
{
std::wstringstream formattedMessage;
formattedMessage << L"Failed to create webview environment" 
<< ": 0x" << std::hex << std::setw(8) << hr;
MessageBox(nullptr, formattedMessage.str().c_str(), nullptr, MB_OK);
}
}

我有:

  • Edge 版本 79.0.309.60(官方内部版本(测试版(64 位(
  • 视窗 10.0.17134
  • Visual Studio 2019 16.4.2

知道为什么找不到我的 Edge 安装吗?

浏览器的版本可能与最新版本的SDK不兼容,您可能需要返回某些版本才能正常工作,如下列表:

https://learn.microsoft.com/en-us/microsoft-edge/hosting/webview2/releasenotes

编辑: 正如WebView2的一位开发人员所告知的那样,目前WebView2仍处于预览版本中,因此最新版本的Webview2将始终伴随着最新的Edge金丝雀版本。

https://github.com/MicrosoftEdge/WebViewFeedback/issues/103#issuecomment-575287157

我遇到了同样的错误,安装 WebView2 运行时后,它可以工作。 https://developer.microsoft.com/microsoft-edge/webview2

必须安装 WebView2 运行时才能运行兼容的应用程序。

此外,您需要升级 NuGet 包,也许在您的代码中您仍然有"预发布版本"的引用。 在该版本中,缺少"WebView2Environment",它有点像"WebView2Experiment"。

  • 将边缘更新到最新版本。
  • 将 Nuget WebView2 SDK 更新为最新版本(包括预发布(