如何检测Xamarin.Forms中选项卡按钮的单击? [英] How can I detect the clicking of a tab button in Xamarin.Forms?

查看:200
本文介绍了如何检测Xamarin.Forms中选项卡按钮的单击?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的代码.我想知道如何检测用户单击已选择的选项卡的时间,因为我想在play.png和pause.png之间切换aPage的图标,另外我还想在APage上调用方法.

Here is the code that I have. I would like to know how I can detect when a user clicks a tab that is already selected as I want to toggle the icon for the aPage between play.png and pause.png plus I also want to call a method on APage.

public partial class MainPage : TabbedPage
{
    public MainPage()
    {
        InitializeComponent();

        var aPage = new NavigationPage(new APage())
        {
            Title = "Play",
            Icon = "play.png"
        };
        var bPage = new NavigationPage(new BPage())
        { 
            Title = "Settings",
            Icon = "b.png"
        };

        Children.Add(aPage);
        Children.Add(bPage);
    }
}

请注意,如果可能的话,我想找到一个不包含适用于iOS和Android的自定义渲染器的解决方案.我想知道是否可以重新定义TabbedPage并将逻辑放在该类中?

Note that if possible I would like to find a solution that does not involve custom renderers for both iOS and Android. I'm wondering can I redefine the TabbedPage and put the logic in that class?

推荐答案

我知道您想避免使用自定义渲染器,但这只能通过使用自定义渲染器"来实现.

I know you want to avoid using custom renderers, but this is only possible by using a Custom Renderer.

using Android.Content;
using Android.Support.Design.Widget;

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

[assembly: ExportRenderer(typeof(MainPage), typeof(MainPageRenderer))]
namespace YourNameSpace
{
    public class MainPageRenderer : TabbedPageRenderer, TabLayout.IOnTabSelectedListener
    {
        MainPage _page;

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

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

            if (e.NewElement != null)
                _page = e.NewElement as MainPage;
            else
                _page = e.OldElement as MainPage;
        }

        void TabLayout.IOnTabSelectedListener.OnTabReselected(TabLayout.Tab tab)
        {
            System.Diagnostics.Debug.WriteLine("Tab Reselected");
            //Handle Tab Reselected
        }
    }
}

Xamarin.iOS自定义渲染器

using System;
using System.Diagnostics;

using UIKit;

using Xamarin.Forms;
using Xamarin.Forms.Platform.iOS;

[assembly: ExportRenderer(typeof(MainPage), typeof(MainPageRenderer))]
namespace YourNameSpace
{
    public class MainPageRenderer : TabbedRenderer
    {
        MainPage _page;

        protected override void OnElementChanged(VisualElementChangedEventArgs e)
        {
            base.OnElementChanged(e);

            if (e.NewElement != null)
                _page = e.NewElement as MainPage;
            else
                _page = e.OldElement as MainPage;

            try
            {
                if (ViewController is UITabBarController tabBarController)
                    tabBarController.ViewControllerSelected += OnTabbarControllerItemSelected;
            }
            catch (Exception exception)
            {
                Debug.WriteLine(exception);
            }
        }

        void OnTabbarControllerItemSelected(object sender, UITabBarSelectionEventArgs eventArgs)
        {
            if (_page?.CurrentPage?.Navigation != null && _page.CurrentPage.Navigation.NavigationStack.Count > 0)
            {
                Debug.WriteLine("Tab Tapped");
                //Handle Tab Tapped
            }
        }
    }
}

代码功劳: @Kyle 查看全文

登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆