【问题标题】:BarBackgroundColor only changes navbar, not the titlebarBarBackgroundColor 仅更改导航栏,而不更改标题栏
【发布时间】:2020-03-08 08:25:55
【问题描述】:

我刚开始使用 xamarin 表单,现在我正在尝试使页面的标题栏和导航栏都根据您所在的页面更改颜色。我也将工具栏位置设置为底部。

问题是改变颜色只会改变底部导航栏,而不是顶部标题栏。

我的主页(标签页)代码在后面

public partial class MainPage : TabbedPage
{
    public MainPage()
    {
        On<Android>().SetToolbarPlacement(ToolbarPlacement.Bottom);
        InitializeComponent();

        this.BarBackgroundColor = Color.FromHex("#008B8B");
        this.BarTextColor = Color.White;
    }
}

这会将底部的导航栏更改为我想要的颜色,但将顶部标题栏保留为默认的蓝色。

预算和费用是两个通用内容页面(如您所见)

【问题讨论】:

  • 在安卓上吗? IOS?你用的是壳牌吗?您应该发布更多问题的背景。告诉我们您的页面是否实现了导航标题视图,是简单的导航栏吗?
  • @NirmalSubedi 抱歉,它在 android 上,因为它基本上是一个新项目,所以实际上没有太多要发布的内容,但我会尽我所能更新我的帖子
  • 是的,带有屏幕截图的代码,预期与实际输出会有所帮助
  • @PatrickGoode 我真的没有其他东西可以发布...这是一个新的 xamarin 表单项目,我所做的只是上面代码中的内容,它改变了底部栏而不是顶部,没有其他代码我可以发布,因为没有任何其他代码
  • 明白了。你能截屏它的样子吗?

标签: c# xamarin.forms


【解决方案1】:

如果您没有设置页面标题,则更正它将是空白的。为了设置每个页面顶部标题栏的颜色,我为每个标签页子页面使用了导航页面,然后设置了这些导航页面的背景颜色

示例:应用主页的 xaml

 <controls:TabbedPage
xmlns:controls="clr-namespace:bla.Controls" xmlns="http://xamarin.com/schemas/2014/forms"
        xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
        x:Class="bla.Views.HomePage"
        >
<controls:TabbedPage.Children>
    <NavigationPage BarTextColor="White" Title="Home" Icon="home.png"  Padding="0,-12,0,0">

        <x:Arguments>
            <HomePage/>
        </x:Arguments>
    </NavigationPage>

    <NavigationPage Title="Communities" BarTextColor="White" Icon="people_outline.png">
        <x:Arguments>
            <OtherPage />
        </x:Arguments>
    </NavigationPage>
</controls:TabbedPage.Children>

您的 app.xaml:

  MainPage = GetMainPage();

  MainPage.SetValue(NavigationPage.BarBackgroundColorProperty, Color.Green);

  public static Page GetMainPage()
    {
        return new HomePage();  //home page is a tabbed page

     }

【讨论】:

  • 您能再解释一下代码吗?我不确定我是否理解
  • 我用我现在剩下的一点时间更新了我的答案。这有帮助吗?如果是这样,请随时标记为答案。谢谢!
【解决方案2】:

您可以将 App.xaml 中的静态资源用于所有项目解决方案的 NavigationPage 样式。

将此代码粘贴到您的 App.xaml 中

<Application.Resources>
    <Style TargetType="NavigationPage">
        <Setter Property="BarBackgroundColor" Value="Color of Bar Background" />
        <Setter Property="BarTextColor" Value="Color of your text on you Bar" />
    </Style>
<Application.Resources>

这条线可以回答你的问题。

    <Setter Property="BarTextColor" Value="Color of your text on you Bar" />

希望对你有帮助...

【讨论】:

    猜你喜欢
    • 2015-12-24
    • 1970-01-01
    • 1970-01-01
    • 2012-01-14
    • 2017-01-19
    • 2022-01-12
    • 1970-01-01
    • 1970-01-01
    • 2017-01-05
    相关资源
    最近更新 更多