点击当前选项卡刷新页面时的 Xamarin.Forms TabbedPage 事件 [英] Xamarin.Forms TabbedPage event when current tab is tapped to refresh the page

查看:38
本文介绍了点击当前选项卡刷新页面时的 Xamarin.Forms TabbedPage 事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Xamarin.Forms 构建一个 iOS/Android 应用程序,并有一个 TabbedPage.

I'm using Xamarin.Forms to build an iOS/Android app, and have a TabbedPage.

如果用户已经在选项卡 2 上,并且单击了选项卡 2,并且我希望选项卡2 刷新,或者运行我自己的函数以便我可以自己刷新它.

If a user is already on Tab 2, and Tab2 is clicked, and I want either the tab2 to refresh, or for a function of my own to run so I can refresh it myself.

有没有办法在 Xamarin.Forms 中做到这一点,或者有没有办法用自定义呈现器做到这一点?

Is there a way to do this within Xamarin.Forms or a way to do it with custom renderers?

推荐答案

这是我最终解决问题的方法.我的 TabbedPage 由每个选项卡的 NavigationPage 组成,因此如果您不使用导航页面,则您的代码将不得不稍作更改,但只是稍微更改.你可以把你的刷新"逻辑放在 OnTabbarControllerItemSelected(iOS)和 OnTabbarControllerItemSelected(Android)中.查看下面的代码.

Here is how I ended up solving the issue. My TabbedPage consisted of a NavigationPage for each tab, so if you are not using navigation pages your code will have to change a little bit but only slightly. You can put your "refresh" logic inside of OnTabbarControllerItemSelected for iOS and OnTabbarControllerItemSelected for Android. Check out the code below.

Android 渲染器(由 Mike Ma 提供)

Android Renderer (courtesy of Mike Ma)

using Android.Support.Design.Widget;
using Xamarin.Forms;
using Xamarin.Forms.Platform.Android;
using Xamarin.Forms.Platform.Android.AppCompat;
[assembly: ExportRenderer(typeof(MainTabbedPage), typeof(MainPageRenderer))]
namespace YourNameSpace
{
    public class MainPageRenderer : TabbedPageRenderer, TabLayout.IOnTabSelectedListener
    {
        private MainTabbedPage _page;
        protected override void OnElementChanged(ElementChangedEventArgs<TabbedPage> e)
        {
            base.OnElementChanged(e);
            if (e.NewElement != null)
            {
                _page = (MainTabbedPage)e.NewElement;
            }
            else
            {
                _page = (MainTabbedPage)e.OldElement;
            }

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

iOS 渲染器:

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

[assembly: ExportRenderer(typeof(MainTabbedPage), typeof(MainPageRenderer))]
namespace YourNameSpace
{
    public class MainPageRenderer : TabbedRenderer
    {
        private MainTabbedPage _page;
        protected override void OnElementChanged(VisualElementChangedEventArgs e)
        {
            base.OnElementChanged(e);

            if (e.NewElement != null)
            {
                _page = (MainTabbedPage)e.NewElement;
            }
            else
            {
                _page = (MainTabbedPage)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 (_page?.CurrentPage?.Navigation != null && _page.CurrentPage.Navigation.NavigationStack.Count > 0)
            {
                await _page.CurrentPage.Navigation.PopToRootAsync();
            }

        }
    }
}

这篇关于点击当前选项卡刷新页面时的 Xamarin.Forms TabbedPage 事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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