无法编译WebView2项目需要自己的回调类模板

问题描述

我正在使用Visual Studio 2015 14.0.23107.0版。该项目的目标平台是10.0.17763.0。

我已阅读this文章,并已进行了第一步。我从GitHub下载了webview2解决方案,并在Visual Studio中将其打开。然后,通过git NuGet,我成功添加了Windows实施库软件包(版本1.0.200902.2)和WebView2软件包(版本0.9.579)。

然后,在我添加HelloWebView.cpp

#include "WebView2.h"

一开始我无法编译它。 HelloWebView.cppWebView2.h中没有错误。问题出在Windows实施库中。

询问俄罗斯堆栈溢出I got the solution-不包括Windows实现库,而是包括ATL,并且

static wil::com_ptr<ICoreWebView2Controller> webviewController;

// Pointer to WebView window
static wil::com_ptr<ICoreWebView2> webviewWindow;

替换为

static CComPtr<ICoreWebView2Controller> webviewController;

// Pointer to WebView window
static CComPtr <ICoreWebView2> webviewWindow;

删除using::Microsoft;

此后,项目进行了编译,但是链接失败-LINK1158 error,can’t run rc.exe解决方法here(需要将rc.exe和rcdll.dll复制到某个文件夹)。

现在它可以编译和运行了。

因此,我从本文开始进行下一步,并添加代码以创建webview并打开url,但同样无法对其进行编译。仅有一个问题-Windows实施库缺少Callback模板类。

因此,要完全解决问题,我唯一需要做的就是提供自己的模板Callback类。但是该怎么做?

当前关键部分(回调标记错误

CreateCoreWebView2EnvironmentWithOptions(nullptr,nullptr,Callback<ICoreWebView2CreateCoreWebView2EnvironmentCompletedHandler>(
  [hWnd](HRESULT result,ICoreWebView2Environment* env) -> HRESULT {

// Create a CoreWebView2Controller and get the associated CoreWebView2 whose parent is the main window hWnd
env->CreateCoreWebView2Controller(hWnd,Callback<ICoreWebView2CreateCoreWebView2ControllerCompletedHandler>(
  [hWnd](HRESULT result,ICoreWebView2Controller* controller) -> HRESULT {
  if (controller != nullptr) {
    webviewController = controller;
    webviewController->get_CoreWebView2(&webviewWindow);
  }

  // Add a few settings for the webview
  // The demo step is redundant since the values are the default settings
  ICoreWebView2Settings* Settings;
  webviewWindow->get_Settings(&Settings);
  Settings->put_IsScriptEnabled(TRUE);
  Settings->put_AreDefaultScriptDialogsEnabled(TRUE);
  Settings->put_IsWebMessageEnabled(TRUE);

  // Resize WebView to fit the bounds of the parent window
  RECT bounds;
  GetClientRect(hWnd,&bounds);
  webviewController->put_Bounds(bounds);

  // Schedule an async task to navigate to Bing
  webviewWindow->Navigate(L"https://www.bing.com/");

  // Step 4 - Navigation events

  // Step 5 - Scripting

  // Step 6 - Communication between host and web content

  return S_OK;
}).Get());
return S_OK;
}).Get());
Windows实施库中的

Callback声明:

template<typename TDelegateInterface,typename TLambda>
ComPtr<typename Details::DelegateArgTraitsHelper<TDelegateInterface>::Interface> Callback(TLambda&& callback) throw()
{
using DelegateHelper = Details::DelegateArgTraitsHelper<TDelegateInterface>;
return DelegateHelper::Traits::template Callback<TDelegateInterface,typename DelegateHelper::Interface>(Details::Forward<TLambda>(callback));
}

代码https://github.com/MicrosoftEdge/WebView2Samples/tree/master/GettingStartedGuides/Win32_GettingStarted


我不知道,我不是很喜欢c ++ 11,14的功能,我在lambda上工作不多,而且(不仅是我)我也不喜欢模板之类的东西。

也许只是在之后调用代码

解决方法

似乎艰苦的夜晚正在努力。 我只包括了WRL

#include <wrl.h>

然后使用:

Microsoft::WRL::Callback<ICoreWebView2CreateCoreWebView2EnvironmentCompletedHandler>(... 

对于每个外观并进行编译。

但这又是一个空白窗口?

This answer帮助了我,现在终于有了所需的解决方案。