.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");

  

恭喜你,大功告成!

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

posted @   .NET-卧龙  阅读(743)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 微软正式发布.NET 10 Preview 1:开启下一代开发框架新篇章
· 没有源码,如何修改代码逻辑?
· NetPad:一个.NET开源、跨平台的C#编辑器
· PowerShell开发游戏 · 打蜜蜂
· 凌晨三点救火实录:Java内存泄漏的七个神坑,你至少踩过三个!
点击右上角即可分享
微信分享提示