如何在基于 MFC 对话框的 webviewWindow 组件初始化之外使用"webviewWindow->Navigate()"?



标题说明了一切。我承认我是MFC和Webview2的新手,但由于";入门;Webview2。但我想通过函数调用将代码导航到另一个url来增加这一点。这是我的:

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
}

当我评论时,所有这些都运行良好

webviewWindow->Navigate(L"http://www.google.com"); 

但问题是,当我试图导航到区块外的谷歌时;webviewController";定义。错误如下:

Exception thrown: read access violation.
webviewWindow.**m_ptr** was nullptr.

我看了文件,四处寻找指南,但一无所获。

错误诊断指示您试图取消引用空指针。由于它方便地同时包含对象名称和原因,因此很明显,您正试图在分配webviewWindow之前访问它。

webviewWindow被分配到这行代码中:

webviewController->get_CoreWebView2(&webviewWindow);

请注意,此代码位于作为ICoreWebView2CreateCoreWebView2ControllerCompletedHandler回调传递的匿名函数对象内部。该代码似乎是异步执行的,因此在创建WebView2控件完成之前,可能会实际执行在创建代码后面的代码。

这样一来,最早可以安全地与WebView2控件交互的时间是从其ICoreWebView2CreateCoreWebView2ControllerCompletedHandler开始的。如果你想导航到任何特定的URL,你必须将代码放入这个回调中。如果有问题的代码在非静态类成员中运行,则可以捕获this并调用该实例上的任何给定类成员。

最新更新