.NET MAUI自定义启动屏失效:全屏显示与自动跳转问题求助
.NET MAUI 自定义全屏启动屏(带版本信息+自动跳转)
一、彻底移除Android默认启动蓝屏
注释项目文件中的<MauiSplashScreen>后,Android仍会显示默认蓝屏,这是因为Android平台启动主题自带背景,按以下步骤修改:
- 打开
Platforms/Android/Resources/values/styles.xml - 找到
Theme.Maui.SplashScreen主题,修改配置让启动窗口透明:
<style name="Theme.Maui.SplashScreen" parent="Theme.Maui.Light"> <item name="android:windowBackground">@android:color/transparent</item> <item name="android:windowFullscreen">true</item> <item name="android:windowContentOverlay">@null</item> </style>
- 打开
Platforms/Android/MainActivity.cs,确保Activity注解的Theme指向修改后的主题:
[Activity(Theme = "@style/Theme.Maui.SplashScreen", MainLauncher = true, LaunchMode = LaunchMode.SingleTop, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation | ConfigChanges.UiMode | ConfigChanges.ScreenLayout | ConfigChanges.SmallestScreenSize | ConfigChanges.Density)]
二、创建自定义启动页ContentPage
1. 启动页XAML(MySplashScreen.xaml)
实现全屏图片+底部版本信息布局:
<?xml version="1.0" encoding="utf-8" ?> <ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" x:Class="YourAppNamespace.MySplashScreen" NavigationPage.HasNavigationBar="False" BackgroundColor="#000000"> <AbsoluteLayout> <!-- 全屏铺满的启动图 --> <Image Source="splash_bg.jpg" AbsoluteLayout.LayoutBounds="0,0,1,1" AbsoluteLayout.LayoutFlags="All" Aspect="AspectFill"/> <!-- 版本信息标签,固定在底部居中 --> <Label x:Name="VersionLabel" TextColor="#FFFFFF" FontSize="Small" Margin="0,0,0,20" AbsoluteLayout.LayoutBounds="0.5,0.95,AutoSize,AutoSize" AbsoluteLayout.LayoutFlags="PositionProportional"/> </AbsoluteLayout> </ContentPage>
2. 启动页后台代码(MySplashScreen.xaml.cs)
添加版本信息读取和3秒自动跳转逻辑:
using Microsoft.Maui.ApplicationModel; namespace YourAppNamespace; public partial class MySplashScreen : ContentPage { public MySplashScreen() { InitializeComponent(); LoadAppVersion(); StartAutoNavigation(); } private void LoadAppVersion() { // 获取当前应用版本号 var version = AppInfo.Current.VersionString; VersionLabel.Text = $"版本 {version}"; } private async void StartAutoNavigation() { // 延迟3秒后跳转到主页面 await Task.Delay(3000); // 非Shell项目直接替换MainPage Application.Current.MainPage = new MainPage(); // Shell项目使用路由跳转:await Shell.Current.GoToAsync("//MainPage"); } }
三、设置App初始页面
修改App.xaml.cs,将初始页面设置为自定义启动页:
public App() { InitializeComponent(); MainPage = new MySplashScreen(); }
四、额外注意事项
- 确保启动图
splash_bg.jpg已添加到项目资源,设置生成操作为MauiImage并勾选所有平台 - 如需隐藏状态栏,可在启动页的
OnAppearing方法中添加:
protected override void OnAppearing() { base.OnAppearing(); #if ANDROID Microsoft.Maui.ApplicationModel.Platform.CurrentActivity.Window.SetFlags( Android.Views.WindowManagerFlags.Fullscreen, Android.Views.WindowManagerFlags.Fullscreen); #elif IOS UIKit.UIApplication.SharedApplication.StatusBarHidden = true; #endif }
内容的提问来源于stack exchange,提问作者OXO
相关产品推荐
相关产品推荐

