简体   繁体   English

Xamarin.Forms - 如何在TabbedPage上禁用Tab?

[英]Xamarin.Forms - How to disable Tab on TabbedPage?

I'm looking for a way to disable a tab within a TabbedPage. 我正在寻找一种方法来禁用TabbedPage中的选项卡。 The tab should be still showing up in the TabbedPage header but simply be disabled. 该选项卡应该仍然显示在TabbedPage标题中,但只是被禁用。

I tried to set the Page to IsEnabled = false but apparently this is not how you do it or I did it wrong. 我试图将Page设置为IsEnabled = false,但显然这不是你怎么做的,或者我做错了。 :) :)

You can create a custom renderer for a tabbed page control and disable user interaction in your platform specific implementation of the renderer. 您可以为选项卡式页面控件创建自定义渲染器,并在渲染器的特定于平台的实现中禁用用户交互。

For example your renderer for iOS would use the UITabBarControllerDelegate Protocol to override 例如,iOS的渲染器将使用UITabBarControllerDelegate Protocol来覆盖

- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController

and return NO for the tab that should not be selectable. 并为不可选择的选项卡返回NO The solution for Android or Windows Phone would work similar. Android或Windows Phone的解决方案类似。

Here is my self-contained take on @joeriks method, which is a nice quick/simple solution. 这是我自带的@joeriks方法,这是一个很好的快速/简单的解决方案。 Annoying that the event args are just EventArgs. 令人讨厌的事件args只是EventArgs。

Just add this to your TabbedPage .cs file somewhere. 只需将其添加到您的TabbedPage .cs文件中。

    Page _lastKnownPage;
    protected override void OnCurrentPageChanged()
    {
        base.OnCurrentPageChanged();
        if (CurrentPage.IsEnabled)
            _lastKnownPage = CurrentPage;
        else
            CurrentPage = _lastKnownPage;
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM