Search code examples
c#xamarin.formsmaster-detail

Xamarin Forms MasterDetailPage dynamically change master


I am using the default MasterDetailPage navigation in my Xamarin app, where master is the side navigation and the detail are the contentpages the user can navigate to.

I already added the code to the MenuPage:

public MenuPage()
        {
            InitializeComponent();


            if (ProfilPage.loggedin)
            {
                ucet_stack.IsVisible = true;
                ucet.IsVisible = true;
                ucet.Text = "Váš účet " + ProfilPage.meno;
            }
            else
            {
                ucet_stack.IsVisible = false;
                ucet.IsVisible = false;
            }
        }

I want to change the content of the MenuPage after the user logs in. Constructor is only called the first time so it never changes. I tried to put the same code in onAppearing but didnt work either. So what could I use to dynamically change the menu after the user logs in? Note I am pretty new to this.

UPDATE:

I created a second MasterDetailPage with new MenuPage and tried to change it in a contentpage which is detail, but it went blank

 if(ProfilPage.loggedin)
        {
            Application.Current.MainPage = new MainLoggedPage();
        } 

Solution

  • first, get a reference to your current MenuPage

    var md = (MasterDetailPage)Application.Current.MainPage;
    var menu = (MenuPage)md.Master;
    

    then, call whatever public method you've created to update it

    menu.SetUserLogin();