c# - Xamarin Master 和 MasterDetailPage InvalidOperationException

标签 c# xamarin xamarin.forms

我不确定为什么会收到此错误我已经阅读了很多有关此错误的信息,听起来它与未设置 xaml 文件中的标题有关。我所有的页面都有标题集。错误来自 app.xaml.cs MainPage= new MainPage();

is there something else that can trigger this exeception?

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

主页.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>

菜单页面.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>

最佳答案

错误消息字面意思是出了什么问题:

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

您已经在 Master 属性中设置了一些内容,如下所示:

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

但您还应该设置 Detail 属性,如下所示:

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

MasterDetailPage 只是一个容器,必须同时具有主控(通常是菜单)和详细信息(从菜单中选择的内容)以显示有用的内容。

关于c# - Xamarin Master 和 MasterDetailPage InvalidOperationException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54908092/

相关文章:

c# - 在 Xamarin.Forms 上将 Activityindicator 居中

xamarin.forms - Xamarin Forms iOS 错误任务图标

c# - 如何抽象出此 Entity Framework IQueryable 查询中的选择?

c# - 在我的网站上编辑图片后如何清除图片缓存?

android - xamarin Visual Studio 2015 apk 安装失败 : Error "parsing the package"

xamarin.forms - Xamarin 表单的 Numericupdown 控件

ios - 在 iOS 上导航时 Xamarin Forms NavigationPage 上一页的一小部分可见

javascript - 错误: [$injector:unpr]

c# - 以编程方式将现有项目添加到新的 VS2012 解决方案失败

c# - 构造函数中的 Xamarin 表单显示警报未显示?