Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change Navigationbar title irrespective of Tab title

I am setting the page title on the Navigationbar as

public Homework ()
{
   InitializeComponent ();
   Title = "Homework";     
}

This page is a child page of Tabbed page where the Tab already has the Title "Tab1".

Now when I open the Homework.cs page from Tab1, The above code changing Tab title to Homework and the Navigationbar title also changes to "Homework". I don't want to change Tab title.

MyTabbed page code using

   public class MyTabbedPage : TabbedPage
    {
        public MyTabbedPage()
        {
           this.CurrentPageChanged +=delegate
           {        
             this.Title = this.CurrentPage.Title;
           };     
        }
    }

See some convenient snippets

public App ()
{
    InitializeComponent();
    MainPage = new NavigationPage(new LoginPage());          
}

LoginContentPage button click

btnLogin.Clicked +=async delegate {
 await Navigation.PushAsync(new ParentDashboard(), false);
};

ParentDashboard Containing tabs

public partial class ParentDashboard : MyTabbedPage
{
    public ParentDashboard()
    {
        InitializeComponent();      
        Title="Home"; //upto here title working
    }
}

From now I am clicking on HomewPage as I shown in starting of this question & that is not working. How can I do this?

like image 957
R15 Avatar asked Oct 29 '22 11:10

R15


1 Answers

I guess your project's hierarchy may be like this:

NavigationPage => TabbedPage => children pages.

Then every time the child page's title changes, the TabbedPage's title will change too. Even though we make a custom renderer for this child page, it's hard to change the page's navigationBar's title. Because the NavigationCtroller's root viewController is your tabbed page.

I recommend you to adjust your project's hierarchy, make each child page wrapped by a navigation page like:

enter image description here

In this way, you can set the navigation page's title to adjust the tab item's title and change the navigation bar's title through setting the Homework's title.

You can refer to my code about constructing app():

// This is a TabbedPage
var tabbedPage = new MyTabbedPage();

var firstPage = new MainPage();
// The NavigationPage's Title will be shown on the tab, and firstPage's title can be shown on the navigation bar
tabbedPage.Children.Add(new NavigationPage(firstPage) { Title = "FirstPage" });

var homePage = new Homework();
tabbedPage.Children.Add(new NavigationPage(homePage) { Title = "SecondPage" });

MainPage = tabbedPage;
like image 157
Anonymous Avatar answered Nov 08 '22 08:11

Anonymous