2
votes

I have a Login page that is the starting point of my application. This page has navigation bar hidden with the following code:

 NavigationPage.SetHasNavigationBar(this, false);

From Login page I start my Dashboard page using the following code:

Navigation.PushAsync(new Dashboard());

I do this on two seperate places inside my Login page. First place is inside constructor of Login page where I check for active user session and the second place is inside method that handles button click for login.

So I call Navigation.PushAsync() like this:

1st way:

public Login()
{
    NavigationPage.SetHasNavigationBar(this, false);
    InitializeComponent();
    var loginSession = App.DataService.CheckUserSession();
    if(loginSession != null)
    {
        Navigation.PushAsync(new Dashboard());
    }
}

2nd way:

public void OnLogin(object o, EventArgs e)
{
    Navigation.PushAsync(new Dashboard());
}

And this is where things start to go strange. If I open my Dasboard page with button click (using 2nd example) the Dasboard page gets loaded normally and has it's own NavigationBar. However everytime that I open Dasboard page when user session is found (using 1st way), my Dasboard page will have missing NavigationBar. To make things even more strange, if I navigate from Dasboard page to another page (lets say Settings) and then return back to Dasboard, navigation bar will then work normally on Dasboard page. I have tried removing the code that hides navigation bar on Login page NavigationPage.SetHasNavigationBar(this, false); and what this does is that Login page will have it's navigation bar shown, but upon navigation to Dashboard (using 1st way), navigation bar will remain the same as on Login page.

I was maybe wondering if the problem lies within the fact that all my pages (Login, Dashboard and Settings) are child's of ContentPage and would have to instead inherit from NavigationPage?

I am really confused about what's happening with my NavigationBar at this moment.

1
How are you setting the app's MainPage? - Mark Larter
Do you mean my Application class? - Denis Vitez
Yes, in your Application class, how does the Login page get initially displayed? Post that code. - Mark Larter
MainPage = new NavigationPage(new Login()); - Denis Vitez
That all looks fine. Only thing I see differs from page constructors in our codebase is that call to InitializeComponent precedes everything else. - Mark Larter

1 Answers

2
votes

It's because the Dashboard page is being pushed from inside the Login constructor. Instead, override OnAppearing() and do it there:

protected override async void OnAppearing()
{
    base.OnAppearing();

    await Navigation.PushAsync(new Dashboard());
}

Also, your children pages should inherit from ContentPage as nesting NavigationPages could lead to other issues :)