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

问题描述 投票:0回答:1

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

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

await Navigation.PushAsync(new SubCategoryView());

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

android xamarin.forms xamarin.android tabbedpage
1个回答
0
投票

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

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

        }
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.