在 Xamarin 中控制和更改导航栏背景颜色时遇到问题



我一直在尝试更改 Xamarin 窗体中导航栏的背景颜色。我有一个 MainPage.xaml,它在 App.xaml.cs 文件中使用以下代码。这行得通...

public App()
{
InitializeComponent();
MainPage = new NavigationPage(new MainPage())
{
BarBackgroundColor = Color.AliceBlue, // Sets color of Navigation Top Bar
BarTextColor = Color.White // Sets the text color of the Navigation Top Bar
};
}

我遇到的问题是我从一个显示 Lottie 动画 5 秒的启动页面开始,然后导航到主页。当主页出现时,导航栏始终为白色。我从我的 Splash.xml 页面导航到该页面,并在 Splash.xaml.cs 文件中使用以下代码:

public partial class Splash : ContentPage
{
public Splash()
{
InitializeComponent();
}
protected async override void OnAppearing()
{
base.OnAppearing();
await Task.Delay(5000);
await this.Navigation.PushModalAsync(new NavigationPage(new MainPage()));
}
}

我尝试将以下内容放在我的 App.xaml 文件中,但它没有任何效果:

<Application.Resources>
<ResourceDictionary>
<Style TargetType="NavigationPage">
<Setter Property="BarBackgroundColor" Value="AliceBlue"/>
</Style>
</ResourceDictionary>
</Application.Resources>

关于在这种情况下声明和控制导航栏背景颜色而缺少什么的任何想法?

谢谢。

在第一种情况下,它按预期工作。在这一行中:

await this.Navigation.PushModalAsync(new NavigationPage(new MainPage()));

您正在创建一个新NavigationPage因此您以前的代码与它无关,如果您想要相同的效果,则需要再次设置颜色。

考虑到第二种解决方案,我希望它能工作,所以可能你没有正确测试它,或者你有更多的代码覆盖了该行为。

最新更新