A cross-platform WebView control for Avalonia UI that provides native web rendering on Windows, macOS, and Linux.
- ✅ Cross-Platform Support: Works on Windows, macOS, and Linux
- ✅ Native Rendering Engines:
- Windows: WebView2 (Microsoft Edge Chromium) via direct COM interop
- macOS: WKWebView (WebKit)
- Linux: WebKitGTK
- ✅ Unified API: Single API across all platforms
- ✅ Pure .NET 8 Implementation: No WinForms or platform-specific SDK dependencies
- ✅ Full Navigation Control: Back, forward, reload, and stop
- ✅ JavaScript Execution: Execute JavaScript code and get results
- ✅ HTML String Support: Load HTML content directly
- ✅ Event Support: Navigation events for tracking page loads
- .NET 8.0 SDK or later
- Avalonia UI 11.x
- Microsoft Edge WebView2 Runtime
- Usually pre-installed on Windows 10/11
- Download from: https://developer.microsoft.com/en-us/microsoft-edge/webview2/
- macOS 10.13 (High Sierra) or later
- WebKit framework (included with macOS)
- WebKitGTK 4.0 or later
- Install on Ubuntu/Debian:
sudo apt-get install libwebkit2gtk-4.0-dev
- Install on Fedora/RHEL:
sudo dnf install webkit2gtk3-devel
- Install on Arch Linux:
sudo pacman -S webkit2gtk
-
Add a reference to the
GreenSwampWebViewproject:<ItemGroup> <ProjectReference Include="path/to/WebViewControl/WebViewControl.csproj" /> </ItemGroup>
-
Add the namespace to your AXAML file:
xmlns:gsw="using:GreenSwampWebView"
-
Add the WebView control to your UI:
<gsw:WebView Name="WebViewControl" Url="https://www.example.com" />
<Window xmlns="https://github.com/avaloniaui"
xmlns:gsw="using:GreenSwampWebView"
x:Class="YourApp.MainWindow">
<gsw:WebView Name="MyWebView"
Url="https://www.google.com"
NavigationStarting="WebView_NavigationStarting"
NavigationCompleted="WebView_NavigationCompleted"
NavigationFailed="WebView_NavigationFailed" />
</Window>using GreenSwampWebView;
// Navigate to a URL
webView.Navigate("https://www.example.com");
// Navigate to HTML content
webView.NavigateToString("<h1>Hello, WebView!</h1>");
// Navigation controls
webView.GoBack();
webView.GoForward();
webView.Reload();
webView.Stop();
// Check navigation state
bool canGoBack = webView.CanGoBack;
bool canGoForward = webView.CanGoForward;
// Execute JavaScript
string result = await webView.ExecuteScriptAsync("document.title");private void WebView_NavigationStarting(object? sender, WebViewNavigationEventArgs e)
{
Console.WriteLine($"Starting navigation to: {e.Uri}");
}
private void WebView_NavigationCompleted(object? sender, WebViewNavigationEventArgs e)
{
if (e.IsSuccess)
{
Console.WriteLine($"Successfully loaded: {e.Uri}");
}
}
private void WebView_NavigationFailed(object? sender, WebViewNavigationEventArgs e)
{
Console.WriteLine($"Navigation failed: {e.ErrorMessage}");
}string? Url- Gets or sets the URL to navigate tostring? Html- Gets or sets the HTML content to displaybool CanGoBack- Gets whether the WebView can navigate backbool CanGoForward- Gets whether the WebView can navigate forward
void Navigate(string url)- Navigates to the specified URLvoid NavigateToString(string html)- Loads the specified HTML contentvoid GoBack()- Navigates back in historyvoid GoForward()- Navigates forward in historyvoid Reload()- Reloads the current pagevoid Stop()- Stops loading the current pageTask<string> ExecuteScriptAsync(string script)- Executes JavaScript code
EventHandler<WebViewNavigationEventArgs> NavigationStarting- Occurs when navigation startsEventHandler<WebViewNavigationEventArgs> NavigationCompleted- Occurs when navigation completesEventHandler<WebViewNavigationEventArgs> NavigationFailed- Occurs when navigation fails
Properties:
string? Uri- The URI of the navigationbool IsSuccess- Whether the navigation was successfulstring? ErrorMessage- Error message if navigation failedint StatusCode- HTTP status code
A complete sample application is included in the samples/WebViewDemo directory. To run it:
cd samples/WebViewDemo
dotnet runThe demo application includes:
- URL navigation with address bar
- Back/Forward navigation buttons
- Reload and Stop buttons
- HTML content loading example
- Status bar showing navigation state
# Clone the repository
git clone https://github.com/Principia4834/GreenSwampWebView.git
cd GreenSwampWebView
# Build the solution
dotnet build GS.WebViewControl.sln
# Run the sample
cd samples/WebViewDemo
dotnet runThe WebView control uses a platform abstraction pattern:
WebView (Main Control)
↓
IWebViewPlatform (Interface)
↓
├── WindowsWebView (Windows/WebView2 COM interop)
├── MacOSWebView (macOS/WKWebView)
└── LinuxWebView (Linux/WebKitGTK)
Each platform implementation handles native interop through P/Invoke:
- Windows: Direct COM interop with WebView2 via WebView2Native.cs (no WinForms dependency)
- macOS: Objective-C runtime calls via WKWebViewNative.cs
- Linux: GTK3 and WebKitGTK calls via WebKitGtkNative.cs
This approach ensures zero overhead from unused platforms and builds cleanly on .NET 8 SDK without requiring platform-specific SDKs.
- Requires WebView2 Runtime to be installed
- COM threading requirements are handled internally
- Async initialization may cause slight delay on first load
- JavaScript execution is simplified (async callback not fully implemented)
- Coordinate system differences are handled automatically
- Requires GTK and WebKitGTK libraries
- JavaScript execution doesn't return results (callback not implemented)
- Best tested on X11; Wayland support may vary
- Install the WebView2 Runtime from Microsoft's website
- Or include the runtime with your application
- Install WebKitGTK:
sudo apt-get install libwebkit2gtk-4.0-37
- Ensure your application has proper window handles
- Check that macOS version is 10.13 or later
- Check that the URL is valid and accessible
- Verify network connectivity
- Look for errors in the console/debug output
Contributions are welcome! Please feel free to submit issues or pull requests.
This project is licensed under the MIT License - see the LICENSE file for details.
- Avalonia UI team for the excellent cross-platform framework
- Microsoft for WebView2
- Apple for WKWebView
- WebKitGTK team for the Linux implementation