两次单击同一标签的文本/图标时,如何使标签页导航?

时间:2019-11-08 09:13:25

标签: android xamarin xamarin.forms xamarin.android tabbedpage

所以我有一个选项卡式页面,并且有几个与之相关的选项卡 enter image description here

在第一个选项卡上,我有一个列表,如果我单击一个图块,可以说是tile1,那么我将打开另一个页面,使这些选项卡保持完整,但是当我再次单击选项卡option1时,我想要的是它应该转到主列表,这不会发生,任何见解都将非常有帮助 点击“ tile1”,我这样做

await Navigation.PushAsync(new SubCategoryView());

我认为这是正确的方法,具有讽刺意味的是,足够的预期行为在iOS中有效,但在Android上无效

1 个答案:

答案 0 :(得分:1)

如果要在选择相同选项卡时返回到根页面,则可以使用自定义渲染器

在Android中


using Android.Content;

using Android.Support.Design.Widget;
using xxx;
using xxx.Droid;

using Xamarin.Forms;
using Xamarin.Forms.Platform.Android;
using Xamarin.Forms.Platform.Android.AppCompat;

[assembly: ExportRenderer(typeof(TabbedPage), typeof(MyTabbedRenderer))]
namespace xxx.Droid
{
    public class MyTabbedRenderer : TabbedPageRenderer, TabLayout.IOnTabSelectedListener
    {

        public MyTabbedRenderer(Context context) : base(context)
        {

        }

        private TabbedPage tabbed;
        protected override void OnElementChanged(ElementChangedEventArgs<TabbedPage> e)
        {
            base.OnElementChanged(e);

            if (e.NewElement != null)
            {
                tabbed = (TabbedPage)e.NewElement;
            }
            else
            {
                tabbed = (TabbedPage)e.OldElement;
            }

        }
        async void TabLayout.IOnTabSelectedListener.OnTabReselected(TabLayout.Tab tab)
        {
            await tabbed.CurrentPage.Navigation.PopToRootAsync();
        }

    }


}

在iOS中

using UIKit;
using System;
using Xamarin.Forms;
using Xamarin.Forms.Platform.iOS;
using xxx;
using xxx.iOS;

[assembly: ExportRenderer(typeof(TabbedPage), typeof(MyTabbedRenderer))]
namespace xxx.iOS
{
    public class MyTabbedRenderer : TabbedRenderer
    {
        private TabbedPage tabbed;
        protected override void OnElementChanged(VisualElementChangedEventArgs e)
        {
            base.OnElementChanged(e);

            if (e.NewElement != null)
            {
                tabbed = (TabbedPage)e.NewElement;
            }
            else
            {
                tabbed = (TabbedPage)e.OldElement;
            }

            try
            {
                var tabbarController = (UITabBarController)this.ViewController;
                if (null != tabbarController)
                {
                    tabbarController.ViewControllerSelected += OnTabbarControllerItemSelected;
                }
            }
            catch (Exception exception)
            {
                Console.WriteLine(exception);
            }
        }

        private async void OnTabbarControllerItemSelected(object sender, UITabBarSelectionEventArgs eventArgs)
        {
            if (tabbed?.CurrentPage?.Navigation != null && tabbed.CurrentPage.Navigation.NavigationStack.Count > 0)
            {
                await tabbed.CurrentPage.Navigation.PopToRootAsync();
            }

        }
    }
}