Looking for an Android solution to stop the clicking of a tab from changing to the new page

Viewed 126

I have five pages in my application and if App.quizRunning == true and the current page is Japanese.PhrasesFrame I need to be able to stop the action of tapping on any of the tabs from doing anything:

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

So far I have an iOS Solution that I came up with. If anyone sees anything wrong with this I would appreciate there advice:

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;
            }
        }  
    }
}

Now I am looking for an Android solution but have been unable to find anything at all.

Does anyone have any ideas how I can do this with an Android renderer?

1 Answers

Not sure why you didn't try 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;
            }
        }
Related