From 1d9338d8233715bccef126d0e30d8f228bc55a85 Mon Sep 17 00:00:00 2001 From: logikonline Date: Fri, 19 Dec 2025 05:17:50 -0500 Subject: [PATCH] Add full XAML support for .NET MAUI compatibility New features: - MauiAppBuilderExtensions.UseOpenMauiLinux() for standard MAUI integration - New template: openmaui-linux-xaml with full XAML support - Standard MAUI XAML syntax (ContentPage, VerticalStackLayout, etc.) - Resource dictionaries (Colors.xaml, Styles.xaml) - Compiled XAML support via MauiXaml items Templates now available: - dotnet new openmaui-linux (code-based UI) - dotnet new openmaui-linux-xaml (XAML-based UI) Usage: var builder = MauiApp.CreateBuilder(); builder .UseMauiApp() .UseOpenMauiLinux(); // Enable Linux with XAML --- Hosting/MauiAppBuilderExtensions.cs | 193 ++++++++++++++++++ README.md | 36 +++- templates/OpenMaui.Linux.Templates.csproj | 11 +- .../.template.config/template.json | 41 ++++ templates/openmaui-linux-xaml-app/App.xaml | 13 ++ templates/openmaui-linux-xaml-app/App.xaml.cs | 10 + .../openmaui-linux-xaml-app/MainPage.xaml | 82 ++++++++ .../openmaui-linux-xaml-app/MainPage.xaml.cs | 30 +++ .../openmaui-linux-xaml-app/MauiProgram.cs | 24 +++ .../OpenMauiXamlApp.csproj | 38 ++++ templates/openmaui-linux-xaml-app/Program.cs | 16 ++ .../Resources/Fonts/.gitkeep | 4 + .../Resources/Images/.gitkeep | 2 + .../Resources/Styles/Colors.xaml | 49 +++++ .../Resources/Styles/Styles.xaml | 104 ++++++++++ 15 files changed, 646 insertions(+), 7 deletions(-) create mode 100644 Hosting/MauiAppBuilderExtensions.cs create mode 100644 templates/openmaui-linux-xaml-app/.template.config/template.json create mode 100644 templates/openmaui-linux-xaml-app/App.xaml create mode 100644 templates/openmaui-linux-xaml-app/App.xaml.cs create mode 100644 templates/openmaui-linux-xaml-app/MainPage.xaml create mode 100644 templates/openmaui-linux-xaml-app/MainPage.xaml.cs create mode 100644 templates/openmaui-linux-xaml-app/MauiProgram.cs create mode 100644 templates/openmaui-linux-xaml-app/OpenMauiXamlApp.csproj create mode 100644 templates/openmaui-linux-xaml-app/Program.cs create mode 100644 templates/openmaui-linux-xaml-app/Resources/Fonts/.gitkeep create mode 100644 templates/openmaui-linux-xaml-app/Resources/Images/.gitkeep create mode 100644 templates/openmaui-linux-xaml-app/Resources/Styles/Colors.xaml create mode 100644 templates/openmaui-linux-xaml-app/Resources/Styles/Styles.xaml diff --git a/Hosting/MauiAppBuilderExtensions.cs b/Hosting/MauiAppBuilderExtensions.cs new file mode 100644 index 0000000..4cc3b75 --- /dev/null +++ b/Hosting/MauiAppBuilderExtensions.cs @@ -0,0 +1,193 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// Copyright (c) 2025 MarketAlly LLC + +using Microsoft.Maui; +using Microsoft.Maui.Controls; +using Microsoft.Maui.Controls.Hosting; +using Microsoft.Maui.Hosting; +using OpenMaui.Platform.Linux.Handlers; + +namespace OpenMaui.Platform.Linux.Hosting; + +/// +/// Extension methods for configuring OpenMaui Linux platform in a MAUI application. +/// This enables full XAML support by registering Linux-specific handlers. +/// +public static class MauiAppBuilderExtensions +{ + /// + /// Configures the application to use OpenMaui Linux platform with full XAML support. + /// + /// The MAUI app builder. + /// The configured MAUI app builder. + /// + /// + /// var builder = MauiApp.CreateBuilder(); + /// builder + /// .UseMauiApp<App>() + /// .UseOpenMauiLinux(); // Enable Linux support with XAML + /// + /// + public static MauiAppBuilder UseOpenMauiLinux(this MauiAppBuilder builder) + { + builder.ConfigureMauiHandlers(handlers => + { + // Register all Linux platform handlers + // These map MAUI virtual views to our Skia platform views + + // Basic Controls + handlers.AddHandler(); + handlers.AddHandler(); + handlers.AddHandler(); + handlers.AddHandler(); + handlers.AddHandler(); + handlers.AddHandler(); + handlers.AddHandler(); + + // Selection Controls + handlers.AddHandler(); + handlers.AddHandler(); + handlers.AddHandler(); + handlers.AddHandler(); + handlers.AddHandler(); + + // Display Controls + handlers.AddHandler(); + handlers.AddHandler(); + handlers.AddHandler(); + handlers.AddHandler(); + + // Layout Controls + handlers.AddHandler(); + handlers.AddHandler(); + handlers.AddHandler(); + handlers.AddHandler(); + + // Collection Controls + handlers.AddHandler(); + handlers.AddHandler(); + handlers.AddHandler(); + handlers.AddHandler(); + handlers.AddHandler(); + + // Navigation Controls + handlers.AddHandler(); + handlers.AddHandler(); + handlers.AddHandler(); + handlers.AddHandler(); + + // Page Controls + handlers.AddHandler(); + handlers.AddHandler(); + + // Graphics + handlers.AddHandler(); + + // Search + handlers.AddHandler(); + + // Window + handlers.AddHandler(); + }); + + // Register Linux-specific services + builder.Services.AddSingleton(); + + return builder; + } + + /// + /// Configures the application to use OpenMaui Linux with custom handler configuration. + /// + /// The MAUI app builder. + /// Action to configure additional handlers. + /// The configured MAUI app builder. + public static MauiAppBuilder UseOpenMauiLinux( + this MauiAppBuilder builder, + Action? configureHandlers) + { + builder.UseOpenMauiLinux(); + + if (configureHandlers != null) + { + builder.ConfigureMauiHandlers(configureHandlers); + } + + return builder; + } +} + +/// +/// Interface for Linux platform services. +/// +public interface ILinuxPlatformServices +{ + /// + /// Gets the display server type (X11 or Wayland). + /// + DisplayServerType DisplayServer { get; } + + /// + /// Gets the current DPI scale factor. + /// + float ScaleFactor { get; } + + /// + /// Gets whether high contrast mode is enabled. + /// + bool IsHighContrastEnabled { get; } +} + +/// +/// Display server types supported by OpenMaui. +/// +public enum DisplayServerType +{ + /// X11 display server. + X11, + /// Wayland display server. + Wayland, + /// Auto-detected display server. + Auto +} + +/// +/// Implementation of Linux platform services. +/// +internal class LinuxPlatformServices : ILinuxPlatformServices +{ + public DisplayServerType DisplayServer => DetectDisplayServer(); + public float ScaleFactor => DetectScaleFactor(); + public bool IsHighContrastEnabled => DetectHighContrast(); + + private static DisplayServerType DetectDisplayServer() + { + var waylandDisplay = Environment.GetEnvironmentVariable("WAYLAND_DISPLAY"); + if (!string.IsNullOrEmpty(waylandDisplay)) + return DisplayServerType.Wayland; + + var display = Environment.GetEnvironmentVariable("DISPLAY"); + if (!string.IsNullOrEmpty(display)) + return DisplayServerType.X11; + + return DisplayServerType.Auto; + } + + private static float DetectScaleFactor() + { + // Try GDK_SCALE first + var gdkScale = Environment.GetEnvironmentVariable("GDK_SCALE"); + if (float.TryParse(gdkScale, out var scale)) + return scale; + + // Default to 1.0 + return 1.0f; + } + + private static bool DetectHighContrast() + { + var highContrast = Environment.GetEnvironmentVariable("GTK_THEME"); + return highContrast?.Contains("HighContrast", StringComparison.OrdinalIgnoreCase) ?? false; + } +} diff --git a/README.md b/README.md index 9495615..a4a9675 100644 --- a/README.md +++ b/README.md @@ -27,14 +27,14 @@ This project brings .NET MAUI to Linux desktops with native X11/Wayland support, ### Installation ```bash -# Install the template +# Install the templates dotnet new install OpenMaui.Linux.Templates -# Create a new project -dotnet new openmaui-linux -n MyApp -cd MyApp +# Create a new project (choose one): +dotnet new openmaui-linux -n MyApp # Code-based UI +dotnet new openmaui-linux-xaml -n MyApp # XAML-based UI (recommended) -# Run +cd MyApp dotnet run ``` @@ -44,6 +44,32 @@ dotnet run dotnet add package OpenMaui.Controls.Linux --prerelease ``` +## XAML Support + +OpenMaui fully supports standard .NET MAUI XAML syntax. Use the familiar XAML workflow: + +```xml + + + +