Enable navigationbar or create a panel on top of the Xamarin.form tabpage

Asked

Viewed 153 times

2

I have this page (Tabbedpage) that creates two tabs:

<?xml version="1.0" encoding="utf-8" ?>
<TabbedPage xmlns="http://xamarin.com/schemas/2014/forms"
            xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
            xmlns:views="clr-namespace:Operacional.Views"
            x:Class="Operacional.Views.MainPage">

    <TabbedPage.Children> 
        <NavigationPage Title="Indicadores">
            <NavigationPage.Icon>
                <OnPlatform x:TypeArguments="FileImageSource">
                    <On Platform="iOS" Value="tab_about.png"/>
                </OnPlatform>
            </NavigationPage.Icon>
            <x:Arguments>
                <views:Indicadores />
            </x:Arguments>
        </NavigationPage>
        <NavigationPage Title="Paineis">
            <NavigationPage.Icon>
                <OnPlatform x:TypeArguments="FileImageSource">
                    <On Platform="iOS" Value="tab_feed.png"/>
                </OnPlatform>
            </NavigationPage.Icon>
            <x:Arguments>
                <views:PaineisPage />
            </x:Arguments>
        </NavigationPage>
    </TabbedPage.Children>
</TabbedPage>

The thing is, I’m not getting to qualify NavigationBar or at least create a panel above tabpages. The ideal would be to enable the NavigationBar, this would be ideal for me. How it works. When opening the App, it lands on a login screen. When you log in, then you enter the MainPage and in it the TabPages. If I try to exeibiate the NavigationBar does not give any error, but does not display. Or she is Hide or the TabPages are "killing" her. Below my codes. Mainpage

[XamlCompilation(XamlCompilationOptions.Compile)]
    public partial class MainPage : TabbedPage
    {
        public MainPage ()
        {
            InitializeComponent ();
            NavigationPage.SetHasNavigationBar(this, true);            
        }
    }

the login screen

public  void Login_Clicked(object sender, EventArgs e)
        {
            LoginService svc = new LoginService();
            LoginRS res = svc.Login(txtUsuario.Text, txtSenha.Text);
            if (res != null && res.Success  )
            {
                App.LooggedUser = res;
                Application.Current.MainPage = new MainPage();
            }
            else if(res != null && ! res.Success)
            {
                lblErroLogin.Text = res.Exception;
            }
            else
            {
                lblErroLogin.Text = "Não foi possível realizar o Login, por favor verifique sua conexão com a Internet";
            }
        }

and my App.xaml.Cs

public App()
        {
            InitializeComponent();

            if (!IsUserLoggedIn)
            {
                MainPage = new NavigationPage(new LoginPage());
            }
            else
            {
                MainPage = new NavigationPage(new MainPage());
            }
        }

1 answer

1


Man, this is a delicate subject. Although it is possible, it is not recommended.

The implementation would be you change the login button code, where you change the app mainpage, to put the TabbedPage within a NavigationPage the same way you did in the else of OnStart, thus:

public  void Login_Clicked(object sender, EventArgs e)
{
    LoginService svc = new LoginService();
    LoginRS res = svc.Login(txtUsuario.Text, txtSenha.Text);
    if (res != null && res.Success  )
    {
        App.LooggedUser = res;
        Application.Current.MainPage = new NavigationPage(new MainPage());
    }
    else if(res != null && ! res.Success)
        lblErroLogin.Text = res.Exception;
    else
        lblErroLogin.Text = "Não foi possível realizar o Login, por favor verifique sua conexão com a Internet";
    }
}

And when browsing from there, use the Navigation from the main page instead of the Navigation from TabbedPage:

App.Current.MainPage.Navigation.PushAsync(new NovaPage());

About the fact that it is not recommended:

Since Xamarin.Forms is multi-platform, it is important that you write your Apps so that usability is "fluent" in each of them at the time of execution.

In this specific case, how can you consult in this article from the developer site Xamarin, we would have a problem in the iOS interface.

On a free website alert translation:

While it is wise to admit Navigationpages within Tabbedpage, we do not recommend that you place a Tabbedpage within a Navigationpage, because in iOS Uitabbarcontroller always acts as a package for Uinavigationcontroller.

In this quote the site recommends to give a check in Guideline regarding this subject: Combined View Controller Interfaces.

I hope it helps.

  • Diego, thanks for the explanation. I will test and put the result. The advantage here, is that despite being Forms, this project will run only on Android, Ufa!!

  • Diego, just one question, how do I make Navigationbar backgroundcolor different color? I’m trying on Mainpage and it’s giving error. How do I do?

  • @pnet, do you mind posting this as another question? This is a common point of doubt in the community and I think it would be easier for others to find the answer if the question is dedicated exclusively to this. (y) Thanks!

  • Yes, of course, actually it’s another question. Sorry. I’ll open another thread.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.