简体   繁体   English

Xamarin Forms - TabbedPage 平台特定 xaml 代码到代码隐藏

[英]Xamarin Forms - TabbedPage platform specific xaml code to code-behind

Im trying to change the color on the icons in a tabpage.我试图更改标签页中图标的颜色。 Im able to do that, with a cast to android:TabbedPage.BarItemColor="Red" .我能够做到这一点,只需转换为android:TabbedPage.BarItemColor="Red"

But the color on the icons have to change, based on the page its currently showing.但是图标上的颜色必须根据当前显示的页面进行更改。

Ive managed to make this method that triggers on page changed in my codebehind:我设法在我的代码隐藏中更改了在页面上触发的这种方法:

Tabbepage.xaml.cs 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;
        }
    }

But i can only figure out how to set the color as this:但我只能弄清楚如何将颜色设置为:

<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>

Can i somehow send the method in my codebehind to my viewmodel, or can i access the cast android:TabbedPage.BarItemColor="Red" from my codebehind?我可以以某种方式将我的代码隐藏中的方法发送到我的视图模型,或者我可以从我的代码隐藏中访问演员android:TabbedPage.BarItemColor="Red"吗?

Thanks in advance!提前致谢!

This is how you can do it in the code behind这就是你如何在后面的代码中做到这一点

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);
        }
    }
}

PS : BarSelectedItemColor is now obsolete use SelectedTabColor in TabbedPage PSBarSelectedItemColor现在已过时,在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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM