如何在毛伊岛 android 的标签栏上添加徽章

问题描述 投票:0回答:1

我尝试在 iOS 中添加徽章。我能够添加成功。有人可以在android 上帮忙吗?请注意,我不想使用 shell 导航。

public class CustomTabbedPageHandler : Microsoft.Maui.Controls.Handlers.Compatibility.TabbedRenderer
{
    public void UpdateTabBadges()
    {
        if (Element is MainTabbedPage customTabbedPage)
        {
            // Ensure that ViewController is not null
            if (ViewController is UITabBarController tabBar && tabBar.TabBar != null)
            {
                for (int i = 0; i < customTabbedPage.Children.Count; i++)
                {
                    var badgeCount = customTabbedPage.GetBadgeCount(i);
                    AddBadgeToTab(i, badgeCount);
                }
            }
        }
    }

    public void AddBadgeToTab(int tabIndex, int count)
    {
        if (ViewController is UITabBarController tabBar)
        {
            try
            {
                // Check if the tabBar has items and if the tabIndex is valid
                if (tabBar.TabBar.Items != null && tabBar.TabBar.Items.Length > tabIndex)
                {
                    // Set the badge value
                    tabBar.TabBar.Items[tabIndex].BadgeValue = count > 0 ? count.ToString() : null;
                }
            }
            catch (Exception ex)
            {
                // Optionally log the exception or handle it as needed
                Console.WriteLine($"Error adding badge: {ex.Message}");
            }
        }
    }
}

这里适用于安卓。但获取 tablayout 总是为空。

public class CustomTabbedPageHandler : TabbedViewHandler
{
    public override void UpdateValue(string property)
    {
        base.UpdateValue(property);

        var tabs = Microsoft.Maui.ApplicationModel.Platform.CurrentActivity?.FindViewById<FragmentContainerView>(Microsoft.Maui.Resource.Id
            .navigationlayout_bottomtabs);

        var tabLayout = tabs?.GetChildAt(0);

        if (tabLayout is TabLayout layout)
        {
            ====//===
        }
    }


}

不确定,我如何访问选项卡布局并在那里添加徽章。

android .net maui
1个回答
0
投票

可以使用平台原生代码进行修改。

TabbedPage.Android.cs的源码中,我们可以先获取

TabbedPageManager
的实例,而从TabbedPageManager的源码中,我们可以使用
Reflection
来获取
TabbedPageManager
(因为它是内部类)和
TabLayout

然后我们可以在

OnHandlerChanged()
上重写方法
TabbedPage

请参考以下代码:

protected override void OnHandlerChanged()
        {
            base.OnHandlerChanged();
#if ANDROID
            FieldInfo tabbedPageManagerFieldInfo = typeof(TabbedPage).GetField("_tabbedPageManager", BindingFlags.NonPublic | BindingFlags.Instance);
            object tabbedPageManager = tabbedPageManagerFieldInfo?.GetValue(this);
            FieldInfo tabLayoutFieldInfo = tabbedPageManager?.GetType().GetField("_tabLayout", BindingFlags.NonPublic | BindingFlags.Instance);
            var tablayout = tabLayoutFieldInfo?.GetValue(tabbedPageManager) as Google.Android.Material.Tabs.TabLayout;
            if (tablayout != null)
            {
                for (int i = 0; i < tablayout.TabCount; i++)
                {
                    tablayout.GetTabAt(i).OrCreateBadge.Number = 10;
                   //tablayout.GetTabAt(i).OrCreateBadge.BackgroundColor = Resource.Color.blue;
                   //tablayout.GetTabAt(i).OrCreateBadge.BadgeTextColor = Resource.Color.white;
                }
            }
#endif
        }
© www.soinside.com 2019 - 2024. All rights reserved.