MAUI实现沉浸式全屏需分平台处理:Android可隐藏状态栏和导航栏,iOS仅能隐藏状态栏;需配合布局Padding、SafeArea适配及平台服务封装,注意手势、键盘与横竖屏兼容性。

MAUI 实现全屏显示(即“沉浸式模式”)主要通过平台特定代码控制状态栏和导航栏的可见性,Android 和 iOS 的实现方式不同,Windows/macOS 不支持传统意义上的沉浸式全屏。核心思路是:隐藏系统 UI(状态栏、导航栏),让应用内容铺满整个屏幕,同时避免内容被系统栏遮挡。
在 MAUI 中需通过 Android 平台专属代码 设置 Window 的 DecorView 系统 UI 标志:
Platforms/Android/MainActivity.cs 的 OnCreate 方法中(确保在 base.OnCreate(savedInstanceState) 之后)添加:if (Build.VERSION.SdkInt >= BuildVersionCodes.R)
{
Window.InsetsController?.Hide(WindowInsets.Type.StatusBars() | WindowInsets.Type.NavigationBars());
Window.InsetsController?.SystemBarsBehavior = Android.Graphics.Insets.SystemBarsBehavior.ShowTransientBarsBySwipe;
}
else
{
var decorView = Window.DecorView;
decorView.SystemUiVisibility = (StatusBarVisibility)(SystemUiFlags.Fullscreen
| SystemUiFlags.HideNavigation
| SystemUiFlags.ImmersiveSticky
| SystemUiFlags.LayoutFullscreen
| SystemUiFlags.LayoutHideNavigation);
}ContentPage)设置:Padding="0",并确保容器(如 Grid 或 ScrollView)不自动预留系统栏空间;iOS 不允许隐藏底部安全区域(Home Indicator),仅能隐藏顶部状态栏:
Platforms/iOS/AppDelegate.cs 中重写 GetStatusBarHidden:public override bool GetStatusBarHidden(UIApplication application) => true;
Info.plist 中添加键 UIViewControllerBasedStatusBarAppearance,值设为 YES(确保状态栏控制权交由 ViewController);Frame)设置:IgnoreSafeArea="True"(MAUI 8+ 支持)或使用 SafeAreaEffect 手动处理。为便于管理,可在共享项目中定义接口,再由各平台实现:
IPlatformFullScreenService 接口,含 EnterImmersiveMode() 和 ExitImmersiveMode();UIApplication.SharedApplication.SetStatusBarHidden;MauiProgram.cs 中注册服务:builder.Services.AddSingleton<iplatformfullscreenservice androidfullscreenservice>()</iplatformfullscreenservice>(按平台条件注册);ImmersiveSticky 后,从边缘上滑可临时呼出导航栏,但不会自动恢复隐藏——这是预期行为;OnConfigurationChanged 中重新应用沉浸设置;IgnoreSafeArea 和平台服务注册更友好,旧版本需手动处理更多生命周期回调。基本上就这些。沉浸式不是“一键开关”,而是结合平台特性、布局适配和生命周期控制的组合操作。做好状态同步(比如退出页面时恢复状态栏)和边界测试(折叠屏、刘海屏、iPad),体验才稳。
以上就是MAUI怎么实现全屏显示 MAUI沉浸式模式教程的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号