我有一个.Net Maui应用程序,它使用Shell TabBar:

<TabBar>
    <Tab Title="Home" Icon="{StaticResource IconHome}">
        <ShellContent ContentTemplate="{DataTemplate local:MainPage}" />
    </Tab>
    <Tab Title="Coverage&#10;Calculator" Icon="{StaticResource IconCalculator}" >
        <ShellContent ContentTemplate="{DataTemplate calculator:CoverageCalculatorPage}" />
    </Tab>
    <Tab Title="Distributor&#10;Locator" Icon="{StaticResource IconLocator}">
        <ShellContent ContentTemplate="{DataTemplate locator:DistributorsLocatorPage}" />
    </Tab>
    <Tab Title="Scan&#10;QR Code" Icon="{StaticResource IconQrScanner}">
        <ShellContent ContentTemplate="{DataTemplate qrScanner:QrScannerPage}" />
    </Tab>        
    <Tab Title="More" Icon="{StaticResource IconMore}">
        <ShellContent ContentTemplate="{DataTemplate more:MoreFeaturesPage}" />
    </Tab>
</TabBar>

图标 colored颜色 在Styles.xaml中设置如下:

        <Setter Property="Shell.TabBarUnselectedColor" Value="{AppThemeBinding Light={StaticResource LaticreteColor}, Dark={StaticResource LaticreteColor}}" />

下面是选项卡栏的外观:

enter image description here

因为我的标签需要多行文本,所以我为Android应用了一个定制的呈现器:

internal class LaticreteShellRenderer : ShellRenderer
{
    public LaticreteShellRenderer(Context context) : base(context) { }
    protected override IShellBottomNavViewAppearanceTracker CreateBottomNavViewAppearanceTracker(ShellItem shellItem)
    {
        return new LaticreteTabLayout();
    }
    public class LaticreteTabLayout : Java.Lang.Object, IShellBottomNavViewAppearanceTracker
    {
        public void ResetAppearance(BottomNavigationView bottomView)
        {
            throw new NotImplementedException();
        }

        public override void SetAppearance(BottomNavigationView bottomView, IShellAppearanceElement appearance)
        {
            bottomView.SetMinimumHeight(180);

            var bottomNavView = bottomView.GetChildAt(0) as BottomNavigationMenuView;

            for (int i = 0; i < bottomNavView.ChildCount; i++)
            {
                var item = bottomNavView.GetChildAt(i) as BottomNavigationItemView;
                var itemTitle = item.GetChildAt(1);
                TextView smallTextView = (TextView)((BaselineLayout)itemTitle).GetChildAt(0);
                TextView largeTextView = (TextView)((BaselineLayout)itemTitle).GetChildAt(1);
                smallTextView.SetLines(2);
                largeTextView.SetLines(2);
            }
        }

这将重置选项卡的 colored颜色 :

enter image description here

我发现我可以这样更改自定义渲染器中的文本 colored颜色 :

                    smallTextView.SetTextColor(global::Android.Graphics.Color.Brown);
                largeTextView.SetTextColor(global::Android.Graphics.Color.Brown);

但我仍然不知道如何设置图标 colored颜色 ,以及如何在App.xaml或Colors.xaml中引用我的自定义 colored颜色 集.

请告知为什么重置选项卡栏未 Select 的 colored颜色 ,以及如何修复此问题.

推荐答案

您设置的附加属性TabBarUnselectedColor不会应用,因为您已经重写了负责执行此操作的方法SetAppearance().

只需在开始时从基类调用该方法即可解决此问题,如下所示:

public void SetAppearance(BottomNavigationView bottomView, IShellAppearanceElement appearance)
{
    base.SetAppearance(bottomView, appearance);
    bottomView.SetMinimumHeight(180);
    ...
 }

帖子主题:Re:Колибри

找不到合适的方法来覆盖"

internal class LaticreteShellRenderer : ShellRenderer
{
    public LaticreteShellRenderer(Context context) : base(context) { }

    protected override IShellBottomNavViewAppearanceTracker CreateBottomNavViewAppearanceTracker(ShellItem shellItem)
    {
        return new LaticreteTabLayout(this, shellItem);
    }
}

    public class LaticreteTabLayout : ShellBottomNavViewAppearanceTracker
    {

    public LaticreteTabLayout (IShellContext shellContext, ShellItem shellItem) : base(shellContext, shellItem)
    {
    }
        public override void SetAppearance(BottomNavigationView bottomView, IShellAppearanceElement appearance)
        {
            base.SetAppearance(bottomView, appearance);

            bottomView.SetMinimumHeight(180);

            var bottomNavView = bottomView.GetChildAt(0) as BottomNavigationMenuView;

            for (int i = 0; i < bottomNavView.ChildCount; i++)
            {
                var item = bottomNavView.GetChildAt(i) as BottomNavigationItemView;
                var itemTitle = item.GetChildAt(1);
                TextView smallTextView = (TextView)((BaselineLayout)itemTitle).GetChildAt(0);
                TextView largeTextView = (TextView)((BaselineLayout)itemTitle).GetChildAt(1);
                smallTextView.SetLines(2);
                largeTextView.SetLines(2);
            }
        }
    }

Csharp相关问答推荐

如何在Shell中创建WinUI 3图形界面?

为什么这个Reflection. Emit代码会导致一个DDL ViolationException?

如何循环遍历XML文档 node 以使用XSLT存储值

如何注册接口类型,类型<>

.NET 8 Web-API返回空列表

Blazor服务器端的身份验证角色

S能够用DATETIME来计算,这有什么错呢?

需要在重新启动ApplicartionPool或IIS后启动/唤醒API的帮助

用C#调用由缓冲区指针参数组成的C API

使用Orleans进行的单元测试找不到接口的实现

使用可信第三方的Iext8.Net pdf签名

从VS调试器而不是测试资源管理器运行的调试NUnitDotNet测试

如何向事件添加成员

C#动态设置ServerReport报表参数

在C#.NET项目中启动时,如何等待提升的PowerShell进程退出?

Postgres ENUM类型在第一次运行时对Dapper不可见

.NET8Blazor-为什么Rapzor渲染在for循环之后显示?

删除MudRadio时,MudRadioGroup未 Select 正确的MudRadio

嵌套Blazor组件内的验证

除非首先访问使用的终结点,否则本地API上的终结点不起作用