2

嗨,我是 Xamarin Forms 的新手,我正在使用 BottomBarPage,现在我需要一个包含不同项目的自定义工具栏,正如您在代码中看到的那样,我成功添加了 ToolbarItem,我的疑问是,如何更改工具栏背景颜色?我在 xf:BottomBarPage 中尝试了 x:BackgroundColor 但没有用。有什么建议吗?

<?xml version="1.0" encoding="utf-8" ?>
<xf:BottomBarPage xmlns="http://xamarin.com/schemas/2014/forms"
            xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
            x:Class="MyProject.Views.StartPage"
            xmlns:xf="clr-namespace:BottomBar.XamarinForms;assembly=BottomBar.XamarinForms"
            xmlns:Views="clr-namespace:MyProject.Views;assembly=MyProject"
            x:Name="TabMenu">


    <xf:BottomBarPage.ToolbarItems x:BackgroundColor="#D60000">
        <ToolbarItem Name="User" Order="Primary" Icon="home.png" Text="Item 1" Priority="0" Clicked="User_Clicked"/>
        <!--<ToolbarItem Name="MenuItem2" Order="Primary" Icon="Xamarin.png" Text="Item 2" Priority="1" />-->
    </xf:BottomBarPage.ToolbarItems>

    <xf:BottomBarPage.Children>
        <Views:MainPage 
            ClassId="Home"
            Title="Page1" 
            Icon="Page1.png" 
            xf:BottomBarPageExtensions.TabColor="#D60000"/>
        <Views:MainPage 
            Title="Page2" 
            Icon="Page2.png"  
            xf:BottomBarPageExtensions.TabColor="#D60000"/>
        <Views:Graphs 
            Title="Page3"  
            Icon="Page3.png" 
            xf:BottomBarPageExtensions.TabColor="#D60000"/>
        <Views:MainPage 
            Title="Page4" 
            Icon="Page4.png"
            xf:BottomBarPageExtensions.TabColor="#D60000"/>
        <Views:Info 
            Title="Page5" 
            Icon="Page5.png"
            xf:BottomBarPageExtensions.TabColor="#D60000"/>
    </xf:BottomBarPage.Children>
</xf:BottomBarPage>

蓝色条是我要更改 颜色工具栏的背景颜色

现在使用 TabbedPage,声明是: Tabbed Page

但是顶部栏的颜色仍然是蓝色,我该如何更改? 顶部工具栏

4

3 回答 3

2

正如@fabriBertani 所说,使用官方的 TabbedPage 和底部标签规范,如他分享的文章中所述,检查我在下面分享的代码

<TabbedPage
    xmlns ="http://xamarin.com/schemas/2014/forms"
    xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
    xmlns:test="clr-namespace:Test;assembly=Test"
    xmlns:android="clr-namespace:Xamarin.Forms.PlatformConfiguration.AndroidSpecific;assembly=Xamarin.Forms.Core"
    android:TabbedPage.ToolbarPlacement="Bottom" 
    BarBackgroundColor="Red"
    x:Class="Test.TabbedPage">
    <test:MainPage Title="Page 1" Icon="alarm"/>
    <test:MainPage Title="Page 2" Icon="watch"/>
</TabbedPage>

所以现在你在按钮上有一个标签栏,颜色为 red 。

现在您需要更改NavigationBar的颜色来执行此操作,您需要访问NavigationPage并更改栏颜色。如果您想设置一次,我会在 App.cs 中更改它,如下所示:

   public App()
        {
            InitializeComponent();

            MainPage = new NavigationPage(new MainPage())
            {
                BarBackgroundColor = Color.Red
            };
        }
于 2019-01-10T13:32:18.237 回答
1

在主页中初始化组件后转到您的 App.xaml.cs

MainPage = new NavigationPage(new Login())
{
    BarBackgroundColor = Color.DarkOrange
};

这将更改您的 ToolBarItem 背景颜色。简单!

于 2019-04-15T12:50:13.223 回答
0

考虑到标签@FabriBertani 给了你答案,没有什么可补充的。

考虑到工具栏,Xamarin.Forms 不支持 iOS 工具栏,如果你想写一些完全一样的东西,你可能需要自己做很多工作,最好使用 Xamarin.iOS + Xamarin.Android 在这种情况下。

如果您想要 Xamari.Forms,没有简单的解决方案,需要高技能水平和大量时间,因此几乎没有更好的答案 - 没有人会为您做,我不确定您的技能是否符合要求的水平即使他们这样做了,在这样的事情上投入精力而不是使用替代方法也可能是浪费时间。

于 2019-01-10T14:51:01.907 回答