0

我有一个 xamarin 表单应用程序,并且能够更改导航栏的颜色。如何跨平台更改状态栏颜色?在下图中,您可以看到绿色的导航页栏背景颜色。上面是蓝色的,我想改变它的颜色。如何以 xamarin 形式实现此跨平台?

在此处输入图像描述

4

2 回答 2

2

您可以使用DependencyService

在共享项目中,定义接口

public interface IStatusBarColor
{
    void SetColoredStatusBar(string color);

}

在安卓中

首先,从 nuegt安装插件CurrentActivity ,检查https://github.com/jamesmontemagno/CurrentActivityPlugin


using Android.OS;
using Android.Views;
using App24.Droid;
using App24;
using Xamarin.Forms;
using Plugin.CurrentActivity;

[assembly: Dependency(typeof(SetStatusBarColorImplemention))]
namespace App24.Droid
{
    public class SetStatusBarColorImplemention : IStatusBarColor
    {
        public SetStatusBarColorImplemention()
        {
        }

        public void SetColoredStatusBar(string color)
        {
            if (Build.VERSION.SdkInt >= BuildVersionCodes.M)
            {
                Device.BeginInvokeOnMainThread(() =>
                {
                    var currentWindow = GetCurrentWindow();
                    currentWindow.DecorView.SystemUiVisibility = 0;
                    currentWindow.SetStatusBarColor(Android.Graphics.Color.ParseColor(color));
                });
            }
        }

        Window GetCurrentWindow()
        {
            var window = CrossCurrentActivity.Current.Activity.Window;

            window.ClearFlags(WindowManagerFlags.TranslucentStatus);

            window.AddFlags(WindowManagerFlags.DrawsSystemBarBackgrounds);

            return window;
        }
    }
}

在 iOS 中


using App24;
using App24.iOS;
using Foundation;
using UIKit;
using Xamarin.Forms;
using Xamarin.Forms.Platform.iOS;
using ObjCRuntime;
using CoreGraphics;
[assembly: Dependency(typeof(SetStatusBarColorImplemention))]
namespace App24.iOS
{
    public class SetStatusBarColorImplemention : IStatusBarColor
    {
        public void SetColoredStatusBar(string hexColor)
        {


            if(UIDevice.CurrentDevice.CheckSystemVersion(13, 0))
            {
                UIWindow window = UIApplication.SharedApplication.KeyWindow;
                UIView view = new UIView(window.WindowScene.StatusBarManager.StatusBarFrame);
                window.AddSubview(view);
                Device.BeginInvokeOnMainThread(() =>
                {

                    if (view.RespondsToSelector(new Selector("setBackgroundColor:")))
                    {
                        view.BackgroundColor = Color.FromHex(hexColor).ToUIColor();
                    }

                    UIApplication.SharedApplication.SetStatusBarStyle(UIStatusBarStyle.LightContent, false);
                    topViewControllerWithRootViewController(UIApplication.SharedApplication.KeyWindow.RootViewController).SetNeedsStatusBarAppearanceUpdate();
                });

            }


            else
            {
                Device.BeginInvokeOnMainThread(() =>
                {
                    UIView statusBar = UIApplication.SharedApplication.ValueForKey(new NSString("statusBar")) as UIView;
                    if (statusBar.RespondsToSelector(new Selector("setBackgroundColor:")))
                    {
                        statusBar.BackgroundColor = Color.FromHex(hexColor).ToUIColor();
                    }
                    UIApplication.SharedApplication.SetStatusBarStyle(UIStatusBarStyle.LightContent, false);
                    topViewControllerWithRootViewController(UIApplication.SharedApplication.KeyWindow.RootViewController).SetNeedsStatusBarAppearanceUpdate();
                });
            }


        }

        UIViewController topViewControllerWithRootViewController(UIViewController rootViewController)
        {
            if (rootViewController is UITabBarController)
            {
                UITabBarController tabBarController = (UITabBarController)rootViewController;
                return topViewControllerWithRootViewController(tabBarController.SelectedViewController);
            }
            else if (rootViewController is UINavigationController)
            {
                UINavigationController navigationController = (UINavigationController)rootViewController;
                return topViewControllerWithRootViewController(navigationController.VisibleViewController);
            }
            else if (rootViewController.PresentedViewController != null)
            {
                UIViewController presentedViewController = rootViewController.PresentedViewController;
                return topViewControllerWithRootViewController(presentedViewController);
            }
            else
            {
                return rootViewController;
            }
        }

    }
}

现在根据需要调用该行。

DependencyService.Get<IStatusBarColor>().SetColoredStatusBar("#00ff00");  // set the color of bar as green

在此处输入图像描述

于 2020-04-15T11:37:13.927 回答
-2

据我所知,您需要分别在每个平台上设置状态栏颜色。StackOverflow 和谷歌上有很多这样的问题可以帮助你。

对于 Android:在 Resources -> values 中检查你的 styles.xml 寻找类似<item name="android:statusBarColor">#000000</item>设置颜色的东西

对于 iOS:在您的 AppDelegate.cs 中查找FinishedLaunsching-Method。您可以使用更改样式UIApplication.SharedApplication.SetStatusBarStyle(UIStatusBarStyle.DarkContent, false);

于 2020-04-15T11:33:49.780 回答