Change navigation page bar color and title color

2019-08-29 08:39发布

I have a Tabbed page attached is a Navigation page I want to change the color or the bar itself and the title color but I am getting a exception:

(System.NullReferenceException: Object reference not set to an instance of an object.)

How can I change the color of my navigation bar and title color?

Here is my tabbed page xaml code:

<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>

Here is my Dashboard.xaml.cs code:

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

2条回答
女痞
2楼-- · 2019-08-29 09:15

Add this to your App.xaml

<Application.Resources>
    <ResourceDictionary>
        <Style TargetType="NavigationPage">
            <Setter Property="BarBackgroundColor" Value="#fff"/>
            <Setter Property="BarTextColor" Value="#203341"/>
        </Style>
    </ResourceDictionary>
</Application.Resources>
查看更多
迷人小祖宗
3楼-- · 2019-08-29 09:18

Use "CurrentPageChanged" event then you can able to change the colour and title of the navigation bar

Here is code snippet hope this will work

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

private void ChangeTitle(object sender, System.EventArgs e)
{
    ((NavigationPage)Parent).BarBackgroundColor = Color.White;
    BarBackgroundColor = Device.RuntimePlatform == Color.White;
}
查看更多
登录 后发表回答