在Xamarin中控制和更改导航栏背景颜色

问题描述 投票:0回答:1

我一直在尝试更改Xamarin Forms中导航栏的背景颜色。我有一个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
        };
    }

我的问题是,我从一个Splash页面开始,该页面显示Lottie动画5秒钟,然后导航到MainPage。当MainPage出现时,导航栏始终只是白色。我从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>

关于在这种情况下为了声明和控制Navigation BarBackgroundColor而缺少的任何想法?

感谢。

c# xamarin
1个回答
0
投票

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

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

您正在创建一个新的NavigationPage,因此先前的代码与之无关,如果您希望获得相同的效果,则需要再次设置颜色。

考虑第二种解决方案,我希望它能起作用,所以可能您没有对其进行正确的测试,或者您有一些替代行为的代码。

© www.soinside.com 2019 - 2024. All rights reserved.