How to change backGround color in Xamarin forms NavigationPage

后端 未结 4 740
北恋
北恋 2021-01-05 15:54

I\'m trying to change the background color of navigationBar in navigationPage I\'m using this code`using System;

using System;
using Xamarin.Forms;
using Sys         


        
相关标签:
4条回答
  • 2021-01-05 16:28
    <Application.Resources>
        
        <ResourceDictionary>
            <!--Global Styles-->
            <Color x:Key="NavigationPrimary">Green</Color>
            <Style TargetType="NavigationPage">
                <Setter Property="BarBackgroundColor" Value="{StaticResource NavigationPrimary}" />
                <Setter Property="BarTextColor" Value="White" />
            </Style>
        </ResourceDictionary>
    </Application.Resources>
    
    1. basically go to App.xaml
    2. paste this code there
    3. change the colour however u want in this part Green
    0 讨论(0)
  • 2021-01-05 16:30

    If you want to setup a global style in xaml, you could something similar to this:

    <?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="Sample.App">
        <Application.Resources>
    
            <ResourceDictionary>
                <Style TargetType="NavigationPage">
                    <Setter Property="BarBackgroundColor" Value="#ff5733"/>
                    <Setter Property="BarTextColor" Value="White"/>
                </Style>
            </ResourceDictionary>
    
        </Application.Resources>
    </Application>
    
    0 讨论(0)
  • 2021-01-05 16:37

    Just set the BarBackgroundColor property of the NavigationPage instance:

    new NavigationPage(new LoginPage()) { BarBackgroundColor = Color.Green };
    
    0 讨论(0)
  • 2021-01-05 16:49

    If you want to change the NavigationBar BackgroundColor on each page with a different color. You can do the following on the Codebehinds of each page/view.

    using Xamarin.Forms;
    using Xamarin.Forms.Xaml;
    
    namespace NewApp.Cross.Views
    {
        [XamlCompilation(XamlCompilationOptions.Compile)]
        public partial class NewView : ContentPage
        {
            public NewView()
            {
                InitializeComponent();
                Title = "PageTitle"
    
                NavigationPage.SetHasBackButton(this, false);
    
                ((NavigationPage)Application.Current.MainPage).BarBackgroundColor = Color.Black;
                ((NavigationPage)Application.Current.MainPage).BarTextColor = Color.OrangeRed;
            }
        }
    }
    

    It works on Android and iOS.

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