如何解决.NET MAUI Blazor应用启动闪白及Windows启动屏不显示问题
你遇到的启动白闪问题,本质是应用初始化阶段原生窗口默认使用白色背景,而你之前修改的都是MAUI页面/BlazorWebView的背景,这些配置在窗口初始化完成后才生效,无法解决启动瞬间的闪屏。另外Windows平台默认启动屏不显示,是因为MAUI对Windows的启动屏需要单独配置,而非自动启用。
一、Windows平台:修复启动白闪+启用默认启动屏
Windows平台的启动闪屏和窗口初始背景需通过修改Platforms/Windows/App.xaml配置:
- 设置窗口初始深色背景
打开Platforms/Windows/App.xaml,在Application.Resources中添加窗口样式,覆盖默认白色背景:
<Application.Resources> <ResourceDictionary> <Style TargetType="Window"> <Setter Property="Background" Value="#1E1E1E"/> <!-- 替换为你的深色模式背景色 --> </Style> </ResourceDictionary> </Application.Resources>
- 启用MAUI默认启动屏显示
在同一个App.xaml的MauiWinUIApplication节点中,添加SplashScreen属性指向项目启动屏资源:
<MauiWinUIApplication x:Class="YourAppName.WinUI.App" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="using:YourAppName.WinUI" SplashScreen="Resources\SplashScreen.png"> <!-- 指向项目中默认的.NET Logo图片路径 --> </MauiWinUIApplication>
确认项目中
Resources/SplashScreen.png存在(MAUI默认模板已包含),若缺失可将官方.NET Logo图片放入该路径。
二、Android平台:消除启动白闪
Android的启动闪屏由drawable文件夹下的配置控制:
- 创建/修改启动屏配置文件
Platforms/Android/Resources/drawable/splash_screen.xml:
<?xml version="1.0" encoding="utf-8"?> <layer-list xmlns:android="http://schemas.android.com/apk/res/android"> <item android:drawable="@color/dark_background" /> <!-- 深色背景 --> <!-- 可选:添加应用Logo --> <item> <bitmap android:gravity="center" android:src="@mipmap/ic_launcher" /> </item> </layer-list>
- 在
Platforms/Android/Resources/values/colors.xml中定义深色背景色:
<resources> <color name="dark_background">#1E1E1E</color> </resources>
- 修改
Platforms/Android/Manifest/AndroidManifest.xml,指定启动屏主题:
<application android:allowBackup="true" android:icon="@mipmap/appicon" android:roundIcon="@mipmap/appicon_round" android:supportsRtl="true" android:theme="@style/Maui.SplashTheme">
确保Maui.SplashTheme在styles.xml中引用启动屏配置:
<style name="Maui.SplashTheme" parent="Maui.BaseTheme"> <item name="android:windowBackground">@drawable/splash_screen</item> </style>
三、iOS平台:消除启动白闪
iOS的启动屏通过LaunchScreen.storyboard配置:
打开Platforms/iOS/Resources/LaunchScreen.storyboard,选中根视图,在属性面板中设置背景色为你的深色模式颜色(例如#1E1E1E)。
四、BlazorWebView加载阶段优化
由于你使用了BlazorWebView,WebView初始化时可能出现白色闪屏,需修改wwwroot/index.html的背景色:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no, viewport-fit=cover" /> <title>YourAppName</title> <base href="/" /> <link href="_content/Microsoft.Maui.Controls/MauiBlazorWebView.css" rel="stylesheet" /> <link href="css/app.css" rel="stylesheet" /> <link href="YourAppName.styles.css" rel="stylesheet" /> <style> body { background-color: #1E1E1E; /* 匹配应用深色背景 */ margin: 0; padding: 0; } </style> </head> <body> <div id="app">Loading...</div> <div id="blazor-error-ui"> An unhandled error has occurred. <a href="" class="reload">Reload</a> <a class="dismiss">🗙</a> </div> <script src="_framework/blazor.webview.js"></script> </body> </html>
你之前修改的ContentPage.BackgroundColor、BlazorWebView.BackgroundColor以及平台Handler背景色,都是在MAUI页面和WebView组件初始化完成后才生效的,而启动白闪发生在原生窗口创建、MAUI框架尚未加载页面的阶段,因此这些修改无法覆盖原生窗口的初始白色背景。
内容的提问来源于stack exchange,提问作者Giancarlo Franco

