【问题标题】:Change navigation page bar color and title color更改导航页栏颜色和标题颜色
【发布时间】:2019-03-08 22:30:33
【问题描述】:

我有一个选项卡式页面附加是一个导航页面我想更改颜色或栏本身和标题颜色,但我得到一个例外:

(System.NullReferenceException:对象引用未设置为对象的实例。)

如何更改导航栏的颜色和标题颜色?

这是我的标签页 xaml 代码:

<TabbedPage xmlns="http://xamarin.com/schemas/2014/forms"
         xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
        xmlns:local="clr-namespace:TBSApp.View"
        x:Class="TBSApp.Tabbed_Page.TabPage"
        NavigationPage.HasNavigationBar="False"
        xmlns:android="clr-namespace:Xamarin.Forms.PlatformConfiguration.AndroidSpecific;assembly=Xamarin.Forms.Core"
        android:TabbedPage.ToolbarPlacement="Bottom"
        BarBackgroundColor="#fff"
        android:TabbedPage.BarItemColor="#bbbbbb"
        android:TabbedPage.BarSelectedItemColor="#fc5661">
<NavigationPage Title="Dashboard" Icon="home.png">
    <x:Arguments>
        <local:Dashboard />
    </x:Arguments>
</NavigationPage>

这是我的 Dashboard.xaml.cs 代码:

((NavigationPage)Application.Current.MainPage).BarBackgroundColor = Color.FromHex("#fff");
((NavigationPage)Application.Current.MainPage).BarTextColor = Color.FromHex("#203341");

【问题讨论】:

  • 此代码是否 ((NavigationPage)Application.Current.MainPage).BarBackgroundColor = Color.FromHex("#fff");工作吗?
  • @AndroDevil nope
  • 你解决了吗?
  • @AndroDevil 是的,请参阅下面的答案
  • 好的。很高兴知道

标签: xamarin xamarin.forms xamarin.android


【解决方案1】:

将此添加到您的 App.xaml

<Application.Resources>
    <ResourceDictionary>
        <Style TargetType="NavigationPage">
            <Setter Property="BarBackgroundColor" Value="#fff"/>
            <Setter Property="BarTextColor" Value="#203341"/>
        </Style>
    </ResourceDictionary>
</Application.Resources>

【讨论】:

  • @LucasZhang-MSFT 我不能等2天才能标记它
【解决方案2】:

使用“CurrentPageChanged”事件可以改变导航栏的颜色和标题

这里是代码 sn-p 希望这能工作

public Dashboard()
{
    InitializeComponent();
    CurrentPageChanged += ChangeTitle;
} 

private void ChangeTitle(object sender, System.EventArgs e)
{
    ((NavigationPage)Parent).BarBackgroundColor = Color.White;
    BarBackgroundColor = Device.RuntimePlatform == Color.White;
}

【讨论】: