Xamarin Master和MasterDetailPage InvalidOperationException

时间:2019-02-27 14:48:42

标签: c# xamarin xamarin.forms

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

  

还有其他东西可以触发这种观念吗?

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

MainPage.xaml

import {Component, Input, OnInit} from '@angular/core';

@Component({
  selector: 'app-button-icon-text',
  templateUrl: './button-icon-text.component.html',
  styleUrls: ['./button-icon-text.component.css']
})
export class ButtonIconTextComponent implements OnInit {

  @Input()
  text;

  @Input()
  icon;

  constructor() {
  }

  ngOnInit() {
  }

}

MenuPage.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>

2 个答案:

答案 0 :(得分:3)

错误消息字面意思是什么:

  

在将MasterDetailPage添加到容器之前,必须先设置“ Master and Detail”

您已经在Master属性中进行了设置,如下所示:

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

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

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

MasterDetailPage只是一个容器,必须具有母版(通常是菜单)和详细信息(从菜单中选取的内容)才能显示有用的内容。

答案 1 :(得分:0)

我还看到如果指定为MasterDetailPage的页面无法正确加载,即会出现此错误,即该页面上的xaml中可能存在错误,甚至加载App.xaml中指定的静态资源时也可能发现错误。对我来说就是这种情况,解决错误可以解决我的问题。