如何在Xamarin表单NavigationPage中更改backGround颜色

时间:2015-02-20 14:31:17

标签: c# xamarin xamarin.forms

我正在尝试在navigationPage中更改navigationBar的背景颜色我正在使用此代码`使用系统;

using System;
using Xamarin.Forms;
using System.Collections.Generic;
using System.Text;
using System.Linq;
using System.Threading.Tasks;
namespace P
{
public class App : Application
{
    public App ()
    {
 MainPage = new NavigationPage(new LoginPage());
    }
    protected override void OnStart ()
    {
    }
    protected override void OnSleep ()
    {
    }
    protected override void OnResume ()
    {
        // Handle when your app resumes
    }
   }
  }

我该怎么办?

4 个答案:

答案 0 :(得分:11)

只需设置NavigationPage实例的BarBackgroundColor属性:

new NavigationPage(new LoginPage()) { BarBackgroundColor = Color.Green };

答案 1 :(得分:7)

如果你想在xaml中设置一个全局样式,你可以这样做:

<?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>

答案 2 :(得分:0)

如果要在每个页面上更改不同颜色的NavigationBar BackgroundColor。您可以在每个页面/视图的Codebehinds上执行以下操作。

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;
        }
    }
}

适用于Android和iOS。

答案 3 :(得分:0)

<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. 基本上转到App.xaml
  2. 将此代码粘贴在那里
  3. 根据需要更改颜色 绿色