Press ESC to close

Xamarin Forms From Master Page to Another Page

Hello friends. In this article, I’m going to talk about a problem that I’ve been standing on. This is the problem that we cannot go back to the previous page when we go to another page of Master Detail Page in Xamarin.Forms. I’m showing a video below for examples.

As you have seen, when you go from Master Page to another page, the back button does not appear. Not much trouble for Android. Because the phones already have back buttons. With this button you can exit. However, this is not possible for iOS devices. Because there are no back buttons. Therefore, different techniques should be used. There are 2 different methods for this. You use it for your project, whichever you prefer. You can create a back button for each page by typing in a Navigation Service. This road is a little more laborious than the other way. The way I will talk about in this article; Changing the Detail Detail page of Master Detail Page continuously. Thus, you can make your project more useful. For example, Ziraat Bank’s mobile application uses such a method.

To make such a project, you need to create a root page. We define a Master Detail Page variable. We need to make a check after defining. With this control, if the variable does not have a value, we need to give it. For example, the Master page of Master Detail Page is always the same. However, the Detail page is always the same at the beginning, but it will vary according to the user’s wishes. Therefore, the Detail page of Master Detail Page is initially null. Because it is empty, we must assign a value by default. We’re doing this here. Then we create a function. You can change the name of this function as desired. I’m creating a function called NavigateTo. With this function I am going to go to the detail page. So I have my personal information and other options on the side.

public class RootPage
    {
        private static MasterDetailPage masterDetailPage { get; set; }
        public static MasterDetailPage MasterDetailPage
        {
            
            get
            {
                if (masterDetailPage == null)
                {
                    LocalUserManager localUserManager = new LocalUserManager();
                    var user = localUserManager.Listele().ToList()[0];
                    masterDetailPage = new MasterDetailPage();
                    masterDetailPage.Master = new AnaSayfaViews.AnaSayfaMaster(user);
                    NavigateTo(new AnaSayfaViews.AnaSayfaDetail(user));
                }

                return masterDetailPage;
            }
            set
            {
                masterDetailPage = value;
            }
        }

        public static void NavigateTo(Page page)
        {
            MasterDetailPage.Detail = new NavigationPage(page)
            {
                BarBackgroundColor = Color.Green,
                BarTextColor = Color.White,
            };
            MasterDetailPage.IsPresented = false;
        }
    }
}

You can use this method if you wish, or you can try the other method in the days ahead. If you have any questions, you can contact me by mail or comment.

Leave a Reply

Your email address will not be published. Required fields are marked *