更改导航页面栏颜色和标题颜色 [英] Change navigation page bar color and title color

查看:129
本文介绍了更改导航页面栏颜色和标题颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我附有一个选项卡式页面,是一个导航页面,我想更改颜色或条形本身以及标题颜色,但出现异常:

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:对象引用未设置为对象的实例.)

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

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

这是我的选项卡式页面xaml代码:

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>

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

Here is my Dashboard.xaml.cs code:

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

推荐答案

使用"CurrentPageChanged"事件,然后您就可以更改导航栏的颜色和标题

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

这篇关于更改导航页面栏颜色和标题颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆