使用 WebView2 多配置文件 API,可以创建和操作用户配置文件以使用 WebView2 控件。 WebView2 中的配置文件在概念上类似于 Microsoft Edge 中的配置文件。 通过多配置文件支持,WebView2 应用可以在单个用户数据文件夹下拥有多个配置文件。
每个配置文件都有一个用于保存浏览器数据的专用配置文件文件夹,该文件夹为每个用户提供单独的浏览数据存储,例如 Cookie、用户首选项设置和缓存资源。 与同一用户配置文件关联的所有 WebView2 控件共享单个配置文件文件夹。
上一种方法:为每个 WebView2 控件使用不同的用户数据文件夹
以前,在没有多配置文件支持的情况下,为了实现数据分离,WebView2 应用可以将不同的用户数据文件夹用于不同的 WebView2 控件。 但是,在这种方法中,必须运行多个 WebView2 运行时实例, (每个实例包括一个浏览器进程和一组子进程) ,这些实例会消耗更多的系统资源,包括内存、CPU 占用量和磁盘空间。
创建 WebView2 时指定配置文件
创建定义配置文件的 options 对象
上的 CreateCoreWebView2ControllerOptions
CoreWebView2Environment
方法创建一个 options 对象 , CoreWebView2ControllerOptions
以提供有关配置文件的特定信息,包括 ProfileName
和 IsInPrivateModeEnabled
。 创建 WebView2 控件实例时,使用此对象指定目标配置文件。
创建使用配置文件的 WebView2 控件
采用options
参数的每个方法都会Create...Controller
创建一个 WebView2 控件,并将其与指定的配置文件相关联。 如果指定的配置文件不存在,将创建新的配置文件。
创建 WebView2 时指定配置文件的示例
public async void CreateWebView2Controller(object sender, RoutedEventArgs e)
{
var hwnd = new HwndForWV2();
Window window = new Window();
window.Content = hwnd;
window.Show();
var env = await CoreWebView2Environment.CreateAsync();
var options = env.CreateCoreWebView2ControllerOptions();
options.ProfileName = "MyProfile";
options.IsInPrivateModeEnabled = true;
var controller = await env.CreateCoreWebView2ControllerAsync(hwnd.Handle, options);
controller.Bounds = new System.Drawing.Rectangle(0, 0, 800, 600);
controller.CoreWebView2.Navigate("https://www.bing.com/");
controller.CoreWebView2.NavigationStarting += CoreWebView_NavigationStarting;
}
public async Task InitializeCoreWebView2()
{
CoreWindow coreWindow = Windows.UI.Core.CoreWindow.GetForCurrentThread();
CoreWebView2Environment env = await CoreWebView2Environment.CreateAsync();
CoreWebView2ControllerWindowReference windowReference = CoreWebView2ControllerWindowReference.CreateFromCoreWindow(coreWindow);
CoreWebView2ControllerOptions options = env.CreateCoreWebView2ControllerOptions();
options.ProfileName = "MyProfile";
options.IsInPrivateModeEnabled = true;
CoreWebView2Controller controller = await env.CreateCoreWebView2ControllerAsync(windowReference, options);
controller.CoreWebView2.NavigationStarting += WebView2_NavigationStarting;
controller.CoreWebView2.Navigate("https://www.bing.com");
}
HRESULT AppWindow::CreateControllerWithOptions()
{
auto webViewEnvironment10 = m_webViewEnvironment.try_query<ICoreWebView2Environment10>();
if (!webViewEnvironment10)
{
FeatureNotAvailable();
return S_OK;
}
wil::com_ptr<ICoreWebView2ControllerOptions> options;
// The validation of parameters occurs when setting the properties.
HRESULT hr = webViewEnvironment10->CreateCoreWebView2ControllerOptions(&options);
if (hr == E_INVALIDARG)
{
ShowFailure(hr, L"Unable to create WebView2 due to an invalid profile name.");
CloseAppWindow();
return S_OK;
}
CHECK_FAILURE(hr);
// If call 'put_ProfileName' with an invalid profile name, the 'E_INVALIDARG' returned
// immediately. ProfileName could be reused.
CHECK_FAILURE(options->put_ProfileName(m_webviewOption.profile.c_str()));
CHECK_FAILURE(options->put_IsInPrivateModeEnabled(m_webviewOption.isInPrivate));
#ifdef USE_WEBVIEW2_WIN10
if (m_dcompDevice || m_wincompCompositor)
#else
if (m_dcompDevice)
#endif
{
CHECK_FAILURE(webViewEnvironment10->CreateCoreWebView2CompositionControllerWithOptions(
m_mainWindow, options.get(),
Callback<ICoreWebView2CreateCoreWebView2CompositionControllerCompletedHandler>(
[this](
HRESULT result,
ICoreWebView2CompositionController* compositionController) -> HRESULT {
auto controller =
wil::com_ptr<ICoreWebView2CompositionController>(compositionController)
.query<ICoreWebView2Controller>();
return OnCreateCoreWebView2ControllerCompleted(result, controller.get());
})
.Get()));
}
else
{
CHECK_FAILURE(webViewEnvironment10->CreateCoreWebView2ControllerWithOptions(
m_mainWindow, options.get(),
Callback<ICoreWebView2CreateCoreWebView2ControllerCompletedHandler>(
this, &AppWindow::OnCreateCoreWebView2ControllerCompleted)
.Get()));
}
return S_OK;
}
访问和操作配置文件
可以通过访问 Profile
WebView2 控件的 属性来获取配置文件对象。
获取配置文件对象后,可以对其进行操作。 使用 CoreWebView2Profile
获取配置文件信息并执行配置文件范围的设置和操作。
访问和操作配置文件的示例
string profileName = controller.CoreWebView2.Profile.ProfileName;
bool inPrivate = controller.CoreWebView2.Profile.IsInPrivateModeEnabled;
// update window title with profileName
UpdateAppTitle(profileName);
// update window icon
SetAppIcon(inPrivate);
string profileName = controller.CoreWebView2.Profile.ProfileName;
bool inPrivate = controller.CoreWebView2.Profile.IsInPrivateModeEnabled;
// update window title with profileName
UpdateAppTitle(profileName);
// update window icon
SetAppIcon(inPrivate);
// This is the callback passed to CreateCoreWebView2Controller.
// Here we can get the profile property of the WebView2 control, then manipulate it.
HRESULT AppWindow::OnCreateCoreWebView2ControllerCompleted(HRESULT result, ICoreWebView2Controller* controller)
{
// ...
m_controller = controller;
wil::com_ptr<ICoreWebView2> coreWebView2;
CHECK_FAILURE(m_controller->get_CoreWebView2(&coreWebView2));
// We should check for failure here because if this app is using a newer
// SDK version compared to the install of the Edge browser, the Edge
// browser might not have support for the latest version of the
// ICoreWebView2_N interface.
coreWebView2.query_to(&m_webView);
// Save PID of the browser process serving last WebView created from our
// CoreWebView2Environment. We know the controller was created with
// S_OK, and it hasn't been closed (we haven't called Close and no
// ProcessFailed event could have been raised yet) so the PID is
// available.
CHECK_FAILURE(m_webView->get_BrowserProcessId(&m_newestBrowserPid));
auto webView2_13 = coreWebView2.try_query<ICoreWebView2_13>();
if (webView2_13)
{
wil::com_ptr<ICoreWebView2Profile> profile;
CHECK_FAILURE(webView2_13->get_Profile(&profile));
wil::unique_cotaskmem_string profile_name;
CHECK_FAILURE(profile->get_ProfileName(&profile_name));
m_profileName = profile_name.get();
BOOL inPrivate = FALSE;
CHECK_FAILURE(profile->get_IsInPrivateModeEnabled(&inPrivate));
if (!m_webviewOption.downloadPath.empty())
{
CHECK_FAILURE(profile->put_DefaultDownloadFolderPath(
m_webviewOption.downloadPath.c_str()));
}
// update window title with m_profileName
UpdateAppTitle();
// update window icon
SetAppIcon(inPrivate);
}
// ...
}
另请参阅