【问题标题】:Title Bar will not change color标题栏不会改变颜色
【发布时间】:2019-12-06 15:54:30
【问题描述】:

我正在使用 Xamrian forms 4.0,由于某种奇怪的原因,即使我在 colors.xml 中将其设置为黑色,我的标题栏也没有显示为黑色。我正在使用新的 AppShell 应用程序项目,这是他们的问题。

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name="launcher_background">#FFFFFF</color>
    <color name="colorPrimary">#000000</color>
    <color name="colorPrimaryDark">#000000</color>
    <color name="colorAccent">#000000</color>
</resources>

此代码位于标准 xam 表单应用程序外壳模板中。

<Shell.Resources>
    <ResourceDictionary>
        <Color x:Key="NavigationPrimary">#2196F3</Color>
        <Style x:Key="BaseStyle" TargetType="Element">
            <Setter Property="Shell.BackgroundColor" Value="#000000" />
            <Setter Property="Shell.ForegroundColor" Value="White" />
       <Setter Property="Shell.TitleColor" Value="White" />
            <Setter Property="Shell.DisabledColor" Value="#B4FFFFFF" />
            <Setter Property="Shell.UnselectedColor" Value="#95FFFFFF" />
            <Setter Property="Shell.TabBarBackgroundColor" Value="#000000" />
            <Setter Property="Shell.TabBarForegroundColor" Value="White"/>
            <Setter Property="Shell.TabBarUnselectedColor" Value="#95FFFFFF"/>
            <Setter Property="Shell.TabBarTitleColor" Value="White"/>
        </Style>
        <Style TargetType="ShellItem" BasedOn="{StaticResource BaseStyle}" />
    </ResourceDictionary>
</Shell.Resources>

【问题讨论】:

    标签: xamarin.forms


    【解决方案1】:

    改变shell中的action bar,有点复杂,首先我们可以通过在shell.xml中添加BackgroundColor="Green"来设置背景色

    <Shell xmlns="http://xamarin.com/schemas/2014/forms" 
      xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
      xmlns:d="http://xamarin.com/schemas/2014/forms/design"
      xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
      mc:Ignorable="d"
      xmlns:local="clr-namespace:ShellActionBar.Views"
      Title="ShellActionBar"
       BackgroundColor="Green"
       x:Class="ShellActionBar.AppShell">
    

    但是,结果类似于以下屏幕截图。 TabBar 的颜色变为绿色。

    然后,我们应该为 TabBar 创建样式。只是为了测试,我复制了TabBar的默认样式,

      <Shell.Resources>
    <ResourceDictionary>
        <Color x:Key="NavigationPrimary">#2196F3</Color>
        <Style x:Key="BaseStyle" TargetType="Element">
            <Setter Property="Shell.BackgroundColor" Value="{StaticResource NavigationPrimary}" />
            <Setter Property="Shell.ForegroundColor" Value="White" />
            <Setter Property="Shell.TitleColor" Value="White" />
            <Setter Property="Shell.DisabledColor" Value="#B4FFFFFF" />
            <Setter Property="Shell.UnselectedColor" Value="#95FFFFFF" />
            <Setter Property="Shell.TabBarBackgroundColor" Value="{StaticResource NavigationPrimary}" />
            <Setter Property="Shell.TabBarForegroundColor" Value="White"/>
            <Setter Property="Shell.TabBarUnselectedColor" Value="#95FFFFFF"/>
            <Setter Property="Shell.TabBarTitleColor" Value="White"/>
        </Style>
        <Style x:Key="MyBaseStyle" TargetType="Element">
    
            <Setter Property="Shell.ForegroundColor" Value="White" />
            <Setter Property="Shell.TitleColor" Value="White" />
            <Setter Property="Shell.DisabledColor" Value="#B4FFFFFF" />
            <Setter Property="Shell.UnselectedColor" Value="#95FFFFFF" />
            <Setter Property="Shell.TabBarBackgroundColor" Value="{StaticResource NavigationPrimary}" />
            <Setter Property="Shell.TabBarForegroundColor" Value="White"/>
            <Setter Property="Shell.TabBarUnselectedColor" Value="#95FFFFFF"/>
            <Setter Property="Shell.TabBarTitleColor" Value="White"/>
        </Style>
    
        <Style TargetType="ShellItem" BasedOn="{StaticResource BaseStyle}" />
        <Style TargetType="TabBar" BasedOn="{StaticResource MyBaseStyle}" />
    </ResourceDictionary>
    </Shell.Resources>
    
     <!-- Your Pages -->
    <TabBar  >
    <Tab Title="Browse" Icon="tab_feed.png" >
        <ShellContent ContentTemplate="{DataTemplate local:ItemsPage}" />
    </Tab>
    <Tab Title="About" Icon="tab_about.png">
        <ShellContent ContentTemplate="{DataTemplate local:AboutPage}" />
    </Tab>
    </TabBar>
    

    这是运行截图。

    【讨论】:

    • 奇怪的是它似乎并没有为我兑现它。
    • @rogue39你可以下载这个演示github.com/851265601/ShellActionBar,然后在你的安卓设备上运行它,如果它会改变操作栏。
    【解决方案2】:

    改变

    &lt;Style TargetType="ShellItem" BasedOn="{StaticResource BaseStyle}" /&gt;

    到

    &lt;Style TargetType="FlyoutItem" BasedOn="{StaticResource BaseStyle}" /&gt;

    他们似乎没有更新文档。

    【讨论】:

      猜你喜欢
      • 2019-01-31
      • 2013-05-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多