【发布时间】:2021-06-19 09:50:11
【问题描述】:
在我的 Xamarin 应用程序中,我在 styles.xml 页面中设置了导航栏(状态栏/顶部栏)的颜色。并且所有页面的导航栏颜色都变为白色。
styles.xml
<?xml version="1.0" encoding="utf-8" ?>
<resources>
<style name="MainTheme" parent="MainTheme.Base">
</style>
<!-- Base theme applied no matter what API -->
<style name="MainTheme.Base" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- colorPrimaryDark is used for the status bar -->
<item name="colorPrimaryDark">#FFFFFF</item>
</style>
</resources>
现在,我只想将单个内容页面中导航栏的颜色更改为黑色。
我按照answer 将代码粘贴到内容页面中,但颜色没有改变。
((NavigationPage)Application.Current.MainPage).BarBackgroundColor = Color.Black;
((NavigationPage)Application.Current.MainPage).BarTextColor = Color.Black;
更新
【问题讨论】:
-
您好,您尝试的代码是正确的。我想可能是你放错了代码。您能否重新检查一次重新分配 BarBackgroundColor 颜色的位置。您需要在需要修改导航栏颜色的屏幕的构造函数中重新分配颜色。
-
实际上我在
MainActivity.cs页面中使用了Window.SetStatusBarColor(Android.Graphics.Color.White);.. 我删除了它并再次尝试上面的代码和答案中的代码(下面).. 没有任何效果.. -
@Karamazov 例如,您想从 contentpage1 导航到 contentpage2,因此您在 contentpage2
OnAppearing()上添加代码,然后在 contentpage2OnDisappearing()上返回默认导航栏颜色。 -
是的..我想要这个..我尝试了下面答案中的代码..但它没有做任何改变..
-
@Karamazov 将这些代码添加到 contentpage 2
private Color barColor; protected override void OnAppearing() { base.OnAppearing(); var navigationPage = Application.Current.MainPage as NavigationPage; barColor = navigationPage.BackgroundColor; navigationPage.BarBackgroundColor = Color.Black; }
标签: c# .net xamarin xamarin.forms