是否可以在Xamarin Forms Android中动态更改标签栏选定的项目颜色?

iam*_*hia 8 c# xamarin.android xamarin xamarin.forms

我有一个Xamarin Forms应用程序,它为用户提供了3个主题选项.我希望能够通过单击按钮更改Tabbar背景,所选项目和未选择的项目颜色.在iOS中,我可以使用如下的渲染器执行此操作:

protected override void OnElementChanged(VisualElementChangedEventArgs e)
{
    base.OnElementChanged(e);

    if(e.OldElement != null)
    {
        Xamarin.Forms.Application.Current.PropertyChange -= Current_PropertyChanged;
        return;
    }

    Xamarin.Forms.Application.Current.PropertyChange += Current_PropertyChanged; //subscribe to the App class' built in property changed event
    UpdateTheme();
}

void Current_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
{
    UpdateTheme();
}
Run Code Online (Sandbox Code Playgroud)

在Android中,我知道可以更改这些颜色,styles.xml但这只允许我设置一次颜色.此外,我正在使用ToolbarPlacement="Bottom"我的标签栏在屏幕的底部.

android:TabbedPage.ToolbarPlacement="Bottom"
android:TabbedPage.BarSelectedItemColor="Red"
android:TabbedPage.IsSwipePagingEnabled="False"
Run Code Online (Sandbox Code Playgroud)

我想知道是否有可能通过BarSelectedItemColor点击按钮动态更改.

iam*_*hia 7

我终于通过使用DynamicResource样式来完成这项工作:

由此:

android:TabbedPage.BarSelectedItemColor="Red"
Run Code Online (Sandbox Code Playgroud)

对此:

android:TabbedPage.BarSelectedItemColor="{DynamicResource BarSelectedItemColor"
Run Code Online (Sandbox Code Playgroud)