首页 > c# > Xamarin Master和MasterDetailPage InvalidOperationException

Xamarin Master和MasterDetailPage InvalidOperationException (Xamarin Master and MasterDetailPage InvalidOperationException)

问题

我不知道为什么我得到这个错误我已经读过很多关于这个错误的声音,听起来它与xaml文件中的标题没有设置有关。我的所有页面都有标题集。错误来自app.xaml.cs MainPage = new MainPage();

还有其他什么可以触发这个例外吗?

System.InvalidOperationException:在将MasterDetailPage添加到容器之前,必须设置Master和Detail

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>

解决方法

我还看到如果指定为MasterDetailPage的页面未正确加载,则会发生此错误,即该页面上的xaml中可能存在错误,甚至发现加载App.xaml中指定的静态资源时出错。这是我的情况,修复错误解决了我的问题。

问题

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>

解决方法

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.

相似信息