c# - 寻找一种 Android 解决方案来阻止单击选项卡更改为新页面

标签 c# xamarin xamarin.forms

我的应用程序中有五个页面,如果 App.quizRunning == true 并且当前页面是 Japanese.PhrasesFrame 我需要能够停止点击任何选项卡以执行任何操作:

namespace Japanese {
    public partial class MainPage : TabbedPage {
        public MainPage() {
            InitializeComponent();
                App.cardsPage = new Cards();
                App.homePage = new HomePage();
                App.helpPage = new HelpPage();
                App.settingsPage = new SettingsPage();
                App.phrasesPage = new PhrasesFrame(this);
                Children.Add(App.navHomePage);
                Children.Add(App.navHelpPage);
                Children.Add(App.navSettingsPage);
                Children.Add(App.navCardsPage);
                Children.Add(App.navPhrasesFrame);
        }
    }
}

到目前为止,我已经有了一个 iOS 解决方案。如果有人发现这有什么问题,我将不胜感激:

using System;
using UIKit;
using Xamarin.Forms;
using Xamarin.Forms.Platform.iOS;
using System.Diagnostics;
using Japanese.Enums;

[assembly: ExportRenderer(typeof(TabbedPage), typeof(Japanese.iOS.TabbedPageRenderer))]
namespace Japanese.iOS {
    public class TabbedPageRenderer : TabbedRenderer {

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

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

            try {
                var tabbarController = (UITabBarController)this.ViewController;

                if (null != tabbarController) 
                    tabbarController.ViewControllerSelected += OnTabbarControllerItemSelected;

            }
            catch (Exception exception) { Console.WriteLine(exception); }
        }

        private void OnTabbarControllerItemSelected(object sender, UITabBarSelectionEventArgs eventArgs)
        {
            if (!(_page.CurrentPage is Japanese.PhrasesFrame) && App.quizRunning == true) {
                _page.CurrentPage = App.navPhrasesFrame;
            }
        }  
    }
}

现在我正在寻找 Android 解决方案,但一直找不到任何东西。

有谁知道我如何使用 Android 渲染器做到这一点?

最佳答案

不确定您为什么不尝试 CurrentPage = App.phrasesPage;

override protected void OnCurrentPageChanged() {
            var a = this.CurrentPage;
            if (Settings.mode == MO.Quiz && CurrentPage != App.phrasesPage) {
                // give the user a confirmation/alert box and
                // depending on the outcome set the variable
                // App.continueQuiz to be true or false and then 
                // go back to Japanese.PhrasesFrame page
                CurrentPage = App.phrasesPage;
            }
        }

关于c# - 寻找一种 Android 解决方案来阻止单击选项卡更改为新页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53018775/

相关文章:

c# - 在不同的 Entity Framework 模型中不能有相同的表名吗?

android - 在 Xamarin 中隐藏 Android 标题栏有延迟

c# - 更改找不到 TreeView 控件的背景颜色?

c# - 如何创建具有不同类型的单个元素的类型安全数组或集合?

c# - WP8.1 BackgroundMediaPlayer 问题 : Send messages with data between Foreground and Background

c# - 你什么时候以及为什么要向上转换一个对象?

ios - 添加观察者时 AVPlayer 崩溃

android - 无法在 Android 平台上初始化 ZXing - Xamarin Forms 应用程序

c# - 如何在不实例化新 ViewModel 的情况下设置 BindingContext

c# - 如何从非异步方法调用异步方法?