Am I missing something re. Title properties when using MasterDetailPage?

JohnHardmanJohnHardman GBUniversity mod
edited December 2017 in Xamarin.Forms

When I started working with Xamarin.Forms, I found that MasterDetailPage did not do what I wanted and contained multiple bugs. I therefore wrote something to replace MasterDetailPage in my own code, to get the precise result I wanted.

I thought I'd take a peek at the current state of MasterDetailPage (using XF 2.4.0.38779) to see how it has changed since then.

At https://developer.xamarin.com/guides/xamarin-forms/application-fundamentals/navigation/master-detail-page/
it says:

"The MasterDetailPage.Master property is set to a ContentPage instance. The MasterDetailPage.Detail property is set to a NavigationPage containing a ContentPage instance."

and:

"The MasterDetailPage.Master page must have its Title property set, or an exception will occur."

Following those two guides, and using MasterBehavior.Split, I find that the Title set on the Master page is not displayed. It seems odd that setting it is required if it's not then displayed. Am I missing something?

Also, on that same page, it says:

"MasterDetailPage is designed to be a root page, and using it as a child page in other page types could result in unexpected and inconsistent behavior. In addition, it's recommended that the master page of a MasterDetailPage should always be a ContentPage instance, and that the detail page should only be populated with TabbedPage, NavigationPage, and ContentPage instances. This will help to ensure a consistent user experience across all platforms."

I find that if I use MasterDetailPage as a child page, on UWP the Detail page's Title is displayed twice. Must be one of those "unexpected and inconsistent behaviors". Seems odd though - if it was going to do something like that, I would have expected that to happen on Android and UWP as well. Makes me wonder if UWP is not kept quite up to date with Android and UWP when it comes to MasterDetailPage...

The good thing is that this latest dabble with MasterDetailPage makes me think creating my own replacement was the right thing to do, even if it took a fair amount of work.

For info, the hacky code I used whilst dabbling with this today is as follows:

using Xamarin.Forms;

namespace ViewsUsingXamarinForms
{
    public class MyAppMasterDetailPage : MasterDetailPage
    {
        public MyAppMasterDetailPage()
        {
            Master = new ContentPage
            {
                BackgroundColor = Color.Yellow,
                Content = new StackLayout
                {
                    VerticalOptions = LayoutOptions.Center,
                    Children =
                    {
                        new Label
                        {
                            HorizontalTextAlignment = TextAlignment.Center,
                            FontSize = 30,
                            BackgroundColor = Color.Blue,
                            TextColor = Color.Aqua,
                            Text = "Master"
                        }
                    }
                },
                Title = "Master ContentPage title"
            };

            Detail = new NavigationPage(new ContentPage
            {
                BackgroundColor = Color.Yellow,
                Content = new StackLayout
                {
                    VerticalOptions = LayoutOptions.Center,
                    Children =
                    {
                        new Label
                        {
                            HorizontalTextAlignment = TextAlignment.Center,
                            FontSize = 30,
                            BackgroundColor = Color.Green,
                            TextColor = Color.Lime,
                            Text = "Detail"
                        }
                    }
                },
                Title = "Detail ContentPage title"
            })
            {
                Title = "Detail NavigationPage title"
            };

            Title = "MasterDetailPage title";
            MasterBehavior = MasterBehavior.Split;
        }

    } // public class MyAppMasterDetailPage : MasterDetailPage

} // namespace ViewsUsingXamarinForms

// eof

Best Answer

Answers

  • JohnHardmanJohnHardman GBUniversity mod

    @NMackay - Many thanks. I would raise a bug, but looking at the new way of doing it made my heart sink. Will climb that mountain when I have a bug that is really impacting me...

  • JohnHardmanJohnHardman GBUniversity mod

    Slight error in my original post. The two places I used "Android and UWP" should have said "Android and iOS"

  • JohnHardmanJohnHardman GBUniversity mod

    @DavidOrtinau - Long-term users agreeing that current behavior is wrong makes me think this might be one for your attention :-)

  • gkustasgkustas USMember

    So @NMackay, what you are saying is that this is a known implementation difference, not a bug?

    Can you recommend an alternative method of a "navigation drawer" that works in XF?

    I'm a new Xamarin developer, just getting my feet wet. I have to say I love the whole concept of using C# and Visual Studio, and the architecture looks well thought out. It's just that I can't get anything to work reliably. :-(

Sign In or Register to comment.