Quantcast
Channel: Xamarin.Forms — Xamarin Community Forums
Viewing all articles
Browse latest Browse all 77050

I can't get my page's title to display in the navigation bar.

$
0
0

I can't tell if this is a bug or if it is a consequence of making a bad architecture decision.

Consider an application with many pages related in this way:

  • FirstPage has some fabulous OpenGL view that does cool stuff. It doesn't have a navigation bar. This includes a button that launches...
  • MenuPage, which displays buttons that launch other forms, but those aren't required for this demo.

There are several things I've tried.

In the initial approach, I start FirstPage the most obvious way (note I'm not wrapping it in a NavigationPage):
public static Page GetMainPage() {
return new FirstPage();
}

FirstPage is really simple, here's just the constructor content:

var lbl = new Label() { Text = "Pretend this is a big complex full-page element." };

var btn = new Button() { Text = "The important thing" };
    btn.Clicked += (s, e) => {
    Navigation.PushModalAsync(new NavigationPage(new MenuPage()));
};

var layout = new StackLayout();
layout.Children.Add(lbl);
layout.Children.Add(btn);

this.Content = layout;

I had to use PushModalAsync() because that seems to be safe if you're not wrapped in a NavigationPage? I'm not sure on that. It could be why I'm having trouble.

I made MenuPage even simpler for the purposes of the demo:
Title = "Menu";

var lbl = new Label() { Text = "asdf" };

var layout = new StackLayout();
layout.Children.Add(lbl);
Content = layout;

With this code, the navigation bar displays on MenuPage, but not the title. The back button doesn't work.

If I modify the PushAsync() call in FirstPage to be PushModalAsync(), I get the title displayed, but the back button on the navigation bar doesn't work anymore.

So I decided that's likely what happens when you don't use NavigationPage start-to-finish, or maybe a side effect of PushModalAsync(). So I changed App:

    public static Page GetMainPage() {
        return new NavigationPage(new FirstPage());
    }

And in FirstPage, used PushAsync() instead. (I also Now the back button works, but MenuPage's Title doesn't show in the navigation bar. If I revert to PushModalAsync(), I get the title but no back button.

Things got more confusing to me when I remembered to put a call to NavigationPage.SetHasNavigationBar() to make sure FirstPage doesn't have the navigation bar, and one in MenuPage to make sure it /does/. If I PushAsync(), I get no navigation bar on either page. If I PushModalAsync(), I get the navigation bar on MenuPage with no title and no back button functionality.

What the heck is going on? Maybe I don't fundamentally understand the difference between modal/non-modal in this context? Maybe your app is supposed to use NavigationPage and always show the navigation bar no matter what? I'm not sure what the right track is.


Viewing all articles
Browse latest Browse all 77050

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>