C# Xamarin表单更改导航栏的背景色
我正在使用Xamarin.Forms并试图更改iOS上导航栏的背景色 我有一个自定义的导航栏类,它继承自NavigationPage,具有可绑定的属性和构造函数,用于设置导航栏的颜色。据我所知,导航栏上有一个默认背景(黑色),Xamarin.Forms导航背景。我可以使用SetColor()方法设置背景色(见下文)。但是,它会留下一条黑线,这是导航栏(iOS)的背景,如图所示 现在,我正在尝试将iOS导航栏的背景色设置为白色或透明。我花了很多时间,但什么也没用。有人能帮我把背景调成白色吗C# Xamarin表单更改导航栏的背景色,c#,xamarin.forms,C#,Xamarin.forms,我正在使用Xamarin.Forms并试图更改iOS上导航栏的背景色 我有一个自定义的导航栏类,它继承自NavigationPage,具有可绑定的属性和构造函数,用于设置导航栏的颜色。据我所知,导航栏上有一个默认背景(黑色),Xamarin.Forms导航背景。我可以使用SetColor()方法设置背景色(见下文)。但是,它会留下一条黑线,这是导航栏(iOS)的背景,如图所示 现在,我正在尝试将iOS导航栏的背景色设置为白色或透明。我花了很多时间,但什么也没用。有人能帮我把背景调成白色吗 //P
//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;
}
}
这过去需要自定义渲染器,但在XF 1.3中不再需要。NavigationPage现在有BarBackgroundColor和BarTextColor属性,它们似乎工作得很好。不幸的是,如果没有自定义的渲染器(我已经找到),就无法更改字体。请尝试以下代码。祝你好运
[assembly: ExportRenderer (typeof (CustomNavigationalPage), typeof (CustomNavigationPageRenderer))]
namespace project.iOS
{
public class CustomNavigationPageRenderer : NavigationRenderer
{
public CustomNavigationPageRenderer()
{
}
public override void ViewDidLoad ()
{
base.ViewDidLoad ();
//Background image
this.NavigationBar.BarTintColor = UIColor.FromPatternImage (UIImage.FromFile ("AnyResourceImage.png"));
//Your desire color
this.NavigationBar.BarTintColor = UIColor.Red;
//Right item color
this.NavigationBar.TopItem.RightBarButtonItem.TintColor = UIColor.FromPatternImage (UIImage.FromFile ("AnyResourceImage.png"));
//Left item color
this.NavigationBar.TopItem.LeftBarButtonItem.TintColor = UIColor.Black;
}
}
}
//Note : Please remove any background color you set in forms shared or pcl project. Hint in this class > CustomNavigationalPage
NavigationController.NavigationBar.TitleTextAttributes=new UIStringAttributes(){ForegroundColor=UIColor.White} 在Xamarin.forms的PCL中尝试此代码。在 App.xaml.cs的构造函数
public App()
{
MainPage = new NavigationPage(new Page1())
{
BarBackgroundColor = Color.Gray
};
}
对我来说,这非常有效:
(App.Current.MainPage as NavigationPage).BarBackgroundColor = Color.FromHex("#4388CC");
我已将此代码放入页面的ViewModel的构造函数中
希望这也适用于您。您可以在global App.xaml文件中进行设置
<Style TargetType="NavigationPage">
<Setter Property="BarBackgroundColor" Value="Blue"/>
<Setter Property="BarTextColor" Value="White"/>
</Style>
更改为您自己的颜色我在帖子中提到,我可以设置导航栏颜色,也可以显示在图片中。问题iOS有自己的背景色(默认为黑色)。我正在尝试将此颜色设置为白色,因此它不会像picBg中那样留下任何线条img和按钮颜色设置对我有效,但是,bg颜色或条带着色颜色仍会留下线条,如中所示。我正试图得到一个像skype iphone应用程序一样的背景色,我相信这是一个底线。我不太清楚怎么去掉这个
<Style TargetType="NavigationPage">
<Setter Property="BarBackgroundColor" Value="Blue"/>
<Setter Property="BarTextColor" Value="White"/>
</Style>