【问题标题】:Xamarin Forms - TabbedPage platform specific xaml code to code-behindXamarin Forms - TabbedPage 平台特定的 xaml 代码到代码隐藏
【发布时间】:2021-03-08 00:48:15
【问题描述】:

我正在尝试更改标签页中图标的颜色。我能够做到这一点,只需投到android:TabbedPage.BarItemColor="Red"

但图标上的颜色必须根据当前显示的页面进行更改。

我设法在我的代码隐藏中更改了在页面上触发的这个方法:

Tabbepage.xaml.cs

private void TabbedPage_CurrentPageChanged(object sender, EventArgs e)
    {
        
        var navigationPage = CurrentPage as NavigationPage;

        var currentPage = navigationPage.CurrentPage;

        if (currentPage.GetType() == typeof(MenuPage))
        {
            Tabbar.BarTextColor = Color.White;
            Tabbar.BarBackgroundColor = Color.FromHex("#004f3d");
        }
        else if (currentPage.GetType() == typeof(HerdList))
        {
            Tabbar.BarTextColor = Color.Black;
            Tabbar.BarBackgroundColor = Color.White;
        }
    }

但我只能弄清楚如何将颜色设置为:

<TabbedPage xmlns="http://xamarin.com/schemas/2014/forms"
            xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
            xmlns:prism="http://prismlibrary.com"
            prism:ViewModelLocator.AutowireViewModel="True"
            x:Class="ChrApp.Views.Tab.BottomTabNavigation"
            xmlns:local="clr-namespace:ChrApp.Views"
            x:Name="Tabbar"
            xmlns:android="clr-namespace:Xamarin.Forms.PlatformConfiguration.AndroidSpecific;assembly=Xamarin.Forms.Core"
            android:TabbedPage.ToolbarPlacement="Bottom"
            android:TabbedPage.BarSelectedItemColor="Green"
            android:TabbedPage.BarItemColor="Red"
            xmlns:CustomRenderer="clr-namespace:ChrApp.CustomRenderer"
            CurrentPageChanged="TabbedPage_CurrentPageChanged"
            >
    <NavigationPage 
        Title="Forside"
                    >
        <x:Arguments>
            <local:MenuPage/>
        </x:Arguments>
        <NavigationPage.IconImageSource>
            <FontImageSource  FontFamily="{StaticResource FontAwesomeSolid}" Glyph="{x:Static CustomRenderer:Icon.House}"/>
        </NavigationPage.IconImageSource>
        
        
    </NavigationPage>
    <NavigationPage 
        Title="Besætning"
                    >
        <x:Arguments>
            <local:HerdList/>
        </x:Arguments>
        <NavigationPage.IconImageSource>
            <FontImageSource  FontFamily="{StaticResource FontAwesomeSolid}" Glyph="{x:Static CustomRenderer:Icon.Pig}"/>
        </NavigationPage.IconImageSource>
    </NavigationPage>


</TabbedPage>

我能否以某种方式将我的代码隐藏中的方法发送到我的视图模型,或者我可以从我的代码隐藏中访问演员表android:TabbedPage.BarItemColor="Red"

提前致谢! ❤

【问题讨论】:

    标签: c# xaml xamarin.forms tabbedpage


    【解决方案1】:

    这就是你可以在后面的代码中做到这一点的方法

    using Xamarin.Forms;
    using Xamarin.Forms.PlatformConfiguration;
    using Xamarin.Forms.PlatformConfiguration.AndroidSpecific;
    using Xamarin.Forms.Xaml;
    using TabbedPage = Xamarin.Forms.TabbedPage;
    
    namespace DummyTestApp.Views
    {
        [XamlCompilation(XamlCompilationOptions.Compile)]
        public partial class TabbedPage1 : TabbedPage
        {
            public TabbedPage1()
            {
                InitializeComponent();
                On<Android>().SetToolbarPlacement(ToolbarPlacement.Bottom).SetBarItemColor(Color.Red).SetBarSelectedItemColor(Color.Green);
            }
        }
    }
    

    P.S.BarSelectedItemColor 现在已过时在 TabbedPage 中使用 SelectedTabColor

    <TabbedPage 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" x:Class="DummyTestApp.Views.TabbedPage1"
                SelectedTabColor="Blue">
        <!--Pages can be added as references or inline-->
        <ContentPage Title="Tab 1" />
        <ContentPage Title="Tab 2" />
        <ContentPage Title="Tab 3" />
    </TabbedPage>
    

    【讨论】:

    • 堆栈溢出的维护是因为这是一本百科全书,像这样的 cmets 没有帮助,也不需要。只需接受答案和/或投票,以供将来参考。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-15
    • 2010-11-03
    • 1970-01-01
    • 1970-01-01
    • 2019-06-07
    相关资源
    最近更新 更多