调用xamarin的目标引发了异常

时间:2018-05-30 06:35:52

标签: c# xaml xamarin xamarin.forms 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" 
                  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 Message Page before error shows up

1 个答案:

答案 0 :(得分:2)

您错过了ContentPage Title

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

[assembly: XamlCompilation(XamlCompilationOptions.Compile)]

re:XAML Compilation

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

`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>