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

Shi*_*ner 4 c# xamarin xamarin.forms

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

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
    }
   }
  }
Run Code Online (Sandbox Code Playgroud)

我该怎么做?

And*_*per 11

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

new NavigationPage(new LoginPage()) { BarBackgroundColor = Color.Green };
Run Code Online (Sandbox Code Playgroud)


Jah*_*mic 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>
Run Code Online (Sandbox Code Playgroud)