.NET MAUI安卓平台实现打开页面全屏显示

1.创建隐藏状态栏行为类:HideStatusBarBehavior.cs

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,添加隐藏状态栏行为

<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导航到页面

Shell.Current.GoToAsync("FullScreenPage");

  

恭喜你,大功告成!

Demo:Maui_FullScreenDemo: Maui安卓平台页面全屏显示demo (gitee.com)

posted @ 2023-02-22 16:56  .NET-卧龙  阅读(1028)  评论(0)    收藏  举报