.Net Maui:如何在 Android 平台上使用 TabbedViewHandler 以 BottomNavigationView 类型访问底部选项卡栏?

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

我正在开发一个 .Net Maui 应用程序,我必须在 Android 上检索底部选项卡。我正在使用 TabbedViewHandler 来实现此目的。问题是 TabbedViewHandler 的 ViewGroup 中没有类型为 BottomNavigationView 的子项。相反,我在 ViewGroup 中得到了一个 RecyclerView 类型的子级。

有人可以帮助我访问底部选项卡吗? 预先感谢。

  1. 自定义TabbedViewHandler
using Android.Content;
using Android.Views;
using Google.Android.Material.BottomNavigation;
using Microsoft.Maui.Controls.Handlers;
using Microsoft.Maui.Platform;
using Microsoft.Maui.Controls.Platform;
using AndroidX.AppCompat.Widget;

namespace SampleApp1
{
    public class CustomTabbedViewHandler : TabbedViewHandler
    {
        public CustomTabbedViewHandler(Context context) : base(context)
        {
        }

        protected override void ConnectHandler(View nativeView)
        {
            base.ConnectHandler(nativeView);

            // Access the ViewGroup containing the tabs
            var viewGroup = nativeView as ViewGroup;

            if (viewGroup != null)
            {
                // Retrieve the BottomNavigationView
                var bottomNavigationView = GetBottomNavigationView(viewGroup);

                if (bottomNavigationView != null)
                {
                    // Perform operations on the BottomNavigationView here
                }
            }
        }

        private BottomNavigationView GetBottomNavigationView(ViewGroup viewGroup)
        {
            for (int i = 0; i < viewGroup.ChildCount; i++)
            {
                var child = viewGroup.GetChildAt(i);

                if (child is BottomNavigationView bottomNavigationView)
                {
                    return bottomNavigationView;
                }
                else if (child is ViewGroup childViewGroup)
                {
                    var result = GetBottomNavigationView(childViewGroup);
                    if (result != null)
                        return result;
                }
            }
            return null;
        }
    }
}

  1. 自定义TabbedView.cs
public class CustomTabbedView : TabbedPage
{

}
  1. 自定义TabbedPage.xaml
<?xml version="1.0" encoding="utf-8" ?>
<controls:CustomTabbedView
    xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
    xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
    xmlns:controls="clr-namespace:SampleApp1"
    xmlns:android="clr-namespace:Microsoft.Maui.Controls.PlatformConfiguration.AndroidSpecific;assembly=Microsoft.Maui.Controls"
    android:TabbedPage.ToolbarPlacement="Bottom"
    android:TabbedPage.IsSwipePagingEnabled="False"
    x:Class="SampleApp1.CustomTabbedPage">
</controls:CustomTabbedView>
  1. 自定义TabbedPage.xaml.cs
namespace SampleApp1;

public partial class CustomTabbedPage : CustomTabbedView
{
    public CustomTabbedPage()
    {
        InitializeComponent();

        AddViews();

        Title = CurrentPage.Title;
        base.OnCurrentPageChanged();
    }

    protected override void OnCurrentPageChanged()
    {
        base.OnCurrentPageChanged();
        Title = CurrentPage.Title;
    }

    private void AddViews()
    {
        var home = new HomePage() { Title = "Home" };
        this.Children.Add(home);

        var detailView = new DetailPage() { Title = "Details" };
        this.Children.Add(detailView);
    }
}

HomePage 和 DetailPage 是从代码隐藏添加的简单内容页面。我确认我已在 MauiProgram.cs 文件中注册了处理程序。

android maui handler tabbar bottomtabs
1个回答
0
投票

您能详细解释一下 ShellBottomNavViewAppearanceTracker 吗?仅供参考,应用程序中仅使用 TabbedPage。

如果您正在开发Shell应用程序,您可以通过

ShellRenderer
ShellBottomNavViewAppearanceTracker
获得BottomNavigationView。

namespace MauiApp2.Platforms.Android
{
    public partial class ShellHandlerEx : ShellRenderer
    {
        protected override IShellBottomNavViewAppearanceTracker CreateBottomNavViewAppearanceTracker(ShellItem shellItem)
        {
            return new ShellBottomNavViewAppearanceTrackerEx(this, shellItem.CurrentItem);
        }
    }
}
namespace MauiApp2.Platforms.Android
{
    class ShellBottomNavViewAppearanceTrackerEx : ShellBottomNavViewAppearanceTracker
    {
        private readonly IShellContext shellContext;

        public ShellBottomNavViewAppearanceTrackerEx(IShellContext shellContext, ShellItem shellItem) : base(shellContext, shellItem)
        {
            this.shellContext = shellContext;
        }

        public override void SetAppearance(BottomNavigationView bottomView, IShellAppearanceElement appearance)
        {

            base.SetAppearance(bottomView, appearance);
            ....
        }
    }
}
var builder = MauiApp.CreateBuilder();
builder.UseMauiApp<App>()
    ....
    .ConfigureMauiHandlers(handlers =>
    {
         handlers.AddHandler<Shell, ShellHandlerEx>();
    });

如果您正在开发TabbedPage应用程序,您可以参考MAUI GitHub上的讨论:如何从TabbedView处理程序访问BottomNavigationView? #16344。它通过反射获取BottomNavigationView。

© www.soinside.com 2019 - 2024. All rights reserved.