Mohamed Al Sabbagh
Mohamed Al Sabbagh

Reputation: 91

Exception has been thrown by the target of an invocation xamarin

I'm trying to build a Master Detail Page, but I keep getting the error "Exception has been thrown by the target of an invocation" and can't test it out. It shows up after I press the login button to move onto the home page (I'll attach screenshots) but obviously the screen just stays on the home page.

Code: 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>

Code: 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

Upvotes: 2

Views: 1818

Answers (1)

SushiHangover
SushiHangover

Reputation: 74124

You are missing the ContentPage Title.

First you should turn on the Xaml compiler to aid in catching XAML problems (at compile time and/or better errors messages without looking through the inner exceptions of TargetInvocationException).

[assembly: XamlCompilation(XamlCompilationOptions.Compile)]

re: XAML Compilation

While the compiler will not actually catch this missing title attribute at build time, you will get the error message at run time:

`Title property must be set on Master page`

So, set the title on your ContentPage:

<ContentPage Title="Some Page Title">

Example:

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

Upvotes: 3

Related Questions