Значок не подсвечивается при использовании BottomBarPage - PullRequest
0 голосов
/ 26 ноября 2018

Я использую пакет BottomBarPage.Я изменяю вкладку «на лету» программным способом, однако значок внизу не выделяется для Android при смене вкладки.Хорошо работает на iOS под рукой.Я попробовал код Джеймса Монтемагно, я не смог заставить его работать с пакетом BottomBarPage.Это ссылка на его код https://montemagno.com/dynamically-changing-xamarin-forms-tab-icons-when-select/

Как мне выделить значок, когда программно создается закладка для BottomBarPage?

Ниже мой код.

public AndroidMainPage(int indexPage)
{
    InitializeComponent();
    NavigationPage.SetHasNavigationBar(this, false);
    CurrentPageChanged += MainPage_CurrentPageChanged;
    CurrentPage = Children[indexPage];
    CurrentPage.Appearing += (s, a) => Children[indexPage].Icon = new FileImageSource() { File = "quizzes_selected.png" };
    CurrentPage.Disappearing += (s, a) => Children[indexPage].Icon = new FileImageSource() { File = "quizzes.png" };
    _currentPage = CurrentPage;
}

private void MainPage_CurrentPageChanged(object sender, EventArgs e)
{
    IIconChange currentBinding;
    if (_currentPage != null)
    {
        currentBinding = ((NavigationPage)_currentPage).CurrentPage.BindingContext as IIconChange;
        if (currentBinding != null)
            currentBinding.IsSelected = false;
    }

    _currentPage = CurrentPage;
    currentBinding = ((NavigationPage)_currentPage).CurrentPage.BindingContext as IIconChange;
    if (currentBinding != null)
        currentBinding.IsSelected = true;

    UpdateIcons?.Invoke(this, EventArgs.Empty);
}

Android BottomBarPage Renderer:

[assembly: ExportRenderer(typeof(AndroidMainPage), typeof(MyTabbedPageRenderer))]
namespace TabPageDemo.Android.Renderers
{
    public class MyTabbedPageRenderer : BottomBarPageRenderer
    {
        bool setup;
        TabLayout layout;

        public MyTabbedPageRenderer()
        {    
        }

        protected override void OnElementChanged(ElementChangedEventArgs<BottomBarPage> e)
        {
            if (e != null) base.OnElementChanged(e);
            if (Element != null)
            {
                ((AndroidMainPage)Element).UpdateIcons += Handle_UpdateIcons;
            }
        }

        protected override void OnElementPropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
        {
            base.OnElementPropertyChanged(sender, e);

            if (layout == null && e.PropertyName == "Renderer")
            {
                layout = (TabLayout)ViewGroup.GetChildAt(1);
            }
        }

        void Handle_UpdateIcons(object sender, EventArgs e)
        {
            TabLayout tabs = layout;

            if (tabs == null)
                return;

            for (var i = 0; i < Element.Children.Count; i++)
            {
                var child = Element.Children[i].BindingContext as IIconChange;
                if (child == null) continue;
                var icon = child.CurrentIcon;
                if (string.IsNullOrEmpty(icon))
                    continue;

                TabLayout.Tab tab = tabs.GetTabAt(i);
                SetCurrentTabIcon(tab, icon);
            }
        }

        void SetCurrentTabIcon(TabLayout.Tab tab, string icon)
        {
            tab.SetIcon(IdFromTitle(icon, ResourceManager.DrawableClass));
        }

        int IdFromTitle(string title, Type type)
        {
            string name = Path.GetFileNameWithoutExtension(title);
            int id = GetId(type, name);
            return id;
        }

        int GetId(Type type, string memberName)
        {
            object value = type.GetFields().FirstOrDefault(p => p.Name == memberName)?.GetValue(type)
                ?? type.GetProperties().FirstOrDefault(p => p.Name == memberName)?.GetValue(type);
            if (value is int)
                return (int)value;
            return 0;
        }
    }
}

При использовании Renderer переменная табуляции всегда равна нулю из этого TabLayout.Tab tab = tabs.GetTabAt(i);

Любое предложение?

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...