From f87ea17a6fc53e2300c9f0f91dc1dcf14b620f32 Mon Sep 17 00:00:00 2001 From: logikonline Date: Sun, 11 Jan 2026 10:37:01 -0500 Subject: [PATCH] Restructured sample pages folder --- TodoApp/{ => Pages}/NewTodoPage.xaml | 0 TodoApp/{ => Pages}/NewTodoPage.xaml.cs | 0 TodoApp/{ => Pages}/TodoDetailPage.xaml | 0 TodoApp/{ => Pages}/TodoDetailPage.xaml.cs | 0 TodoApp/{ => Pages}/TodoListPage.xaml | 0 TodoApp/{ => Pages}/TodoListPage.xaml.cs | 0 WebViewDemo/Program.cs | 283 +++++++++++++++++++++ WebViewDemo/WebViewDemo.csproj | 18 ++ 8 files changed, 301 insertions(+) rename TodoApp/{ => Pages}/NewTodoPage.xaml (100%) rename TodoApp/{ => Pages}/NewTodoPage.xaml.cs (100%) rename TodoApp/{ => Pages}/TodoDetailPage.xaml (100%) rename TodoApp/{ => Pages}/TodoDetailPage.xaml.cs (100%) rename TodoApp/{ => Pages}/TodoListPage.xaml (100%) rename TodoApp/{ => Pages}/TodoListPage.xaml.cs (100%) create mode 100644 WebViewDemo/Program.cs create mode 100644 WebViewDemo/WebViewDemo.csproj diff --git a/TodoApp/NewTodoPage.xaml b/TodoApp/Pages/NewTodoPage.xaml similarity index 100% rename from TodoApp/NewTodoPage.xaml rename to TodoApp/Pages/NewTodoPage.xaml diff --git a/TodoApp/NewTodoPage.xaml.cs b/TodoApp/Pages/NewTodoPage.xaml.cs similarity index 100% rename from TodoApp/NewTodoPage.xaml.cs rename to TodoApp/Pages/NewTodoPage.xaml.cs diff --git a/TodoApp/TodoDetailPage.xaml b/TodoApp/Pages/TodoDetailPage.xaml similarity index 100% rename from TodoApp/TodoDetailPage.xaml rename to TodoApp/Pages/TodoDetailPage.xaml diff --git a/TodoApp/TodoDetailPage.xaml.cs b/TodoApp/Pages/TodoDetailPage.xaml.cs similarity index 100% rename from TodoApp/TodoDetailPage.xaml.cs rename to TodoApp/Pages/TodoDetailPage.xaml.cs diff --git a/TodoApp/TodoListPage.xaml b/TodoApp/Pages/TodoListPage.xaml similarity index 100% rename from TodoApp/TodoListPage.xaml rename to TodoApp/Pages/TodoListPage.xaml diff --git a/TodoApp/TodoListPage.xaml.cs b/TodoApp/Pages/TodoListPage.xaml.cs similarity index 100% rename from TodoApp/TodoListPage.xaml.cs rename to TodoApp/Pages/TodoListPage.xaml.cs diff --git a/WebViewDemo/Program.cs b/WebViewDemo/Program.cs new file mode 100644 index 0000000..289dd7c --- /dev/null +++ b/WebViewDemo/Program.cs @@ -0,0 +1,283 @@ +// 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:

+
    +
  • Full HTML5 support
  • +
  • CSS3 animations and transitions
  • +
  • JavaScript execution
  • +
  • Navigation history (back/forward)
  • +
  • WebGL and canvas support
  • +
+
+ + + +

+ 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}"; + } + } +} diff --git a/WebViewDemo/WebViewDemo.csproj b/WebViewDemo/WebViewDemo.csproj new file mode 100644 index 0000000..beca1f9 --- /dev/null +++ b/WebViewDemo/WebViewDemo.csproj @@ -0,0 +1,18 @@ + + + + Exe + net9.0 + enable + enable + WebViewDemo + linux-arm64 + true + false + + + + + + +