簡體   English   中英

如何在 Xamarin 表單 NavigationPage 中更改背景顏色

[英]How to change backGround color in Xamarin forms NavigationPage

我正在嘗試更改 navigationPage 中 navigationBar 的背景顏色我正在使用以下代碼:

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

我該怎么做?

只需設置 NavigationPage 實例的 BarBackgroundColor 屬性:

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

如果你想在 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>

如果您想使用不同的顏色更改每個頁面上的 NavigationBar BackgroundColor。 您可以對每個頁面/視圖的代碼隱藏執行以下操作。

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。

<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. 在這部分改變你想要的顏色綠色

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM