Maui Blazor 中文社区 QQ群:645660665

Maui Blazor 设置全屏以及去掉标题栏 (Windows 平台)

编辑 MauiProgram.cs 文件

  1. 隐藏标题栏,隐藏边框

    • 已知问题, 可调整大小窗体, 标题栏会残留一点像素作为调整句柄

    using Microsoft.Extensions.Logging;
    using Microsoft.Maui.LifecycleEvents;
    #if WINDOWS
    using Microsoft.UI;
    using Microsoft.UI.Windowing;
    using Microsoft.UI.Xaml.Controls;
    using Microsoft.UI.Xaml;
    #endif
    
    namespace JovenResApp;
    public static class MauiProgram
    {
        public static MauiApp CreateMauiApp()
        {
            var builder = MauiApp.CreateBuilder();
            builder
                .UseMauiApp<App>()
                .ConfigureFonts(fonts =>
                {
                    fonts.AddFont("OpenSans-Regular.ttf", "OpenSansRegular");
                    fonts.AddFont("OpenSans-Semibold.ttf", "OpenSansSemibold");
                });
    
    #if DEBUG
            builder.Logging.AddDebug();
    #endif
    
    #if WINDOWS     
            builder.ConfigureLifecycleEvents((events) =>
            {
                events.AddWindows(wndLifeCycleBuilder =>
                {
                    wndLifeCycleBuilder.OnWindowCreated((window) =>
                    {
                        if (window.AppWindow.Presenter is OverlappedPresenter p)
                        {
                            //p.IsResizable = false;
                            p.SetBorderAndTitleBar(false, false); 
                        }
                    });
                });
            });
    #endif
    
            return builder.Build();
        }
    }
    
  2. 全屏, 不可调整窗口大小

    using Microsoft.Extensions.Logging;
    using Microsoft.Maui.LifecycleEvents;
    #if WINDOWS
    using Microsoft.UI;
    using Microsoft.UI.Windowing;
    using Microsoft.UI.Xaml.Controls;
    using Microsoft.UI.Xaml;
    #endif
    
    namespace JovenResApp;
    public static class MauiProgram
    {
        public static MauiApp CreateMauiApp()
        {
            var builder = MauiApp.CreateBuilder();
            builder
                .UseMauiApp<App>()
                .ConfigureFonts(fonts =>
                {
                    fonts.AddFont("OpenSans-Regular.ttf", "OpenSansRegular");
                    fonts.AddFont("OpenSans-Semibold.ttf", "OpenSansSemibold");
                });
    
    #if DEBUG
            builder.Logging.AddDebug();
    #endif
    
    #if WINDOWS     
            builder.ConfigureLifecycleEvents((events) =>
            {
                events.AddWindows(wndLifeCycleBuilder =>
                {
                    wndLifeCycleBuilder.OnWindowCreated((window) =>
                    {
                        window.AppWindow.SetPresenter(AppWindowPresenterKind.FullScreen);//全屏
                        if (window.AppWindow.Presenter is OverlappedPresenter p)
                        {
                            p.IsResizable = false;
                            p.SetBorderAndTitleBar(false, false); 
                        }
                    });
                });
            });
    #endif
    
            return builder.Build();
        }
    }
    
    
  3. 如果不需要部署为Maui平台,其实可以简单使用 Wpf blazor hybird

    简简单单就实现

    WindowState="Maximized"  WindowStyle="None" 
    
posted @ 2024-11-24 18:36  AlexChow  阅读(45)  评论(0编辑  收藏  举报