gpt4 book ai didi

Xamarin 形成 TabbedPage

转载 作者:行者123 更新时间:2023-12-04 08:15:00 32 4
gpt4 key购买 nike

我在我的 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);
}

}
}

最佳答案

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

尝试将监听器设置为 this:tabLayout.SetOnTabSelectedListener(this);

也可以查看 TabbedPageRenderer.cs source code 获取灵感,

关于Xamarin 形成 TabbedPage,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52833863/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com