MAUI启动画面依赖各平台原生机制:Android用SplashActivity和主题配置,iOS/macOS用LaunchScreen.storyboard,Windows用SplashScreen元素;统一隐藏逻辑可通过App.xaml.cs控制。

MAUI 实现启动画面(Splash Screen)主要靠平台原生配置,因为 MAUI 本身不提供跨平台的 SplashScreen 控件——它依赖 Android、iOS、macOS 和 Windows 各自的启动图机制。核心思路是:在原生项目中设置启动图资源 + 配置启动 Activity / Scene / Window,再用 MAUI 的 App.xaml.cs 或生命周期事件控制“何时隐藏”。
Android 是最典型的场景。你需要一个专用的 SplashActivity 作为启动入口,并通过主题指定背景图。
Platforms/Android/Resources/values/styles.xml 中定义主题:<style name="SplashTheme" parent="Theme.AppCompat.Light.NoActionBar">
<item name="android:windowBackground">@drawable/splash_background</item>
<item name="android:windowNoTitle">true</item>
<item name="android:windowFullscreen">true</item>
</style>Resources/drawable/ 下新建 splash_background.xml(用 `Platforms/Android/MainActivity.cs 上方添加 [Activity(Theme = "@style/SplashTheme", ...)],并确保 LaunchMode = LaunchMode.SingleTop;OnCreate 中调用 base.OnCreate(savedInstanceState) 后立即跳转到主 Activity(如 StartActivity(typeof(MainActivity))),再 Finish() 关闭 Splash 页面。iOS 和 macOS 使用 Storyboard 做启动图,无需代码逻辑,纯配置。
Platforms/iOS/LaunchScreen.storyboard(若不存在可新建),拖入 ImageView 或约束布局,设置图片为 Assets.xcassets 中的启动图资源;Info.plist 中的 UILaunchStoryboardName 指向 LaunchScreen;Platforms/MacCatalyst/Assets.xcassets 添加启动图,并在 Info.plist 设置 NSAppIcon 和启动配置(macOS 启动图实际是窗口首次显示前的静态图,靠 Application.Windows[0].Show(); 触发时机控制)。Windows(WinUI3)支持内置 SplashScreen API,MAUI 会自动启用,但需注意资源路径和隐藏时机。
Platforms/Windows/App.xaml 中 Application.SplashScreen 已声明(MAUI 模板默认已配);Platforms/Windows/Assets/SplashScreen.png(推荐 1280×720 或按比例缩放);App.xaml.cs 的 OnInitialized 或 OnLaunched 中,调用 Microsoft.UI.Xaml.Window.Current.Content = new MainPage(); 后,SplashScreen 会自动隐藏;如需手动控制,可通过 WindowHelper.GetWindowForCurrentView().Content = ... 触发。如果你希望在 MAUI 页面加载完成后才真正“结束”启动流程(比如等数据初始化完),可以加一层过渡控制:
App.xaml.cs 的 OnInitialized 中不立即设置 MainPage,而是先设为一个空 ContentPage 或占位页;Application.Current.MainPage = new MainPage();Handler.PostDelayed 确保 UI 线程安全。基本上就这些。关键不是写多少 C# 代码,而是理解各平台启动图的本质——它是原生系统在应用进程启动初期渲染的一张静态图,MAUI 只负责衔接好这个过程。配对资源尺寸、命名规范、主题/Storyboard 绑定,比写逻辑更重要。
以上就是MAUI怎么实现一个Splash Screen MAUI启动画面配置的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号