Forum Xamarin.Forms

Navigate programmatically from one detail page to another (MasterDetailPage)

AzucenaFAzucenaF MXMember ✭✭
edited May 2015 in Xamarin.Forms

I have a button on a detail page (using MasterDetailPage) that it is supposed to take you to a different detail page. The problem is if I change de detail page from outside menu (MasterDetailPage), detail page does not work properly.
Have anyone tried this before?

Answers

  • LeeOlsen.5615LeeOlsen.5615 USUniversity ✭✭

    I believe you would normally change the Page.Master & Page.Detail properties with new pages rather than swapping out the MasterDetailPage itself. At least this is how I'm handling it.

  • AdamPAdamP AUUniversity ✭✭✭✭✭

    Generally what you do is you have the Master Detail Page and inside the detail page you put a navigation page that allows you to move between pages while still having the master page available.

    If you want to move from one master page to a completely different page (or even different master page), then you would have a navigation page at the root and the master pages / content pages as children of that. But I think the first approach might be what you are after.

  • DavePaulDavePaul CAMember ✭✭
    edited May 2015

    I don't know if this is considered correct but it works for me.
    App.Current.MainPage = whichever masterdetail page you want to load.

    Edit... sorry didn't read your comment correctly, just the follow ups.

    You could use messaging center to make the change from the menu/master page.

  • Hello,
    Can someone help with this issue please? I am having the same issue of wanting to navigate from one detail page to the other but i don't know what calling code to add to the first detail page?

  • nthingnthing USMember ✭✭

    App.Current.MainPage = new YourMasterDetailPage { Detail = new NavigationPage(new YourDetailPage()) }

  • SmathsSmaths USMember ✭✭

    Hi @AzuncenaF,

    Here is the code I'm using in my MasterDetail app which is based on Xamarin sample code. There should only be one MasterDetailPage in your app.

    In the constructor for App.cs set MainPage = new MasterDetailRootPage(). In the MasterDetaiRootPage, you then create a NavigationPage for the MasterDetaiRootPage.Master and MasterDetaiRootPage.Detail repectively. Use references to those either the master navigation or detail navigation to push/pop pages for navigation. Also, If you want to replace a detail page, for example, and not push/pop, then replace the detail navigation with a new NavigationPage containing a page as content.

    Below is an edited version of my MasterDetailRootPage. It contains the basic logic of creating the MasterDetailPage and includes impelemntation for a full-screen login page which you may or may not need.

    public class MasterDetailRootPage : MasterDetailPage
    {
        public NavigationPage MasterNavigation;
        public NavigationPage DetailNavigation;
        public bool IsAuthenticated;
        public bool IsTesting;
        private LoginPage _loginPage;
    
        protected override void OnAppearing()
        {
            base.OnAppearing();
    
            IsGestureEnabled = false;
            //IsAuthenticated = true;         // For testing set true
            CheckAuthenticationStatus();
            if (IsAuthenticated)
            {
                if (HasLocalData())
                {
                    LoadData();
                }
                else
                {
                    DisplayAlert("Alert", "App does not have any data. Tap \"Update\" to download data.", "OK");
                }
            }
        }
    
        public MasterDetailRootPage()
        {
            Master = MasterContent();
            Detail = DetailContent();
        }
    
        private NavigationPage MasterContent()
        {
            MasterPage_FormTypes masterPage = new MasterPage_FormTypes {Title = "Forms"};
            MasterNavigation = new NavigationPage(masterPage) {Title = "Menu"};
            return MasterNavigation;
        }
    
        private NavigationPage DetailContent()
        {
            HomePage homePage = new HomePage();
            DetailNavigation = new NavigationPage(homePage);
            return DetailNavigation;
        }
    
        #region User Login
        private void CheckAuthenticationStatus()
        {
            if (IsAuthenticated)
            {
                return;
            }
            PresentLoginPage();
        }
    
        public void PresentLoginPage()
        {
            if (_loginPage == null)
            {
                _loginPage = new LoginPage(this);
            }
    
            Navigation.PushModalAsync(_loginPage, false);
        }
        #endregion
    }
    

    Also included is a class RootHelper that I can access globally to get references to master or detail navigation stack. For example, to get a reference to the master navigation page I would use:

    RootHelper.RootPage().Master

    RootHelper also shows some examples of specifc methods to configure the detail navigation.

    public static class RootHelper
    {
        public static MasterDetailRootPage RootPage()
        {
            return (MasterDetailRootPage) Application.Current.MainPage;
        } 
    
        public static void NavigateToHomePage()
        {
            try
            {
                HomePage homePage = new HomePage();
                MasterDetailRootPage masterDetailRootPage = (MasterDetailRootPage)Application.Current.MainPage;
                masterDetailRootPage.Detail = new NavigationPage(homePage);
                masterDetailRootPage.IsPresented = false;
            }
            catch (Exception ex)
            {
                Debug.WriteLine("!!! NavigateToHomePage() Exception !!!");
                Debug.WriteLine("Exception Description: " + ex);
            }
        }
    }
    

    Hope this helps!

  • PranayKothariPranayKothari USMember

    Thanks @Smaths, your solution works well.

  • Mini_TitanMini_Titan Member ✭✭

    Merci @nthing ta réponse a résolu mon problème

  • ledragonledragon Member ✭✭✭

    Hi there,

    Dose anyone now how to get the current Detail page from the MasterDetail?

    I've seen the following:

    var masterDetail = (MasterDetailPage)app.MainPage;
        var detail = (NavigationPage)masterDetail.Detail;
    

    But what if your MasterDetail isn't the MainPage?

    Thanks in advance

Sign In or Register to comment.