在 visual studio 2019 年,如何在每个视图中自定义导航栏(不是全局)
in visual studio 2019, how to customise navigation bar in each view (not globally)
我想为一个视图自定义导航栏(同样,不是全局的,只是为那个视图)。我在互联网上进行了搜索,只找到了如何从 app.xaml 文件中全局自定义它。
我尝试在我的 view.xaml 文件中编写如下代码
<ContentPage.Resources>
<ResourceDictionary>
<Style TargetType="NavigationPage">
<Setter Property="BarBackgroundColor"
Value="{StaticResource blackColor}"/>
<Setter Property="BarTextColor"
Value="{StaticResource whiteColor}"/>
</Style>
</ResourceDictionary>
</ContentPage.Resources>
但它不像我设计其他项目(例如 entries/buttons
时那样起作用
关于 Style
如果 Key
未设置值,则样式将应用于 TargetType 的所有对象。
在您的 App.Xaml 中,设置样式 x:Key
<Style TargetType="ContentPage" x:Key="specialPage">
<Setter Property="BackgroundColor" Value="Green"/>
</Style>
在您的 ContentPage 页面中
<ContentPage
xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
Style="{StaticResource Key=specialPage}"
如您所见,此样式适用于 ContentPage,因为 NavigationApp 只为一个应用程序设置一次,您可以在 ContentPage 样式中设置 NavigationBar 的 属性。
所以,
1) 您可以保持 NavigationPage 静态并使用静态变量来更改 NavigationPage 属性
App.Xaml.cs
public static App CurrentApplication;
public NavigationPage AppNavigationPage;
public App()
{
InitializeComponent();
CurrentApplication = this;
this.AppNavigationPage = new NavigationPage(new MainPage());
MainPage = this.AppNavigationPage;
}
Page.Xaml.cs
public Page1()
{
InitializeComponent();
App.CurrentApplication.AppNavigationPage.BarBackgroundColor = Color.Red;
}
2) 使用动态资源为 NavigationPage 设置样式
NavigationPage.TitleView
此标签将帮助您自定义导航栏。
我想为一个视图自定义导航栏(同样,不是全局的,只是为那个视图)。我在互联网上进行了搜索,只找到了如何从 app.xaml 文件中全局自定义它。
我尝试在我的 view.xaml 文件中编写如下代码
<ContentPage.Resources>
<ResourceDictionary>
<Style TargetType="NavigationPage">
<Setter Property="BarBackgroundColor"
Value="{StaticResource blackColor}"/>
<Setter Property="BarTextColor"
Value="{StaticResource whiteColor}"/>
</Style>
</ResourceDictionary>
</ContentPage.Resources>
但它不像我设计其他项目(例如 entries/buttons
时那样起作用关于 Style
如果 Key
未设置值,则样式将应用于 TargetType 的所有对象。
在您的 App.Xaml 中,设置样式 x:Key
<Style TargetType="ContentPage" x:Key="specialPage">
<Setter Property="BackgroundColor" Value="Green"/>
</Style>
在您的 ContentPage 页面中
<ContentPage
xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
Style="{StaticResource Key=specialPage}"
如您所见,此样式适用于 ContentPage,因为 NavigationApp 只为一个应用程序设置一次,您可以在 ContentPage 样式中设置 NavigationBar 的 属性。
所以,
1) 您可以保持 NavigationPage 静态并使用静态变量来更改 NavigationPage 属性
App.Xaml.cs
public static App CurrentApplication;
public NavigationPage AppNavigationPage;
public App()
{
InitializeComponent();
CurrentApplication = this;
this.AppNavigationPage = new NavigationPage(new MainPage());
MainPage = this.AppNavigationPage;
}
Page.Xaml.cs
public Page1()
{
InitializeComponent();
App.CurrentApplication.AppNavigationPage.BarBackgroundColor = Color.Red;
}
2) 使用动态资源为 NavigationPage 设置样式
NavigationPage.TitleView 此标签将帮助您自定义导航栏。