// Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. using Microsoft.Maui; using Microsoft.Maui.Controls; using Microsoft.Maui.Hosting; using Microsoft.Maui.Platform.Linux; using Microsoft.Maui.Platform.Linux.Hosting; namespace WebViewDemo; public static class MauiProgram { public static MauiApp CreateMauiApp() { var builder = MauiApp.CreateBuilder(); builder.UseMauiApp(); builder.UseLinux(); return builder.Build(); } } public static class Program { public static void Main(string[] args) { Console.WriteLine("[Program] Starting WebView Demo"); var app = MauiProgram.CreateMauiApp(); LinuxApplication.Run(app, args); } } public class App : Application { public App() { MainPage = new NavigationPage(new WebViewPage()) { Title = "WebView Demo" }; } } public class WebViewPage : ContentPage { private readonly WebView _webView; private readonly Entry _urlEntry; private readonly Label _statusLabel; public WebViewPage() { Title = "WebView Demo"; _webView = new WebView { HeightRequest = 400, VerticalOptions = LayoutOptions.Fill, HorizontalOptions = LayoutOptions.Fill, Source = new UrlWebViewSource { Url = "https://dotnet.microsoft.com" } }; _webView.Navigating += OnNavigating; _webView.Navigated += OnNavigated; _urlEntry = new Entry { Placeholder = "Enter URL...", Text = "https://dotnet.microsoft.com", HorizontalOptions = LayoutOptions.Fill }; _urlEntry.Completed += OnUrlSubmitted; _statusLabel = new Label { Text = "Ready", TextColor = Colors.Gray, FontSize = 12 }; var goButton = new Button { Text = "Go", WidthRequest = 60 }; goButton.Clicked += (s, e) => Navigate(); var backButton = new Button { Text = "Back", WidthRequest = 60 }; backButton.Clicked += (s, e) => _webView.GoBack(); var forwardButton = new Button { Text = "Forward", WidthRequest = 80 }; forwardButton.Clicked += (s, e) => _webView.GoForward(); var reloadButton = new Button { Text = "Reload", WidthRequest = 70 }; reloadButton.Clicked += (s, e) => _webView.Reload(); var loadHtmlButton = new Button { Text = "Load HTML", WidthRequest = 100 }; loadHtmlButton.Clicked += OnLoadHtmlClicked; var evalJsButton = new Button { Text = "Run JS", WidthRequest = 80 }; evalJsButton.Clicked += OnEvalJsClicked; // Navigation bar var navBar = new HorizontalStackLayout { Spacing = 5, Children = { backButton, forwardButton, reloadButton } }; // URL bar var urlBar = new HorizontalStackLayout { Spacing = 5, Children = { _urlEntry, goButton } }; // Action buttons var actionBar = new HorizontalStackLayout { Spacing = 5, Children = { loadHtmlButton, evalJsButton } }; Content = new VerticalStackLayout { Padding = 10, Spacing = 10, Children = { new Label { Text = "WebView Demo - WebKitGTK", FontSize = 20, FontAttributes = FontAttributes.Bold }, navBar, urlBar, _webView, actionBar, _statusLabel } }; } private void Navigate() { var url = _urlEntry.Text?.Trim(); if (string.IsNullOrEmpty(url)) return; // Add https:// if not present if (!url.StartsWith("http://") && !url.StartsWith("https://")) url = "https://" + url; _webView.Source = new UrlWebViewSource { Url = url }; _urlEntry.Text = url; } private void OnUrlSubmitted(object? sender, EventArgs e) { Navigate(); } private void OnNavigating(object? sender, WebNavigatingEventArgs e) { _statusLabel.Text = $"Loading: {e.Url}"; Console.WriteLine($"[WebViewPage] Navigating to: {e.Url}"); } private void OnNavigated(object? sender, WebNavigatedEventArgs e) { _statusLabel.Text = e.Result == WebNavigationResult.Success ? $"Loaded: {e.Url}" : $"Failed: {e.Result}"; _urlEntry.Text = e.Url; Console.WriteLine($"[WebViewPage] Navigated: {e.Result} - {e.Url}"); } private void OnLoadHtmlClicked(object? sender, EventArgs e) { var html = @" OpenMaui WebView

Hello from OpenMaui Linux!

This HTML content is rendered by WebKitGTK inside your .NET MAUI application.

WebView Features:

Powered by WebKitGTK - the same engine used by GNOME Web (Epiphany)

"; _webView.Source = new HtmlWebViewSource { Html = html }; _statusLabel.Text = "Loaded custom HTML"; } private async void OnEvalJsClicked(object? sender, EventArgs e) { try { var result = await _webView.EvaluateJavaScriptAsync("document.title"); _statusLabel.Text = $"JS Result: {result ?? "(null)"}"; Console.WriteLine($"[WebViewPage] JS Eval result: {result}"); } catch (Exception ex) { _statusLabel.Text = $"JS Error: {ex.Message}"; } } }