我想为一个视图自定义导航栏(同样,不是全局的,仅针对那个视图)。我在互联网上搜索,只找到如何从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>
但是,它的工作方式与我使用其他项目(如条目/按钮)时不同。
发布于 2020-02-26 23:58:30
对于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进行样式设计
发布于 2020-02-27 00:13:48
NavigationPage.TitleView此标记将帮助您自定义导航条。
https://stackoverflow.com/questions/60428032
复制