WebView Environment Options
Overview
The WebView environment options allow you to customize the underlying browser engine before it's initialized. This is essential for configuring browser-specific settings like developer tools, private browsing, user data directories, and other platform-specific features that must be set during creation.
The EnvironmentRequested
event is fired before the WebView adapter is created, giving you the opportunity to modify these settings based on your application's requirements.
Basic Usage
var webView = new WebView();
webView.EnvironmentRequested += (sender, args) =>
{
// Enable developer tools for all platforms
args.EnableDevTools = true;
// Platform-specific configuration
switch (args)
{
case WindowsWebView2EnvironmentRequestedEventArgs webView2Args:
webView2Args.IsInPrivateModeEnabled = true;
break;
case AppleWKWebViewEnvironmentRequestedEventArgs appleArgs:
appleArgs.NonPersistentDataStore = true;
break;
case GtkWebViewEnvironmentRequestedEventArgs gtkArgs:
gtkArgs.EphemeralDataManager = true;
break;
}
};
Base Class Properties
WebViewEnvironmentRequestedEventArgs
Properties:
EnableDevTools
(bool): Controls whether users can open DevTools via context menu or keyboard shortcuts. Available on all platforms.
Platform-Specific Options
Windows WebView2
Key Properties:
ExplicitEnvironment
: Use an existing ICoreWebView2Environment COM handleProfileName
: Set a custom browser profile nameBrowserExecutableFolder
: Specify Edge browser executable locationUserDataFolder
: Define where user data is storedAdditionalBrowserArguments
: Pass custom Chromium command-line flagsLanguage
: Set browser UI language (BCP 47 format)IsInPrivateModeEnabled
: Enable private browsing mode
Example:
webView.EnvironmentRequested += (sender, args) =>
{
if (args is WindowsWebView2EnvironmentRequestedEventArgs webView2)
{
webView2.ProfileName = "AvaloniaUser";
webView2.UserDataFolder = Path.Combine(AppContext.BaseDirectory, "webview");
}
};
macOS/iOS (WKWebView)
Key Properties:
NonPersistentDataStore
: Use memory-only data storageDataStoreIdentifier
: Set unique identifier for persistent dataApplicationNameForUserAgent
: Customize user agent application nameUpgradeKnownHostsToHTTPS
: Automatically upgrade HTTP to HTTPSLimitsNavigationsToAppBoundDomains
: Restrict navigation to app domains
Example:
webView.EnvironmentRequested += (sender, args) =>
{
if (args is AppleWKWebViewEnvironmentRequestedEventArgs wkWebView)
{
wkWebView.NonPersistentDataStore = true;
wkWebView.ApplicationNameForUserAgent = "Avalonia WebView Sample";
}
};
Linux (GTK WebKit)
Key Properties:
ApplicationNameForUserAgent
: Customize user agent application nameExperimentalOffscreen
: Enable experimental offscreen renderingEphemeralDataManager
: Use non-persistent data storageBaseDataDirectory
: Set base directory for website dataBaseCacheDirectory
: Set base directory for cacheSharedProcessModel
: Use shared process for all WebView instancesDisableCache
: Completely disable caching for memory optimization
Example:
webView.EnvironmentRequested += (sender, args) =>
{
if (args is GtkWebViewEnvironmentRequestedEventArgs gtkArgs)
{
gtkArgs.EphemeralDataManager = true;
gtkArgs.EnableDevTools = true;
}
};