How can we select the default page on a MasterDetail project?

ClaudyDetryClaudyDetry USMember ✭✭

I am new to Xamarin.Forms and have created a new project from the MasterDetail template.

I have added a new content page to the project an made the required changes to add it the menu.
All is fine except that I cannot find the way to make my HomePage the default page that is loaded when the app is launched.

Any advice will be greatly appreciated :smile:

Best Answer

  • ColeXColeX Xamurai
    edited January 18 Accepted Answer

    You can set it code behind like @AndreiMisiukevich_ said , and you can also set it in xaml .

    <MasterDetailPage xmlns="http://xamarin.com/schemas/2014/forms"
                      xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
                      xmlns:local="clr-namespace:MasterDetailPageNavigation;assembly=MasterDetailPageNavigation"
                      x:Class="MasterDetailPageNavigation.MainPage">
        <MasterDetailPage.Master>
            <local:MasterPage x:Name="masterPage" />
        </MasterDetailPage.Master>
        /******* this part**********/
        <MasterDetailPage.Detail>
            <NavigationPage>
                <x:Arguments>
                    <local:ContactsPage />
                </x:Arguments>
            </NavigationPage>
        </MasterDetailPage.Detail>
       /*****************/
    </MasterDetailPage>
    

    Refer https://docs.microsoft.com/en-us/xamarin/xamarin-forms/app-fundamentals/navigation/master-detail-page#creating-a-masterdetailpage

Answers

  • AndreiMisiukevich_AndreiMisiukevich_ USMember ✭✭✭✭

    masterDetailPage.Detail = new HomePage();

  • ColeXColeX Member, Xamarin Team Xamurai
    edited January 18 Accepted Answer

    You can set it code behind like @AndreiMisiukevich_ said , and you can also set it in xaml .

    <MasterDetailPage xmlns="http://xamarin.com/schemas/2014/forms"
                      xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
                      xmlns:local="clr-namespace:MasterDetailPageNavigation;assembly=MasterDetailPageNavigation"
                      x:Class="MasterDetailPageNavigation.MainPage">
        <MasterDetailPage.Master>
            <local:MasterPage x:Name="masterPage" />
        </MasterDetailPage.Master>
        /******* this part**********/
        <MasterDetailPage.Detail>
            <NavigationPage>
                <x:Arguments>
                    <local:ContactsPage />
                </x:Arguments>
            </NavigationPage>
        </MasterDetailPage.Detail>
       /*****************/
    </MasterDetailPage>
    

    Refer https://docs.microsoft.com/en-us/xamarin/xamarin-forms/app-fundamentals/navigation/master-detail-page#creating-a-masterdetailpage

  • ClaudyDetryClaudyDetry USMember ✭✭

    @AndreiMisiukevich_ said:
    masterDetailPage.Detail = new HomePage();

    Dear Andrei,

    Thank you very much for your help.

    I tried to add "masterDetailPage.Detail = new HomePage();" in MainPage code but masterDetailPage object is unknown.
    It may not be the right place to add it...

    Anyway, I solved the problem through the xaml.

    ` public MainPage()
    {
    InitializeComponent();

            MasterBehavior = MasterBehavior.Popover;
    
            MenuPages.Add((int)MenuItemType.Browse, (NavigationPage)Detail);
    
            masterDetailPage.Detail = new HomePage();
    
        }`
    
  • ClaudyDetryClaudyDetry USMember ✭✭
    edited January 18

    'Problem' solved through the xaml, thank you!

Sign In or Register to comment.