【问题标题】:Chromium Edge-based WebView2 does not work基于 Chromium Edge 的 WebView2 不起作用
【发布时间】:2020-01-13 06:59:35
【问题描述】:

我已按照 Getting Started with WebView2 (developer preview) 的所有说明创建使用 Microsoft Edge (Chromium) 的应用程序。但是,它无法找到 Edge 浏览器。我还尝试了示例应用程序(thisthis),但结果相同。我已经为 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 位)
  • Windows 10.0.17134
  • Visual Studio 2019 16.4.2

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

【问题讨论】:

  • WebView2窗口是否显示?您使用的是哪个版本的 WebView2 SDK?我尝试过 this sample 并使用 Microsoft.Windows.ImplementationLibrary v1.0.191107.2 版本和 Microsoft.Web.WebView2 v0.8.355,代码在我这边运行良好,您可以查看它。此外,您可以尝试通过CreateWebView2EnvironmentWithDetails 设置 browserExecutableFolder
  • 是的,我也检查过,结果相同。
  • 我尝试为第一个参数指定C:\Program Files (x86)\Microsoft\Edge Beta\Application,但出现了同样的错误。然后我使用进程监视器并注意到它正在尝试查找文件 EBWebView\x64\EmbeddedBrowserWebView.dll,因此它实际上位于c:\Program Files (x86)\Microsoft\Edge Beta\Application\79.0.309.63 下。当我指定时,它就起作用了。但是,我认为这还不够好。我没有指定安装的文件夹。如果我对安装在客户处的应用程序执行此操作,我希望它自动确定其安装路径。
  • 参考this article,可以看到在使用CreateWebView2EnvironmentWithDetails方法时,如果browserExecutableFolder为null或空字符串创建WebView,API会尝试寻找机器上安装的Edge兼容版本根据频道偏好,首先尝试查找每个用户安装,然后查找每个机器安装。它会自动检测 Edge(铬)浏览器,在我这边,使用默认设置安装 Microsoft Edge,它会自动检测浏览器。

标签: c++ windows browser webview microsoft-edge


【解决方案1】:

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

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

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

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

【讨论】:

  • 是的,您应该在 Nuget 上下载 Edge Canary 版本以及 WebView2 的预发布版本。
【解决方案2】:

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

另外,你需要升级 NuGet 包,可能在你的代码中你仍然有“预发布版本”的引用。 在那个版本中,缺少“WebView2Environment”,它是一种“WebView2Experiment”。

【讨论】:

    【解决方案3】:

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

    【讨论】:

      【解决方案4】:
      • 将 Edge 更新到最新版本。
      • 将 Nuget WebView2 SDK 更新到最新版本(包括预发布版)

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2019-09-28
        • 1970-01-01
        • 2023-03-09
        • 1970-01-01
        • 1970-01-01
        • 2021-06-23
        • 1970-01-01
        • 2020-09-21
        相关资源
        最近更新 更多