' NavigationPage.HasNavigationBar="False" ' not working when directing to page

SaoryEmanoelleSaoryEmanoelle USMember ✭✭
edited May 10 in Xamarin.Forms

Good afternoon!

I did this once in Xamarin.Android but can't make it work in Xamarin.Forms:

My first page (LoginServerView) consists of only an Entry where the user will type down the server he wants to access. Once the server is saved, he can click "Next" and go to a different page (LoginDataView) where his username and password will be asked. He can then log in and use the app.

The second time he uses the app however I want to check if the server has already been saved (I'm currently using Properties to keep the string in check). If the validation is true, then instead of opening LoginServerView again, I want to send the user directly to LoginDataView. When that happens, I also want to get rid of the back button and instead force the user to click on a ToolbarItem if he wants to change the server (Which shouldn't be needed once he's written it down first). However, if I force LoginDataView to load first, my Toolbaritems disappear and the BackButton appears, even though I set NavigationPage.HasNavigationBar="False"on xaml.

I tried to post images but it won't let me. If the explanation is confusing, I can't try again.

Below is my code for App:

public App()
{
InitializeComponent();

        if (Current.Properties.ContainsKey("Servidor"))
        {
            MainPage = new NavigationPage(new LoginServerView(0)); //Tells the LoginServerView to immediatelly open LoginDataView
        }
        else
            MainPage = new NavigationPage(new LoginServerView(1)); //Tells the LoginServerView to load and wait for User input

}

I don't understand what I'm doing wrong. Both processes go through the same methods so why am I getting a different output?

Thank you!

Answers

  • ClintStLaurentClintStLaurent USUniversity ✭✭✭✭✭

    NavigationPage.SetHasNavigationBar(this, false);

    Put that in the constructor of your Page, above (before) InitializeComponent();

  • SaoryEmanoelleSaoryEmanoelle USMember ✭✭

    Hello! Thanks for the quick reply!

    Doing that takes away my NavigationBar completely :( I'd like it to stay on the page, just not the BackButton.

  • TaylorDTaylorD USMember ✭✭✭

    @SaoryEmanoelle Do the same steps as Clint described by putting the code before InitializeComponent();, but try this line of code instead.

    NavigationPage.SetHasBackButton(this, false);
    
  • SaoryEmanoelleSaoryEmanoelle USMember ✭✭

    Hi @TaylorD ! I did as you told me to but there's something wrong; when the second page is loaded first (LoginDataView), the toolbar seems to be the one from the LoginServerView (The title is from it and it also doesn't have the ToolbarItems).

    If I interact with the page though and come back to it it loads correctly.

  • TaylorDTaylorD USMember ✭✭✭

    It looks like you are swapping views by passing in the index of what Page/View you're wanting to show instead of splitting those two views/logic up into separate pages. Then you would only say:

    public App()
    {
        InitializeComponent();
    
        if (Current.Properties.ContainsKey("Servidor"))
        {
            MainPage = new NavigationPage(new LoginServerPage());
        }
        else
        {
            MainPage = new NavigationPage(new RetrieveServerPage());
        }
    }
    

    That way they each have their own split logic for which toolbar items to show and if they would like the Back button or not. Then from the RetrieveServerPage page, you would call

    await Navigation.PushAsync(new LoginServerView());
    

    once they have entered the ServerURL, to show the login page where the user enters their credentials.

  • SaoryEmanoelleSaoryEmanoelle USMember ✭✭

    Yeah, that's right. I wanted both cases to always go through the LoginServerView first so it'd be added into my stack. That way, if I needed to go back and change the server, I'd just use PopAsync() in my LoginDataView one.

    Is that a bad practice? If I want to go back to the Server Page, should I create a new instance of it instead of going back to a previously opened one?

  • TaylorDTaylorD USMember ✭✭✭
    edited May 10

    Yeah I understand your logic. Makes sense and the way I would solve that issue is to still split the pages up still. If you already have a ServerURL, then insert the server url request page before the login page and it should be put in the stack so you can navigate back to it.

    public App()
    {
        InitializeComponent();
    
        RetrieveServerPage retrieveServerPage = new RetrieveServerPage();
    
        if (Current.Properties.ContainsKey("Servidor"))
        {
            LoginServerPage loginServerPage = new LoginServerPage();
    
            MainPage = new NavigationPage(loginServerPage);
            MainPage.Navigation.InsertPageBefore(retrieveServerPage, loginServerPage)
        }
        else
        {
            MainPage = new NavigationPage(retrieveServerPage);
        }
    }
    
  • SaoryEmanoelleSaoryEmanoelle USMember ✭✭

    Whoa, that InsertPageBefore is awesome!!! First time I've seen it and it's helped a lot! :D

    This solution worked! Thank you very much for the quick response @TaylorD and @ClintStLaurent ^-^

Sign In or Register to comment.