简体   繁体   中英

An exception of type 'System.Reflection.TargetInvocationException' occurred in mscorlib.ni.dll but was not handled in user code: in Xamarin Form

I created the MasterDetailPage in Xamarin form but when i tried to run the app it gave me the above error. Below is the Xaml code for my MasterDetailPage

<?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:App2;assembly=App2"
         x:Class="App2.ParentsMenuPage">
<MasterDetailPage.Master>
<pages:ParentMasterPage x:Name="masterPage" />
</MasterDetailPage.Master>
<MasterDetailPage.Detail>
<NavigationPage>
  <x:Arguments>
    <pages:ParentHomePage />
   </x:Arguments>
  </NavigationPage>
</MasterDetailPage.Detail>
</MasterDetailPage>


 The Xaml code for My Master Page is below


 <?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="App2.ParentMasterPage" Padding="0,40,0,0">
<ContentPage.Content>
<StackLayout VerticalOptions="FillAndExpand">
  <ListView x:Name="listView" VerticalOptions="FillAndExpand" SeparatorVisibility="None">
    <ListView.ItemTemplate>
      <DataTemplate>
        <ImageCell Text="{Binding Title}" ImageSource="{Binding IconSource}" />
      </DataTemplate>
    </ListView.ItemTemplate>
   </ListView>
  </StackLayout>
</ContentPage.Content>

The code-behind for the master page is below

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using Xamarin.Forms;

namespace App2
{
public class ParentMasterItem
{
    public string Title { get; set; }
    public string IconSource { get; set; }
    public Type TargetType { get; set; }
}

public partial class ParentMasterPage : ContentPage
{
    public ListView ListView { get { return listView; } }

    public ParentMasterPage()
    {
        InitializeComponent();

        var masterPageItems = new List<ParentMasterItem>();
        masterPageItems.Add(new ParentMasterItem
        {
            Title = "Home",
            IconSource = "images/home-small.png",
            TargetType = typeof(ParentHomePage)
        });
        masterPageItems.Add(new ParentMasterItem
        {
            Title = "My Ward's",
            IconSource = "images/ward-small.png",
            TargetType = typeof(ParentWardsPage)
        });
        masterPageItems.Add(new ParentMasterItem
        {
            Title = "Chat",
            IconSource = "images/chat-small.png",
            TargetType = typeof(ParentChatPage)
        });

        listView.ItemsSource = masterPageItems;
       }
      }
    }

Then Detail Page

 <?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="App2.ParentHomePage">

 <ContentPage.Content>
   <StackLayout>
    <StackLayout.Children>
     <Frame OutlineColor="Accent">
       <Frame.Content>
         <StackLayout Orientation="Horizontal">
          <StackLayout.Children>
            <Label Text="Home Page"
                   VerticalOptions="Center" />
          </StackLayout.Children>
         </StackLayout>
       </Frame.Content>
     </Frame>

  </StackLayout.Children>
</StackLayout>
</ContentPage.Content>
</ContentPage>

Please can anyone help me figure out where the problem is

  1. MyMasterPage should have a Title.
  2. Take in Consideration that inside the MasterDetailsPage.Master Tags the Content Page Should Have a Title.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM