gpt4 book ai didi

xaml - Xamarin forms 选定选项卡的 Shell 自定义图标

转载 作者:行者123 更新时间:2023-12-02 19:49:05 26 4
gpt4 key购买 nike

我正在玩 COOL xamarin shell,但我没有找到更改所选选项卡图标的方法。

<TabBar Route="sections">
<Tab Title="home">
<Tab.Icon>
<FontImageSource FontFamily="{StaticResource AppIcons}" Glyph="{x:Static framework:Icons.HomePage}" />
</Tab.Icon>
<ShellContent ContentTemplate="{DataTemplate home:HomePage}" Route="home" />
</Tab>

目标是仅在选中此选项卡时使用 Icons.HomePageFilled 而不是 Icons.HomePage。相同的逻辑应该适用于其他选项卡。

我想我迷失了在网络上找到的解决方案。他们谈论自定义渲染器(ShellTabLayoutAppearanceTracker)、视觉状态、效果等......但不知道是否可行,理想的解决方案是什么

最佳答案

您需要使用 custom renderer shell 自定义每个平台上的选项卡选择图标。

在 iOS 中,覆盖 CreateTabBarAppearanceTracker 方法:

[assembly: ExportRenderer(typeof(AppShell), typeof(MyShellRenderer))]
namespace App30.iOS
{
public class MyShellRenderer : ShellRenderer
{
protected override IShellSectionRenderer CreateShellSectionRenderer(ShellSection shellSection)
{
var renderer = base.CreateShellSectionRenderer(shellSection);
if (renderer != null)
{

}
return renderer;
}

protected override IShellTabBarAppearanceTracker CreateTabBarAppearanceTracker()
{
return new CustomTabbarAppearance();
}
}

public class CustomTabbarAppearance : IShellTabBarAppearanceTracker
{
public void Dispose()
{

}

public void ResetAppearance(UITabBarController controller)
{

}

public void SetAppearance(UITabBarController controller, ShellAppearance appearance)
{
UITabBar myTabBar = controller.TabBar;

if (myTabBar.Items != null)
{
UITabBarItem itemOne = myTabBar.Items[0];

itemOne.Image = UIImage.FromBundle("tab_about.png");
itemOne.SelectedImage = UIImage.FromBundle("tab_feed.png");


UITabBarItem itemTwo = myTabBar.Items[1];

itemTwo.Image = UIImage.FromBundle("tab_feed.png");
itemTwo.SelectedImage = UIImage.FromBundle("tab_about.png");

//The same logic if you have itemThree, itemFour....
}

}

public void UpdateLayout(UITabBarController controller)
{

}
}
}

在 Android 中,覆盖 CreateBottomNavViewAppearanceTracker 方法:

[assembly: ExportRenderer(typeof(AppShell), typeof(MyShellRenderer))]
namespace App30.Droid
{
public class MyShellRenderer : ShellRenderer
{
public MyShellRenderer(Context context) : base(context)
{
}

protected override IShellBottomNavViewAppearanceTracker CreateBottomNavViewAppearanceTracker(ShellItem shellItem)
{
return new CustomBottomNavAppearance();
}
}

public class CustomBottomNavAppearance : IShellBottomNavViewAppearanceTracker
{
public void Dispose()
{

}

public void ResetAppearance(BottomNavigationView bottomView)
{

}

public void SetAppearance(BottomNavigationView bottomView, ShellAppearance appearance)
{
IMenu myMenu = bottomView.Menu;

IMenuItem myItemOne = myMenu.GetItem(0);

if (myItemOne.IsChecked)
{
myItemOne.SetIcon(Resource.Drawable.tab_about);
}
else
{
myItemOne.SetIcon(Resource.Drawable.tab_feed);
}

//The same logic if you have myItemTwo, myItemThree....

}
}
}

我上传了一个示例项目 here你可以检查它。

关于xaml - Xamarin forms 选定选项卡的 Shell 自定义图标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58635349/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com