【问题标题】:How to change backGround color in Xamarin forms NavigationPage如何在 Xamarin 表单 NavigationPage 中更改背景颜色
【发布时间】:2015-02-20 14:31:16
【问题描述】:

我正在尝试更改 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
    }
   }
  }

我该怎么做?

【问题讨论】:

    标签: c# xamarin xamarin.forms


    【解决方案1】:

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

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

    【讨论】:

      【解决方案2】:

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

      【讨论】:

      • @x-rw 没有太多信息很难说。您是否将其包含在您的 app.xml 文件中?如果仍然有问题,我建议打开一个单独的问题。
      • 太棒了!这无需各种自定义即可工作
      【解决方案3】:

      如果您想将每个页面上的 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。

      【讨论】:

        【解决方案4】:
        <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. 在这部分改变你想要的颜色 绿色

        【讨论】:

          猜你喜欢
          • 2015-03-16
          • 2018-02-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2021-01-26
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多