user5843610
user5843610

Reputation:

Xamarin Master and MasterDetailPage InvalidOperationException

I am not sure why I am getting this error I have read alot about this error and sounds like it is related to the title in the xaml file not being set. All my pages have the title set. The error is coming from the app.xaml.cs MainPage= new MainPage();

is there something else that can trigger this exeception?

System.InvalidOperationException: Master and Detail must be set before adding MasterDetailPage to a container

MainPage.xaml

<?xml version="1.0" encoding="utf-8" ?>
<MasterDetailPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:pages="clr-namespace:ReimbursementApp.Pages"
             x:Class="ReimbursementApp.Pages.MainPage"
             xmlns:telerikPrimitives="clr-namespace:Telerik.XamarinForms.Primitives;assembly=Telerik.XamarinForms.Primitives"
             Title="Main">

    <MasterDetailPage.Master>
        <pages:MenuPage />
    </MasterDetailPage.Master>

</MasterDetailPage>

MenuPage.xaml

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="ReimbursementApp.Pages.MenuPage"
             xmlns:telerikPrimitives="clr-namespace:Telerik.XamarinForms.Primitives;assembly=Telerik.XamarinForms.Primitives"
             xmlns:telerik="clr-namespace:Telerik.XamarinForms.DataControls;assembly=Telerik.XamarinForms.DataControls"
             Title="Menu">
    <!--<ContentPage.ToolbarItems>
        <ToolbarItem Activated="OnToolbarButtonClick" Order="Primary" Priority="0" Icon="hamburgerButtonIcon"/>
    </ContentPage.ToolbarItems>
    <Grid>
        <telerikPrimitives:RadSideDrawer x:Name="drawer" DrawerLength="250">
        </telerikPrimitives:RadSideDrawer>
    </Grid>-->

    <StackLayout VerticalOptions="FillAndExpand">
        <ListView x:Name="ListViewMenu"
                    HasUnevenRows="True">
            <ListView.ItemTemplate>
                <DataTemplate>
                    <ViewCell>
                        <Grid Padding="10">
                            <Label Text="{Binding Title}" FontSize="20"/>
                        </Grid>
                    </ViewCell>
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>
    </StackLayout>
</ContentPage>

Upvotes: 1

Views: 914

Answers (2)

MREme
MREme

Reputation: 18

I have also seen this error occur if the page designated as the MasterDetailPage does not load correctly i.e. there may be errors in your xaml on that page or even errors found loading a static resource specified in your App.xaml. This was the case for me and fixing the errors resolved my problem.

Upvotes: 0

Gerald Versluis
Gerald Versluis

Reputation: 34013

The error message is literally saying what's wrong:

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

You already have set something in the Master property, like this:

<MasterDetailPage.Master> 
    <pages:MenuPage />
</MasterDetailPage.Master>

But you should also set the Detail property, something like this:

<MasterDetailPage.Detail> 
    <pages:MyMainPage />
</MasterDetailPage.Detail>

The MasterDetailPage is only a container and must have both a master (usually a menu) and a detail (something picked from the menu) to show something useful.

Upvotes: 2

Related Questions