.NET MAUI安卓平台实现打开页面全屏显示
1.创建隐藏状态栏行为类:HideStatusBarBehavior.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 | public class HideStatusBarBehavior : PlatformBehavior<Page> { #if ANDROID protected override void OnAttachedTo(Page bindable, Android.Views.View platformView) { SetStatusBarVisibility( false ); } #endif #if ANDROID protected override void OnDetachedFrom(Page bindable, Android.Views.View platformView) { SetStatusBarVisibility( true ); } #endif private static void SetStatusBarVisibility( bool isVisible) { #if ANDROID var activity = Platform.CurrentActivity ?? throw new InvalidOperationException( "Android Activity can't be null." ); var window = activity.Window ?? throw new InvalidOperationException($ "{nameof(activity.Window)} cannot be null" ); #endif if (isVisible) { #if ANDROID window.ClearFlags(Android.Views.WindowManagerFlags.TranslucentStatus); window.ClearFlags(Android.Views.WindowManagerFlags.Fullscreen); window.Attributes.LayoutInDisplayCutoutMode = Android.Views.LayoutInDisplayCutoutMode.Default; #endif } else { #if ANDROID window.Attributes.LayoutInDisplayCutoutMode = Android.Views.LayoutInDisplayCutoutMode.ShortEdges; window.AddFlags(Android.Views.WindowManagerFlags.TranslucentStatus); window.AddFlags(Android.Views.WindowManagerFlags.Fullscreen); #endif } } } |
2.在全屏显示的页面设置隐藏shell的导航栏,将演示模式设置为Modal,添加隐藏状态栏行为
1 2 3 4 5 6 7 8 9 10 11 12 | < ContentPage x:Class="FullScreenDemo.FullScreenPage" xmlns="http://schemas.microsoft.com/dotnet/2021/maui" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" xmlns:local="clr-namespace:FullScreenDemo" Shell.NavBarIsVisible="False" Shell.PresentationMode="Modal"> < ContentPage.Behaviors > < local:HideStatusBarBehavior /> </ ContentPage.Behaviors > ... </ ContentPage > |
3.使用shell导航到页面
1 | Shell.Current.GoToAsync( "FullScreenPage" ); |
恭喜你,大功告成!
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 微软正式发布.NET 10 Preview 1:开启下一代开发框架新篇章
· 没有源码,如何修改代码逻辑?
· NetPad:一个.NET开源、跨平台的C#编辑器
· PowerShell开发游戏 · 打蜜蜂
· 凌晨三点救火实录:Java内存泄漏的七个神坑,你至少踩过三个!