Xamarin表单 - 如果选项卡处于活动状态,则更改选项卡图标

时间:2016-06-20 09:31:48

标签: android xamarin.forms

我需要使用Xamarin Forms 2更改Android中所选标签的图标,例如LinkedIn应用。

这是我的代码,但它不起作用:

private void LoadTab(string title, string selectedIcon, string unselectedIcon)
{
    var tab = new ContentPage();
    if (Device.OS == TargetPlatform.iOS) tab.Title = title;
    tab.Icon = new FileImageSource() { File = selectedIcon };
    tab.Appearing += (s, a) => tab.Icon = new FileImageSource() { File = selectedIcon };
    tab.Disappearing += (s, a) => tab.Icon = new FileImageSource() { File = unselectedIcon };
    Children.Add(tab);
}

我也尝试过使用PagesChanged事件但结果是一样的。

这是我更改页面时记录器写的内容:

W/FragmentManager(30781): moveToState: Fragment state for FragmentContainer{e26ef3e #3 id=0x2 android:switcher:2:565409837} not updated inline; expected state 3 found 2

1 个答案:

答案 0 :(得分:4)

您应该创建selector drawable xml,而不是仅使用png文件作为图标。 例如:

<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/selectedIcon" android:state_selected="true"></item>
<item android:drawable="@drawable/selectedIcon" android:state_pressed="true"></item>
<item android:drawable="@drawable/unselectedIcon"></item>

将它放在android项目中的drawable文件夹中。

然后: tab.Icon = new FileImageSource() { File = iconSelector };

您不需要检查条件

相关问题