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