[英]How do you use “webviewWindow->Navigate()” from outside the initialization of the webviewWindow component in MFC dialog-based?
Title says it all.标题说明了一切。 I'll admit that I'm new to MFC and Webview2 but I have a working program that displays the webview already thanks to the "Getting Started" sections of Webview2.我承认我是 MFC 和 Webview2 的新手,但是由于 Webview2 的“入门”部分,我已经有一个可以显示 webview 的工作程序。 But I wanted to add onto this by having code navigate to a different url by a function call.但是我想通过让代码通过函数调用导航到不同的 url 来添加它。 Here's what I have:这是我所拥有的:
BOOL CMFCApplication1Dlg::OnInitDialog(){
CDialogEx::OnInitDialog();
// Add "About..." menu item to system menu.
// IDM_ABOUTBOX must be in the system command range.
ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
ASSERT(IDM_ABOUTBOX < 0xF000);
CMenu* pSysMenu = GetSystemMenu(FALSE);
if (pSysMenu != nullptr)
{
BOOL bNameValid;
CString strAboutMenu;
bNameValid = strAboutMenu.LoadString(IDS_ABOUTBOX);
ASSERT(bNameValid);
if (!strAboutMenu.IsEmpty())
{
pSysMenu->AppendMenu(MF_SEPARATOR);
pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
}
}
// Set the icon for this dialog. The framework does this automatically
// when the application's main window is not a dialog
SetIcon(m_hIcon, TRUE); // Set big icon
SetIcon(m_hIcon, FALSE); // Set small icon
// TODO: Add extra initialization here
// this is where the WebView2 "Getting Started" code begins
HRESULT hresult = CoInitializeEx(NULL, COINIT_APARTMENTTHREADED | COINIT_DISABLE_OLE1DDE);
// The main window class name.
static TCHAR szWindowClass[] = _T("DesktopApp");
// The string that appears in the application's title bar.
static TCHAR szTitle[] = _T("WebView sample");
// Pointer to WebViewController
static wil::com_ptr<ICoreWebView2Controller> webviewController;
// Pointer to WebView window
static wil::com_ptr<ICoreWebView2> webviewWindow;
HWND hWnd = GetSafeHwnd();
try {
// Locate the browser and set up the environment for WebView
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 = { 0, 0, 1920, 1080 };
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());
webviewWindow->Navigate(L"http://www.google.com");
}
catch (...) {
MessageBoxW(L"can i bypass this null pointer? probably not", 0, MB_OK);
}
return TRUE; // return TRUE unless you set the focus to a control
}
All of this runs well when I comment当我发表评论时,所有这些都运行良好
webviewWindow->Navigate(L"http://www.google.com");
But the problem is when I try to navigate to google outside of the block "webviewController" is defined.但问题是当我尝试在定义块“webviewController”之外导航到谷歌时。 Here's the Error:这是错误:
Exception thrown: read access violation.
webviewWindow.**m_ptr** was nullptr.
I've look at the documentation and looked around for guides, but I've gotten nothing.我查看了文档并四处寻找指南,但一无所获。
The error diagnostic indicates, that you're trying to dereference a null pointer.错误诊断表明您正在尝试取消引用空指针。 Since it conveniently contains both the object name and a reason, it's clear that you're trying to access webviewWindow
before it has been assigned.由于它方便地包含对象名称和原因,很明显您正在尝试在分配之前访问webviewWindow
。
webviewWindow
is assigned to in this line of code:在这行代码webviewWindow
分配给:
webviewController->get_CoreWebView2(&webviewWindow);
Pay attention that this code is inside an anonymous function object passed as the ICoreWebView2CreateCoreWebView2ControllerCompletedHandler
callback.请注意,此代码位于作为ICoreWebView2CreateCoreWebView2ControllerCompletedHandler
回调传递的匿名函数对象中。 The code appears to be executed asynchronously, leaving the possibility that code that's visually trailing the creation code is actually executed prior to creation of the WebView2 control completing.该代码似乎是异步执行的,因此可能会在 WebView2 控件的创建完成之前实际执行在视觉上落后于创建代码的代码。
With that out of the way, the earliest time when it's safe to interact with the WebView2 control is from its ICoreWebView2CreateCoreWebView2ControllerCompletedHandler
.顺便说一下,与 WebView2 控件交互的最早时间是从它的ICoreWebView2CreateCoreWebView2ControllerCompletedHandler
。 If you want to navigate to any particular URL, you're going to have to place that code into this callback.如果要导航到任何特定 URL,则必须将该代码放入此回调中。 If the code in question is running inside a non-static class member you can capture this
and call any given class member on that instance.如果有问题的代码在非静态类成员中运行,您可以捕获this
并调用该实例上的任何给定类成员。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.