Xamarin Forms control the color/title of the header bar

后端 未结 4 751
孤城傲影
孤城傲影 2021-02-02 00:08

I have the following form created with Xamarin Forms. I have drawn in a red rectangle to highlight the problem area. I need the blue color in the header to be a different colo

相关标签:
4条回答
  • 2021-02-02 00:39

    The BarBackgroundColor is a property of the NavigationPage class:

    public App()
    {
        MainPage = new NavigationPage(new Page1())
        {
            BarBackgroundColor = Color.FromHex("#ff5300"),
            BarTextColor = Color.White,
        }; 
    }
    
    0 讨论(0)
  • 2021-02-02 00:52

    If you want to use one color for all NavigationPage elements you can do it easier. Add global style to app for a NavigationPage

    <?xml version="1.0" encoding="utf-8"?>
    <Application xmlns="http://xamarin.com/schemas/2014/forms" 
        xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" 
        x:Class="My.App">
        <Application.Resources>
            <!-- Application resource dictionary -->
            <ResourceDictionary>
              <!-- Pallete -->     
              <Color x:Key="primary-back-title-color">#4a148c</Color>
              <Color x:Key="primary-title-color">#FFFFFF</Color>
              <!-- Pallete-end -->  
              <Style ApplyToDerivedTypes="true" TargetType="NavigationPage">
                    <Setter Property="BarBackgroundColor" Value="{StaticResource Key=primary-back-title-color}"/>
                    <Setter Property="BarTextColor" Value="{StaticResource Key=primary-title-color}"/>
              </Style>
            </ResourceDictionary>
        </Application.Resources>
    </Application>
    

    Now you can do:

            void OnTappedProfile(object sender, System.EventArgs e)
            {
                Navigation.PushAsync(new Profile());
            }
    

    Where Profile is ContentPage

    0 讨论(0)
  • 2021-02-02 01:02

    Set the BarBackgroundColor of the NavigationPage. You can do something like this (in the most basic example sense):

            var nav = new NavigationPage
            {
                Title = "Detail"
            };
            nav.PushAsync(new ContentPage() { Title = "Home" });
            nav.BarBackgroundColor = Color.MediumPurple;
    
            var mdp = new MasterDetailPage()
            {
                Master = new ContentPage()
                {
                    Title = "Master"
                },
                Detail = nav
            };
            MainPage = mdp;
    

    The title of the ContentPage being presented by the NavigationPage is what will show the title on that bar.

    0 讨论(0)
  • 2021-02-02 01:02

    Do not forget to add

    ToolbarResource = Resource.Layout.Toolbar;
    

    to MainActivity in Android project, I have lost few hours because of this.

    0 讨论(0)
提交回复
热议问题