Xamarin形成TabbedPage

时间:2018-10-16 10:56:18

标签: xamarin xamarin.forms xamarin.android tabbedpage

我在xamarin表单项目中使用“选项卡式”页面。 我试图在Android的MyTabsRenderer类中使用OnTabReselected事件。但是不会调用诸如OnTabSelected,OnTabReselected和OnTabUnselected之类的事件。有人对此有任何解决方案吗?

xamarin表格版本:3.2.0.871581

VS版本:15.8.6

这是我的代码段(MyTabsRenderer类):

using Android.OS;
using Android.Views;
using Xamarin.Forms;
using Xamarin.Forms.Platform.Android.AppCompat;
using MyProject;
using MyProject.Droid;
using Xamarin.Forms.Platform.Android;
using System.ComponentModel;
using Android.Support.V4.View;
using Android.Content.Res;
using Android.Content;
using Android.Support.Design.Widget;

[assembly: ExportRenderer(typeof(TabController), typeof(MyTabsRenderer))]
namespace MyProject.Droid
{
    public class MyTabsRenderer : TabbedPageRenderer, TabLayout.IOnTabSelectedListener
    {


        bool setup;
        ViewPager viewPager;
        TabLayout tabLayout;

        public MyTabsRenderer(Context context) : base(context)
        {
        }

        protected override void SetTabIcon(TabLayout.Tab tab, FileImageSource icon)
        {
            base.SetTabIcon(tab, icon);
        }

        protected override void OnElementChanged(ElementChangedEventArgs<TabbedPage> e)
        {
            base.OnElementChanged(e);
        }
        protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            base.OnElementPropertyChanged(sender, e);

            //if (setup)
            //    return;


            if (e.PropertyName == "Renderer" || e.PropertyName == "CurrentPage")
            {
                viewPager = (ViewPager)ViewGroup.GetChildAt(0);
                tabLayout = (TabLayout)ViewGroup.GetChildAt(1);
                setup = true;
                ColorStateList colors = GetTabColor();

                for (int i = 0; i < tabLayout.TabCount; i++)
                {
                    var tab = tabLayout.GetTabAt(i);
                    SetTintColor(tab, colors);
                }
                tabLayout.SetOnTabSelectedListener(new TabLayout.ViewPagerOnTabSelectedListener(viewPager));
            }
        }


        void OnTabReselected(TabLayout.Tab tab)
        {
            // To have the logic only on he tab on position 1
            if (tab == null || tab.Position != 1)
            {
                return;
            }

            if (tab.Text == "Play")
            {
                tab.SetText("Pause");
                tab.SetIcon(Resource.Drawable.icon);
                // App.pauseCard = false;
            }
            else
            {
                tab.SetText("Play");
                tab.SetIcon(Resource.Drawable.icon);
                //  App.pauseCard = true;
            }

            SetTintColor(tab, GetTabColor());

        }

        void SetTintColor(TabLayout.Tab tab, ColorStateList colors)
        {
            var icon = tab?.Icon;
            if (icon != null)
            {
                icon = Android.Support.V4.Graphics.Drawable.DrawableCompat.Wrap(icon);
                Android.Support.V4.Graphics.Drawable.DrawableCompat.SetTintList(icon, colors);
            }
        }

        ColorStateList GetTabColor()
        {
            return ((int)Build.VERSION.SdkInt >= 23)
                ? Resources.GetColorStateList(Resource.Color.icon_tab, Forms.Context.Theme)
                               : Resources.GetColorStateList(Resource.Color.icon_tab);
        }

    }
}

1 个答案:

答案 0 :(得分:0)

可能是因为您设置的OnTabSelectedListener错误。

尝试将侦听器设置为thistabLayout.SetOnTabSelectedListener(this);

也可以从TabbedPageRenderer.cs source code

中获取灵感。