Reputation: 345
I am using Visual Studio 2015, version 14.0.23107.0. Project’s target platform is 10.0.17763.0.
I have read this article, and did first steps. I downloaded webview2 solution from GitHub and opened it in Visual Studio. Then, via git NuGet I successfully added Windows Implementation Library package (version 1.0.200902.2), and WebView2 package (version 0.9.579).
Then, in HelloWebView.cpp
I added
#include "WebView2.h"
At the start I couldn’t compile it. There was no errors in HelloWebView.cpp
and in WebView2.h
. The problem was in Windows Implementation Library.
Asking on Russian stack overflow I got the solution - do not include Windows Implementation Library, but ATL instead, and
static wil::com_ptr<ICoreWebView2Controller> webviewController;
// Pointer to WebView window
static wil::com_ptr<ICoreWebView2> webviewWindow;
Replace with
static CComPtr<ICoreWebView2Controller> webviewController;
// Pointer to WebView window
static CComPtr <ICoreWebView2> webviewWindow;
And remove using::Microsoft
;
After that, project compiles, but link fails - LINK1158 error, can’t run rc.exe
. The solution was here (need to copy rc.exe and rcdll.dll to some folder).
Now it compiles and works.
So, I come to next step from the article and add code to create webview and open url, but again can’t compile it. There is only one problem - missed Callback
template class from Windows Implementation Library.
So the only thing I need to do, to completely solve the problem is to provide my own template Callback
class. But how to do that?
Current critical section (Callback is marked as an error)
CreateCoreWebView2EnvironmentWithOptions(nullptr, 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());
Callback
declaration from Windows Implementation Library:
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));
}
Source code: https://github.com/MicrosoftEdge/WebView2Samples/tree/master/GettingStartedGuides/Win32_GettingStarted.
I don’t know, I don’t really a fan of c++11,14 features I didn’t work a lot with lambdas and I also (bet not only I) don’t like stuff like templates.
Maybe just to call code after?
Upvotes: 0
Views: 1310
Reputation: 345
Seems hard nights do their work.
I included only WRL
:
#include <wrl.h>
And then used:
Microsoft::WRL::Callback<ICoreWebView2CreateCoreWebView2EnvironmentCompletedHandler>(...
For each appearance and it compiled.
But it again was a blank window😑
This answer helped me, and now I finally got working solution I need.
Upvotes: 2