Xamarin表单更改导航栏的背景颜色 [英] Xamarin forms change background color of navigation bar

查看:410
本文介绍了Xamarin表单更改导航栏的背景颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Xamarin.Forms,并尝试更改iOS上导航栏的背景颜色.

I'm using Xamarin.Forms and trying to change the background color of the navigation bar on iOS.

我有一个自定义的导航栏类,该类继承自NavigationPage,具有可绑定的属性和构造函数,用于设置导航栏的颜色.根据我的理解,导航栏在其Xamarin.Forms导航背景上具有默认背景(黑色).我可以使用SetColor()方法设置背景颜色(见下文).但是,它留下一条黑线,这是导航栏(iOS)的背景,如图所示. 图片链接

I have a customized navigation bar class that inherits from NavigationPage, with a bindable property and constructor, which sets the color of the navigation bar. According to my understanding the navigation bar has a default background (black) on top of it Xamarin.Forms navigation background. I'm able to set the background color with the SetColor() method (see below). However, it leaves a black line, which is the background of the navigation bar (iOS) as shown in the pic. Picture Link

现在,我正在尝试将iOS导航栏的背景色设置为白色或透明.我花了很多时间,但没有任何效果.有人可以协助您将背景设置为白色.

Now, I'm trying to set the iOS navigation bar background color to white or transparent. Ive spent a lot of time but nothing worked. Could someone assist how to set the background to white.

//PCL class
public class CustomNavigationalPage : NavigationPage
{
    public  static readonly BindableProperty BarBgColorProperty = 
        BindableProperty.
        Create<CustomNavigationalPage, UIColor>  
            (p => p.BarBackgroundColorR, null);

    public UIColor BarBackgroundColorR
    {
        get { return (UIColor)base.GetValue (BarBgColorProperty); }
        set { base.SetValue (BarBgColorProperty, value); }
    }

    public NavigationalPageCustomized() : base()
    {
        SetColor();
    }

    void SetColor()
    {
        BarBackgroundColor = Color.Transparent; 
        BarTextColor = Color.Blue;
    }
}

导航栏渲染器类:

[assembly: ExportRenderer (typeof (CustomNavigationalPage), typeof (CustomNavigationPageRenderer))]

 namespace project.iOS
 {
     public class CustomNavigationPageRenderer : NavigationRenderer
     {
         public CustomNavigationPageRenderer()
         {
             // UINavigationBar.Appearance.SetBackgroundImage (UIImage.FromFile ("navbg.png"), UIBarMetrics.Default);
         }

         protected override void OnElementChanged (VisualElementChangedEventArgs args)
         {
             base.OnElementChanged (args);

             var nb = (NavigationalPageCustomized) Element;

             if (nb != null) 
             { 
                 nb.BarBackgroundColorR = UIColor.White;
             }
         }
     }
  }

推荐答案

Xamarin.forms的 PCL 中尝试此代码.在 App.xaml.cs的构造函数.

Try this code in your PCL of Xamarin.forms. Change below code in the constructor of App.xaml.cs.

public App()
{
    MainPage = new NavigationPage(new Page1())
    {
        BarBackgroundColor = Color.Gray
    };
}

这篇关于Xamarin表单更改导航栏的背景颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆