Xamarin.Forms : change dynamically BarBackgroundColor

Marcel Delhaye 26 Reputation points
2024-07-04T17:47:17.02+00:00

In a Xamarin.Forms program, how to dynamically change the BarBackgroundColor? I already changed the app.xaml file like this:

<Application.Resources>
    <ResourceDictionary>
        <!--Global Styles-->
        <Color x:Key="NavigationPrimary">#2196F3</Color>
        <Style TargetType="NavigationPage">
            <Setter Property="BarBackgroundColor" Value="DarkOliveGreen" />
            <Setter Property="BarTextColor" Value="White" />
        </Style>
    </ResourceDictionary>
</Application.Resources>

But the color changes are not dynamic: they are read when the app is started, then can no longer be modified.

This works by making the following changes in the app.xaml.cs file:

            DB_ParametreList = dbparametre.GetAllParametre();
            parametres = DB_ParametreList[0];
            if (parametres.WallpaperName == "wallpapers_4.png")
            {
                MainPage = new NavigationPage(new ListeAchatsPage())
                {
                    BarBackgroundColor = Color.FromHex("#705502"),
                    BarTextColor = Color.White,
                };
            }
            else if (parametres.WallpaperName == "wallpapers_3.png")
            {
                MainPage = new NavigationPage(new ListeAchatsPage())
                {
                    BarBackgroundColor = Color.FromHex("#09506B"),
                    BarTextColor = Color.White,
                };
            }
            else
            {
                MainPage = new NavigationPage(new ListeAchatsPage())
                {
                    BarBackgroundColor = Color.DarkOliveGreen,
                    BarTextColor = Color.White,
                };
            }

The problem comes from the MainPage which cannot be a NavigationPage because I have to display the icon of a Hambuger menu. Please help me to resolve this issue.

Xamarin
Xamarin
A Microsoft open-source app platform for building Android and iOS apps with .NET and C#.
5,363 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Wenyan Zhang (Shanghai Wicresoft Co,.Ltd.) 32,386 Reputation points Microsoft Vendor
    2024-07-05T02:31:49.9466667+00:00

    Hello,

    Xamarin support has been ended, please upgrade Xamarin projects to .NE.

    See Upgrade from Xamarin to .NET & .NET MAUI

    Xamarin official support policy | .NET (microsoft.com)

    Best Regards,

    Wenyan Zhang


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.