我正在搜索,但是无法从我的可移植代码中找到是否可以更改每个平台的StatusBar颜色?(用于Android, iOS & WinPhone 8.1
)
public App()
{
// Change the StatusBar color
MainPage = new MainPageUser();
}
几年后,我将重新解决此问题,因为即使我的答案被接受为正确答案,但我的答案仍然是错误的。我现在已经解决了。
我误读了要更改导航栏的问题,或者当时它在Android中的工作方式有所不同。
我认为至少这是一个更好的答案,应该更好地帮助更改Android和iOS中导航栏的颜色。
将此代码添加到您的Xamarin.Forms项目
public interface IStatusBarPlatformSpecific
{
void SetStatusBarColor(Color color);
}
将该类添加到您的Android项目中
[assembly: Dependency(typeof(MyDemo.Droid.CustomRenderers.Statusbar))]
namespace MyDemo.Droid.CustomRenderers
{
public class Statusbar : IStatusBarPlatformSpecific
{
public Statusbar()
{
}
public void SetStatusBarColor(Color color)
{
// The SetStatusBarcolor is new since API 21
if (Build.VERSION.SdkInt >= BuildVersionCodes.Lollipop)
{
var androidColor = color.AddLuminosity(-0.1).ToAndroid();
//Just use the plugin
CrossCurrentActivity.Current.Activity.Window.SetStatusBarColor(androidColor);
}
else
{
// Here you will just have to set your
// color in styles.xml file as shown below.
}
}
}
}
将此CurrentActivityPlugin添加到您的项目中
现在,您可以像这样更改Forms项目中的颜色
var statusbar = DependencyService.Get<IStatusBarPlatformSpecific>();
statusbar.SetStatusBarColor(Color.Green);
注意上面的else语句。如果您使用的版本号低于21,则需要像这样将颜色硬编码到Android项目中的styles.xml中
<?xml version="1.0" encoding="utf-8" ?>
<resources>
<style name="MainTheme.Base" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="android:statusBarColor">#544054</item>
</style>
</resources>
对于iOS,其类似
将此添加到您的Info.plist(此处有更多文档)
<key>UIStatusBarStyle</key>
<string>UIStatusBarStyleLightContent</string>
<key>UIViewControllerBasedStatusBarAppearance</key>
<false/>
并将此代码添加到StatusBar类的iOS版本中的SetStatusBarColor方法中
UIView statusBar = UIApplication.SharedApplication.ValueForKey(
new NSString("statusBar")) as UIView;
if (statusBar != null && statusBar.RespondsToSelector(
new ObjCRuntime.Selector("setBackgroundColor:")))
{
// change to your desired color
statusBar.BackgroundColor = Color.FromHex("#7f6550").ToUIColor();
}
我写了一篇更详细的博客文章,介绍了如果有人感兴趣的话,如何从Xamarin.Forms设置状态栏的颜色。它仅讨论Android和iOS,但应使您了解如何使用其他平台。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句