如何使用 AppShell 更改 Xamarin Forms ContentPage 中的状态栏? [英] How to change the Status Bar in Xamarin Forms ContentPage using AppShell?

查看:37
本文介绍了如何使用 AppShell 更改 Xamarin Forms ContentPage 中的状态栏?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用新概念 AppShell,我正在执行以下流程

I am using the new concept AppShell, and I am doing the following flow

    App.MainPage = new AppShell();

那我做

    protected async override void OnStart()
    {
        await Shell.Current.GoToAsync($"//{nameof(LoginPage)}");
    }

有了这个,我有一个 ForgetPasswordPage,来自 LoginViewModel 我做

With this I have a ForgetPasswordPage, from LoginViewModel I do

   await Shell.Current.GoToAsync($"//{nameof(ForgetPasswordPage)}");

关键是如果我这样做

    var color = new Color(33, 150, 243);
    Xamarin.Essentials.Platform.CurrentActivity.Window.SetStatusBarColor(color);

在 Android 的 Create 方法中,StatusBar 具有我定义的颜色.现在iOS的方法在哪里,像这样?

Inside the Create method from Android the StatusBar has the color I defined. Now where is the method for iOS, like this?

我尝试了这些解决方案

Xamarin.Forms.Shell:如何管理状态栏颜色

https://github.com/georgemichailou/ShaXam/

没有成功...

有人可以解释一下,如何更改状态代码,但我想在我的 ContentPage 中设置它,我可以使用样式或这样的简单代码行

Someone can explain, how to change the status code but I would like to set it inside my ContentPage, I could use a style, or a simple line of code like this

Xamarin.Essentials.Platform.CurrentActivity.Window.SetStatusBarColor(color);

Xamarin.Essentials.Platform.CurrentActivity.Window.SetStatusBarColor(color);

在 Android 项目中使用.

Which is used in Android project.

我按照其他帖子中的步骤做了,然后得到:

I did the steps from the other posts, and I got:

Foundation.MonoTouchException:'Objective-C 异常抛出.名称:NSInternalInconsistencyException 原因:在 UIApplication 上调用了 -statusBar 或 -statusBarWindow 的应用程序:必须更改此代码,因为不再有状态栏或状态栏窗口.改为在窗口场景中使用 statusBarManager 对象.本机堆栈跟踪:0 核心基金会 0x00007fff20422fba __exceptionPreprocess + 242

System.Reflection.TargetInvocationException Message=调用的目标已抛出异常

推荐答案

您可以在此处查看我的视频,其中详细介绍了:https://www.youtube.com/watch?v=GKJRR8_DSSs

You can take a look at my video here that walks through in details: https://www.youtube.com/watch?v=GKJRR8_DSSs

一般来说:

安卓 -

        public void SetStatusBarColor(System.Drawing.Color color, bool darkStatusBarTint)
        {
            if (Build.VERSION.SdkInt < Android.OS.BuildVersionCodes.Lollipop)
                return;

            var activity = Platform.CurrentActivity;
            var window = activity.Window;
            window.AddFlags(Android.Views.WindowManagerFlags.DrawsSystemBarBackgrounds);
            window.ClearFlags(Android.Views.WindowManagerFlags.TranslucentStatus);
            window.SetStatusBarColor(color.ToPlatformColor());

            if (Build.VERSION.SdkInt >= Android.OS.BuildVersionCodes.M)
            {
                var flag = (Android.Views.StatusBarVisibility)Android.Views.SystemUiFlags.LightStatusBar;
                window.DecorView.SystemUiVisibility = darkStatusBarTint ? flag : 0;
            }
        }

iOS:

 public void SetStatusBarColor(System.Drawing.Color color, bool darkStatusBarTint)
        {
            if (UIDevice.CurrentDevice.CheckSystemVersion(13, 0))
            {
                var statusBar = new UIView(UIApplication.SharedApplication.KeyWindow.WindowScene.StatusBarManager.StatusBarFrame);
                statusBar.BackgroundColor = color.ToPlatformColor();
                UIApplication.SharedApplication.KeyWindow.AddSubview(statusBar);
            }
            else
            {
                var statusBar = UIApplication.SharedApplication.ValueForKey(new NSString("statusBar")) as UIView;
                if (statusBar.RespondsToSelector(new ObjCRuntime.Selector("setBackgroundColor:")))
                {
                    statusBar.BackgroundColor = color.ToPlatformColor();
                }
            }
            var style = darkStatusBarTint ? UIStatusBarStyle.DarkContent : UIStatusBarStyle.LightContent;
            UIApplication.SharedApplication.SetStatusBarStyle(style, false);
            Xamarin.Essentials.Platform.GetCurrentUIViewController()?.SetNeedsStatusBarAppearanceUpdate();
        }

这篇关于如何使用 AppShell 更改 Xamarin Forms ContentPage 中的状态栏?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆