我有一个选项卡页面是一个导航页面我想更改颜色或栏本身和标题颜色,但我得到一个例外:
(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");
将其添加到App.xaml
<Application.Resources>
<ResourceDictionary>
<Style TargetType="NavigationPage">
<Setter Property="BarBackgroundColor" Value="#fff"/>
<Setter Property="BarTextColor" Value="#203341"/>
</Style>
</ResourceDictionary>
</Application.Resources>
使用“CurrentPageChanged”事件,然后您可以更改导航栏的颜色和标题
这是代码片段希望这将工作
public Dashboard()
{
InitializeComponent();
CurrentPageChanged += ChangeTitle;
}
private void ChangeTitle(object sender, System.EventArgs e)
{
((NavigationPage)Parent).BarBackgroundColor = Color.White;
BarBackgroundColor = Device.RuntimePlatform == Color.White;
}