Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Xamarin.forms 如何获得导航栏的背景色?_Xamarin.forms - Fatal编程技术网

Xamarin.forms 如何获得导航栏的背景色?

Xamarin.forms 如何获得导航栏的背景色?,xamarin.forms,Xamarin.forms,在我的Xamarin.Forms应用程序中,我希望我的网格背景色与导航栏的背景色相同,如下所示: BackgroundColor="{StaticResource BarBackgroundColor}" Page main = new MainPage(); Page navigation = new NavigationPage(main) { BarBackgroundColor = Color.Red, BarTextColor = Color.Yellow }; 如

在我的Xamarin.Forms应用程序中,我希望我的
网格
背景色与导航栏的背景色相同,如下所示:

BackgroundColor="{StaticResource BarBackgroundColor}"
Page main = new MainPage();
Page navigation = new NavigationPage(main)
{
    BarBackgroundColor = Color.Red,
    BarTextColor = Color.Yellow
};

如何执行此操作?

您可以通过将当前页面转换为导航页面来检索导航栏的颜色。然后,您可以简单地使用检索到的颜色更改网格的颜色。在页面的OnAppearing override上,使用以下代码获取导航栏颜色:

protected override void OnAppearing()
{
   var navPage = Application.Current.MainPage as NavigationPage;
   if (navPage != null)
   {
      var barColor = navPage.BarBackgroundColor;
   }
   base.OnAppearing();
}
或者,正如前面提到的,您可以在App.xaml中预定义颜色,然后从那里简单地使用它

<Style TargetType="NavigationPage">
       <Setter Property="BarBackgroundColor" Value="Blue"/>
       <Setter Property="BarTextColor" Value="White"/>
</Style>

BarBackgroundProperty是
导航页面
中的附加属性。请注意,它可能会在推送到NavigationPage的每个页面上发生更改

假设您的App.Current.MainPage设置如下:

BackgroundColor="{StaticResource BarBackgroundColor}"
Page main = new MainPage();
Page navigation = new NavigationPage(main)
{
    BarBackgroundColor = Color.Red,
    BarTextColor = Color.Yellow
};
navigation
页面是拥有
BarBackgroundColor
属性的页面。所以如果你想找回它,你应该从那里得到它。我看不出您如何通过
静态资源
实现这一点

我想您可以通过viewmodel上的属性来实现这一点。例如:

public class MyViewModel
{
    ...
    public Color BarBackgroundColor
    {
        get
        {
            return ((NavigationPage)App.Current.MainPage)?.BarBackgroundColor;
        }
    }
    ...
}
要在XAML上使用它(一旦MyViewModel{或它的某种继承}是
BindingContext
或您的页面):


我希望它能有所帮助。

您可以通过将下面的代码放入App.xaml来使用全局样式

<Application.Resources>
        <ResourceDictionary>
            <Color x:Key="NavigationPrimary">#1A237E</Color>
            <Style TargetType="NavigationPage">
                <Setter Property="BarBackgroundColor" Value="{StaticResource NavigationPrimary}" />
                <Setter Property="BarTextColor" Value="White" />
            </Style>
            <Style TargetType="Grid">
               <Setter Property="BackgroundColor" Value="{StaticResource NavigationPrimary}" />
            </Style>
        </ResourceDictionary>
    </Application.Resources>

#1A237E

Application.Current.MainPage
没有属性
BarBackgroundColor
是否将其强制转换到导航页面?另外,页面是否初始化为NavigationPage?