如何在 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。您可以在每个 page/view.
的代码隐藏上执行以下操作
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>
- 基本上去App.xaml
- 将此代码粘贴到此处
- 在这部分改变你想要的颜色
绿色
我正在尝试更改 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。您可以在每个 page/view.
的代码隐藏上执行以下操作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>
- 基本上去App.xaml
- 将此代码粘贴到此处
- 在这部分改变你想要的颜色 绿色