Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Xamarin - Master and Detail must be set before adding MasterDetailPage to a container

I am trying to add a simple Master Detail page to an already existing Xamarin application. Here is the MasterDetailPage declaration

<MasterDetailPage xmlns="http://xamarin.com/schemas/2014/forms"
                  xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
                  xmlns:pages="clr-namespace:MyCareManager.XamForms.Pages;assembly=MyCareManager.XamForms"
                  x:Class="MyCareManager.XamForms.Pages.SettingsPage">
  <MasterDetailPage.Master>
    <ContentPage Title="This is the test master page"></ContentPage>
  </MasterDetailPage.Master>
  <MasterDetailPage.Detail>
    <NavigationPage>
      <x:Arguments>
        <ContentPage Title="This is a view"></ContentPage>
      </x:Arguments>
    </NavigationPage>
  </MasterDetailPage.Detail>
</MasterDetailPage>

However, when I run the application I get the following error when navigation through to the page :

Master and Detail must be set before adding MasterDetailPage to a container

I am assuming that it is to do with autofac that is being used in the application as an IOC container but havent been able to put a finger to it. Has anyone else experienced this?

like image 871
Farax Avatar asked Jun 24 '16 04:06

Farax


4 Answers

Here is my running code if some one need it :

<?xml version="1.0" encoding="utf-8" ?>
<MasterDetailPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="CrossApp1.MenuPage">
        <MasterDetailPage.Master>
          <ContentPage Title="Menu">
            <StackLayout Orientation="Vertical">
              <Button Text="Sports"/>
              <Button Text="Economy"/>
              <Button Text="Education"/>
              <Button Text="Science"/>
            </StackLayout>
          </ContentPage>
        </MasterDetailPage.Master>

        <MasterDetailPage.Detail>

          <NavigationPage>
            <x:Arguments>
              <ContentPage Title="This is a view"></ContentPage>
            </x:Arguments>
          </NavigationPage>
        </MasterDetailPage.Detail>
</MasterDetailPage>
like image 71
fandro Avatar answered Sep 26 '22 12:09

fandro


I solved this issue by adding Detail section of MasterDetailPage

Like this

<MasterDetailPage.Master>
    <ContentPage Title="Menu">
        <StackLayout Padding="20">
            <Button Text="ViewA"/>
        </StackLayout>
    </ContentPage>
</MasterDetailPage.Master>

<!--Add below code too-->
<MasterDetailPage.Detail>
    <NavigationPage>
        <x:Arguments>
            <ContentPage Title="This is a view"></ContentPage>
        </x:Arguments>
    </NavigationPage>
 </MasterDetailPage.Detail>
like image 37
R15 Avatar answered Sep 25 '22 12:09

R15


I forgot to use InitializeComponent(); on my MasterDetailPage code behind file. In your case that must be SettingsPage. I had commented it out, because he showed me an error one time.

like image 45
testing Avatar answered Sep 24 '22 12:09

testing


You can try this: Open a simple class for Master Detail Page. Set the name MyMasterPage (set the name what you desire).

public class MyMasterPage : MasterDetailPage
    {
        public MyMasterPage()
        {
            this.Master = new MenuPage();//name of your menupage                
            this.Detail = new DetailPage();//name of your detailpage

        }
    }

Now you have your Master Detail Page. Last thing you should add 2 ContentPage one is for Menu Page, other for DetailPage.

like image 43
ayberkzeray Avatar answered Sep 24 '22 12:09

ayberkzeray