NativeWebDialog
Overview
NativeWebDialog
is a dialog window that hosts a native web browser implementation. It provides a way to display web content in a separate window, particularly useful for platforms like Linux where embedded WebView controls might not be available.
Properties
Title
public string? Title { get; set; }
Gets or sets the dialog window title.
CanUserResize
public bool CanUserResize { get; set; }
Gets or sets whether the dialog can be resized by the user.
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
Closing
public event EventHandler Closing;
Fires before the WebView dialog is closed.
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.
WebMessageReceived
public event EventHandler<WebMessageReceivedEventArgs>? WebMessageReceived;
Fires after web content sends a message to the app host via invokeCSharpAction(body)
.
Methods
Show
public void Show()
Opens the WebView dialog.
Show (with owner)
public bool Show(IPlatformHandle owner)
Opens the WebView dialog with a specified owner window.
Close
public void Close()
Closes the WebView dialog.
Resize
public bool Resize(int width, int height)
Resizes the dialog window to the specified dimensions.
Move
public bool Move(int x, int y)
Moves the dialog window to the specified screen coordinates.
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.
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.
TryGetCookieManager
public NativeWebViewCookieManager? TryGetCookieManager()
Returns an instance of NativeWebViewCookieManager
for managing cookies if supported by the platform.
TryGetWebViewPlatformHandle
public IPlatformHandle? TryGetWebViewPlatformHandle()
Gets platform handle of the webview hosted inside the dialog
TryGetPlatformHandle
public IPlatformHandle? TryGetPlatformHandle()
Returns a platform handle of the dialog window itself.
Usage Example
var dialog = new NativeWebDialog
{
Title = "Avalonia Docs",
CanUserResize = false,
Source = new Uri("https://docs.avaloniaui.net/")
};
// Create TaskCompletionSource so we can wait until window is closed.
var tcs = new TaskCompletionSource();
dialog.Closing += (s, e) => tcs.SetResult();
// Show the dialog
dialog.Show(mainWindow);
await tcs.Task;
Platform Support
Feature | Windows | macOS | Linux | iOS | Android | Browser |
---|---|---|---|---|---|---|
Show | ✔ | ✔ | ✔ | ✖ | ✖ | ✖ |
Show(Window) | ✔ | ✔ | ✔* | ✖ | ✖ | ✖ |
WebMessageReceived | ✔ | ✔ | ✖ | ✖ | ✖ | ✖ |
* Linux support may vary depending on the window manager