Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/xamarin/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Xamarin表单选项卡页_Xamarin_Xamarin.forms_Xamarin.android_Tabbedpage - Fatal编程技术网

Xamarin表单选项卡页

Xamarin表单选项卡页,xamarin,xamarin.forms,xamarin.android,tabbedpage,Xamarin,Xamarin.forms,Xamarin.android,Tabbedpage,我正在xamarin表单项目中使用选项卡式页面。 我试图在android的MyTabsRender类中使用OnTabReselected事件。但是像OnTabReselected、OnTabReselected和OnTabUnselected这样的事件没有被调用。有人对此有什么解决办法吗 xamarin表单版本:3.2.0.871581 VS版本:15.8.6 以下是我的代码片段(MyTabsRender类): 使用Android.OS; 使用Android.Views; 使用Xamarin.F

我正在xamarin表单项目中使用选项卡式页面。 我试图在android的MyTabsRender类中使用OnTabReselected事件。但是像OnTabReselected、OnTabReselected和OnTabUnselected这样的事件没有被调用。有人对此有什么解决办法吗

xamarin表单版本:3.2.0.871581

VS版本:15.8.6

以下是我的代码片段(MyTabsRender类):

使用Android.OS;
使用Android.Views;
使用Xamarin.Forms;
使用Xamarin.Forms.Platform.Android.AppCompat;
使用MyProject;
使用MyProject.Droid;
使用Xamarin.Forms.Platform.Android;
使用系统组件模型;
使用Android.Support.V4.View;
使用Android.Content.Res;
使用Android.Content;
使用Android.Support.Design.Widget;
[程序集:导出呈现程序(typeof(TabController)、typeof(mytabsrender))]
名称空间MyProject.Droid
{
公共类MyTabsRenderer:TabbedPageRenderer,TabLayout.IOnTabSelectedListener
{
布尔设置;
查看页面查看页面;
表格布局;
公共MyTabsRender(上下文):基(上下文)
{
}
受保护的覆盖无效设置选项卡图标(TabLayout.Tab选项卡,FileImageSource图标)
{
base.SetTabIcon(选项卡,图标);
}
受保护的覆盖无效OnElementChanged(ElementChangedEventArgs e)
{
基础。一个要素发生变化(e);
}
受保护的覆盖无效OnElementPropertyChanged(对象发送方,PropertyChangedEventArgs e)
{
base.OnElementPropertyChanged(发送方,e);
//如果(设置)
//返回;
如果(e.PropertyName==“渲染器”| | e.PropertyName==“当前页面”)
{
viewPager=(viewPager)ViewGroup.GetChildAt(0);
tabLayout=(tabLayout)ViewGroup.GetChildAt(1);
设置=真;
ColorStateList colors=GetTabColor();
for(int i=0;i=23)
?Resources.GetColorStateList(Resource.Color.icon_选项卡,Forms.Context.Theme)
:Resources.GetColorStateList(Resource.Color.icon_选项卡);
}
}
}

这可能是因为您将OnTabSelectedListener设置错误

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


另外,为了获得灵感,请查看TabbedPageRenderer.cs,

尝试使用完整的方法名
void TabLayout.IOnTabSelectedListener.OnTabReselected(TabLayout.Tab选项卡)
您使用的是安卓:TabbedPage.ToolbarPlacement=“Bottom”吗?
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);
        }

    }
}