Xamarin Forms控制标题栏的颜色/标题

Joh*_*ore 10 xamarin xamarin.forms

我有以下使用Xamarin Forms创建的表单.我画了一个红色矩形来突出问题区域.我需要标题中的蓝色是不同的颜色并显示标题.

在此输入图像描述

这是我想要的近似值.请忽略后面的箭头和汉堡包菜单在右边的事实(顺便说一句,MasterDetail可以在右边和左边有汉堡包吗?).

在此输入图像描述

以下代码是我用来创建它的代码.我在NavigationPage中嵌入了我的MainPage(具有ListView).然后我将MasterDetailPage的Detail页面设置为上述的NavigationPage.在此处设置BackgroundColor属性不起作用.请注意,Title属性也不起作用.

如何更改标题背景的颜色和标题?

        var navPage = new NavigationPage(new MainPage());

        App.Current.MainPage = new MasterDetailPage
        {
            BackgroundColor = Color.FromHex("#174873"),
            Title = "MY DRIVES",
            Master = new MenuPage()
            {
                Title = "Master Page Title"
            },
            Detail = navPage
        };
Run Code Online (Sandbox Code Playgroud)

小智 22

如果要对所有 NavigationPage元素使用一种颜色,则可以更轻松地完成.为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>
Run Code Online (Sandbox Code Playgroud)

现在你可以这样做:

        void OnTappedProfile(object sender, System.EventArgs e)
        {
            Navigation.PushAsync(new Profile());
        }
Run Code Online (Sandbox Code Playgroud)

其中Profile是ContentPage


Pau*_*aul 8

设置BarBackgroundColorNavigationPage.你可以做这样的事情(在最基本的例子意义上):

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

ContentPage呈现NavigationPage的标题是将显示该栏上的标题.


小智 6

BarBackgroundColor 是 NavigationPage 类的一个属性:

public App()
{
    MainPage = new NavigationPage(new Page1())
    {
        BarBackgroundColor = Color.FromHex("#ff5300"),
        BarTextColor = Color.White,
    }; 
}
Run Code Online (Sandbox Code Playgroud)