调用 xamarin 的目标已抛出异常

Exception has been thrown by the target of an invocation xamarin

我正在尝试构建主详细信息页面,但我一直收到错误 "Exception has been thrown by the target of an invocation" 并且无法对其进行测试。它在我按下登录按钮移至主页后出现(我将附上屏幕截图)但显然屏幕只是停留在主页上。

代码: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" 
                  x:Class="RoseySports.Home">





            <MasterDetailPage.Master>

        <ContentPage> 
            <AbsoluteLayout VerticalOptions="FillAndExpand" HorizontalOptions="FillAndExpand">

        <Image AbsoluteLayout.LayoutFlags="All" AbsoluteLayout.LayoutBounds="0, 0, 1, 1"
            Source="background.jpg" Aspect="AspectFill"/>

        <ScrollView AbsoluteLayout.LayoutFlags="All" AbsoluteLayout.LayoutBounds="0, 0, 1, 1">



        <StackLayout>
        <Button Text="Create Activity" TextColor="White" Clicked="Handle_Clicked"/>
            <Button Text="Check Availability" TextColor="White"/>
            <Button Text="Check Invitations" TextColor="White"/>
            <Button x:Name="SMA" Text="Propose Saturday  Morning Activity" TextColor="White"/>
            <Button x:Name="Logout" Text="Logout" Clicked="Handle_Clicked_1" TextColor="White" Margin="20"/>
        </StackLayout>
        </ScrollView>
        </AbsoluteLayout>
            </ContentPage>
                </MasterDetailPage.Master>


            <MasterDetailPage.Detail>

            <ContentPage>

        <AbsoluteLayout VerticalOptions="FillAndExpand" HorizontalOptions="FillAndExpand">

        <Image AbsoluteLayout.LayoutFlags="All" AbsoluteLayout.LayoutBounds="0, 0, 1, 1"
            Source="background.jpg" Aspect="AspectFill"/>

        <ScrollView AbsoluteLayout.LayoutFlags="All" AbsoluteLayout.LayoutBounds="0, 0, 1, 1">

                    <ContentView Padding="10,40,10,10">
        <Button Text="Menu" HorizontalOptions="Start" VerticalOptions="Start" TextColor="White" Clicked="Handle_Clicked_2"/>
                        </ContentView>

        </ScrollView>
        </AbsoluteLayout>


        </ContentPage>

            </MasterDetailPage.Detail>
</MasterDetailPage>

代码:C#:

using System;
using System.Collections.Generic;

using Xamarin.Forms;

namespace RoseySports
{
    public partial class Home : MasterDetailPage
    {
        public Home()
        {
            InitializeComponent();

        }

        private void Handle_Clicked(object sender, System.EventArgs e)
        {
            Detail = new Create_Activity();
            IsPresented = false;
        }

        private async void Handle_Clicked_1(object sender, System.EventArgs e)
        {
            await Navigation.PushModalAsync(new Login_Page());
        }

        void Handle_Clicked_2(object sender, System.EventArgs e)
        {
            IsPresented = true;
        }
    }
}

Error MessagePage before error shows up

您缺少 ContentPage Title

首先,您应该打开 Xaml 编译器以帮助捕获 XAML 问题(在编译时 and/or 更好的错误消息,而无需查看 TargetInvocationException 的内部异常)。

[assembly: XamlCompilation(XamlCompilationOptions.Compile)]

回复:XAML Compilation

虽然编译器实际上不会在构建时捕获这个缺少的标题属性,但您会在 运行 时收到错误消息:

`Title property must be set on Master page`

因此,在您的 ContentPage 上设置标题:

<ContentPage Title="Some Page Title">

示例:

<MasterDetailPage.Master>
    <ContentPage Title="Some Page Title">
        <AbsoluteLayout VerticalOptions="FillAndExpand" HorizontalOptions="FillAndExpand">
            <Image AbsoluteLayout.LayoutFlags="All" AbsoluteLayout.LayoutBounds="0, 0, 1, 1" Source="background.jpg" Aspect="AspectFill"/>
            <ScrollView AbsoluteLayout.LayoutFlags="All" AbsoluteLayout.LayoutBounds="0, 0, 1, 1">
                <StackLayout>
                    <Button Text="Create Activity" TextColor="White" Clicked="Handle_Clicked"/>
                    <Button Text="Check Availability" TextColor="White"/>
                    <Button Text="Check Invitations" TextColor="White"/>
                    <Button x:Name="SMA" Text="Propose Saturday  Morning Activity" TextColor="White"/>
                    <Button x:Name="Logout" Text="Logout" Clicked="Handle_Clicked_1" TextColor="White" Margin="20"/>
                </StackLayout>
            </ScrollView>
        </AbsoluteLayout>
    </ContentPage>
</MasterDetailPage.Master>
<MasterDetailPage.Detail>
    <ContentPage>
        <AbsoluteLayout VerticalOptions="FillAndExpand" HorizontalOptions="FillAndExpand">
            <Image AbsoluteLayout.LayoutFlags="All" AbsoluteLayout.LayoutBounds="0, 0, 1, 1" Source="background.jpg" Aspect="AspectFill"/>
                <ScrollView AbsoluteLayout.LayoutFlags="All" AbsoluteLayout.LayoutBounds="0, 0, 1, 1">
                    <ContentView Padding="10,40,10,10">
                        <Button Text="Menu" HorizontalOptions="Start" VerticalOptions="Start" TextColor="White" Clicked="Handle_Clicked_2"/>
                    </ContentView>
                </ScrollView>
        </AbsoluteLayout>
    </ContentPage>
</MasterDetailPage.Detail>