NativeWebView
Overview
NativeWebView
is a control that provides a native web browser implementation for Avalonia and WPF applications. It wraps platform-specific web controls and provides a unified API for web browsing functionality.
Properties
Source
public Uri Source { get; set; }
The URI of the top-level document displayed in the WebView. Setting this property is equivalent to calling Navigate()
.
Default value: about:blank
CanGoBack
public bool CanGoBack { get; }
Indicates whether the WebView can navigate to a previous page in the navigation history.
CanGoForward
public bool CanGoForward { get; }
Indicates whether the WebView can navigate to a next page in the navigation history.
Events
AdapterCreated
public event EventHandler<WebViewAdapterEventArgs>? AdapterCreated;
Fires after underlying webview adapter was initialized.
AdapterDestroyed
public event EventHandler<WebViewNavigationCompletedEventArgs>? AdapterDestroyed;
Fires after underlying webview adapter was destroyed.
EnvironmentRequested
public event EventHandler<WebViewEnvironmentRequestedEventArgs>? EnvironmentRequested;
Fired before the underlying webview adapter is created, allowing customization of the webview environment. Use this event to modify environment options (such as enabling private mode or dev tools) before the webview is initialized. The event argument type depends on the platform.
See WebView Environment Options for more details.
NavigationCompleted
public event EventHandler<WebViewNavigationCompletedEventArgs>? NavigationCompleted;
Fires after navigation of the top-level document completes rendering, either successfully or unsuccessfully.
NavigationStarted
public event EventHandler<WebViewNavigationStartingEventArgs>? NavigationStarted;
Fires before a new navigation starts for the top-level document.
NewWindowRequested
public event EventHandler<WebViewNewWindowRequestedEventArgs>? NewWindowRequested;
Fires before a new navigate starts for the top level document.
WebMessageReceived
public event EventHandler<WebMessageReceivedEventArgs>? WebMessageReceived;
Fires after web content sends a message to the app host via invokeCSharpAction(body)
.
WebResourceRequested
public event EventHandler<WebResourceRequestedEventArgs>? WebResourceRequested;
Fires when the WebView is performing a URL request to a matching URL. Arguments include request information, and headers dictionary.
Headers dictionary can be readonly depending on the request or platform.
Always check result of the TrySet
and TryRemove
methods.
Usage Example
Bi-directional JS<->C# communication example:
private async void NativeWebView_OnNavigationCompleted(object? sender, WebViewNavigationCompletedEventArgs e)
{
await ((NativeWebView)sender!).InvokeScript(""" invokeCSharpAction("{'key': 10}") """);
}
private void NativeWebView_OnWebMessageReceived(object? sender, WebMessageReceivedEventArgs e)
{
var message = e.Body;
// message == "{'key': 10}"
}
Methods
Navigate
public void Navigate(Uri url)
Navigates the WebView to the specified URI.
NavigateToString
public void NavigateToString(string text)
Renders the provided HTML string as the top-level document.
InvokeScript
public Task<string?> InvokeScript(string scriptName)
Executes the provided JavaScript in the top-level document.
Usage Example
<NativeWebView Source="https://avaloniaui.net/" NavigationCompleted="WebView_NavigationCompleted" />
private async void WebView_NavigationCompleted(object? sender, WebViewNavigationCompletedEventArgs args)
{
// Execute JavaScript
await webView.InvokeScript("alert('Hello World')");
}
GoBack
public bool GoBack()
Navigates to the previous page in navigation history. Returns false
if navigation is not possible.
GoForward
public bool GoForward()
Navigates to the next page in navigation history. Returns false
if navigation is not possible.
Refresh
public bool Refresh()
Reloads the current page.
Stop
public bool Stop()
Stops any ongoing navigation.
TryGetCommandManager
public NativeWebViewCommandManager? TryGetCommandManager()
Returns an instance of NativeWebViewCommandManager
for executing common keyboard commands if supported by the platform.
Usage Example
var commandManager = webView.TryGetCommandManager();
if (commandManager != null)
{
// Copy selected content
commandManager.Copy();
}
TryGetCookieManager
public NativeWebViewCookieManager? TryGetCookieManager()
Returns an instance of NativeWebViewCookieManager
for managing cookies if supported by the platform.
Usage Example
var cookieManager = webView.TryGetCookieManager();
if (cookieManager != null)
{
// Get all cookies
var cookies = await cookieManager.GetCookiesAsync();
}
TryGetPlatformHandle
public IPlatformHandle? TryGetPlatformHandle()
Returns a platform handle of the native control for accessing platform-specific APIs. See Native Browser interop for details.
BeginReparenting
public IDisposable BeginReparenting(bool yieldOnLayoutBeforeExiting = true)
Delays destruction of the native control during parent changes.
BeginReparentingAsync
public IAsyncDisposable BeginReparentingAsync()
Asynchronously delays destruction of the native control during parent changes.
Platform Support
Feature | Windows WebView2-Edge | Linux | macOS/iOS WKWebView | Android | Browser |
---|---|---|---|---|---|
NativeWebView | ✔ | ✖ | ✔ | ✔ | ✖* |
TryGetCommandManager | ✔ | ✖ | ✔ | ✔ | ✖* |
TryGetCookieManager | ✔ | ✖ | ✔ | ✖ | ✖* |
* Support is possible, but wasn't implemented yet. Let us know if it's a blocker for you.
For Linux support, please use NativeWebDialog