【问题标题】:How to change the Status Bar in Xamarin Forms ContentPage using AppShell?如何使用 AppShell 更改 Xamarin Forms ContentPage 中的状态栏?
【发布时间】:2021-09-07 14:57:16
【问题描述】:

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

    App.MainPage = new AppShell();

那我做

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

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

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

关键是如果我这样做

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

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

我尝试了这些解决方案

没有成功...

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

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

在Android项目中使用。

我做了其他帖子中的步骤,我得到了:

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

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

【问题讨论】:

    标签: xamarin.forms


    【解决方案1】:

    您可以在这里观看我的视频,详细介绍: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();
            }
    

    【讨论】:

      猜你喜欢
      • 2019-06-15
      • 2019-12-31
      • 1970-01-01
      • 2022-01-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多