Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Xamarin Forms: System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation

I am struggling with this issue. I created just a simple cross platform page here is XAML code:

<?xml version="1.0" encoding="utf-8" ?>
<CarouselPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="ForTesting.TestPage">
  <Label Text="{Binding MainText}" VerticalOptions="Center" HorizontalOptions="Center" />
  <ContentPage>
    <ContentPage.Padding>
      <OnPlatform x:TypeArguments="Thickness" iOS="0,40,0,0" Android="0,40,0,0" />
    </ContentPage.Padding>
  </ContentPage>
</CarouselPage>

And here is same cross platform page class:

public partial class TestPage: CarouselPage
    {
        public TestPage()
        {
            InitializeComponent();
            new Label
            {
                Text = "heelow",
                FontSize = Device.GetNamedSize(NamedSize.Medium, typeof(Label)),
                HorizontalOptions = LayoutOptions.Center
            };
         }
    }

For testing I created simple label, but even without label it is doesn't work.

I am calling this page in my MainPage.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:local="clr-namespace:ForTesting;assembly=ForTesting"
                  x:Class="ForTesting.MainPage"
          MasterBehavior="Popover">
  <ContentPage.ToolbarItems>
    <ToolbarItem x:Name="CClick"
                 Text="C :"
                 Order="Primary">
    </ToolbarItem>
  </ContentPage.ToolbarItems>
  <MasterDetailPage.Master>
    <local:MasterPage x:Name="masterPage" />
  </MasterDetailPage.Master>
  <MasterDetailPage.Detail>
    <NavigationPage>
      <x:Arguments>
        <local:TestPage/>
      </x:Arguments>
    </NavigationPage>
  </MasterDetailPage.Detail>
</MasterDetailPage>

And on this line of class: ForTesting.MainPage.xaml.g.cs I am getting error when I am executing program:

public partial class MainPage : global::Xamarin.Forms.MasterDetailPage {

        [System.CodeDom.Compiler.GeneratedCodeAttribute("Xamarin.Forms.Build.Tasks.XamlG", "0.0.0.0")]
        private global::Xamarin.Forms.ToolbarItem CClick;

        [System.CodeDom.Compiler.GeneratedCodeAttribute("Xamarin.Forms.Build.Tasks.XamlG", "0.0.0.0")]
        private global::ForTesting.MasterPage masterPage;

        [System.CodeDom.Compiler.GeneratedCodeAttribute("Xamarin.Forms.Build.Tasks.XamlG", "0.0.0.0")]
        private void InitializeComponent() {
-->         this.LoadFromXaml(typeof(MainPage));
        }
    }

Error:

Unhandled Exception: System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation.

And I have another cross platform page which is same as TestPage.xaml , but it is working when I am executing.

like image 803
BinaryTie Avatar asked May 26 '16 08:05

BinaryTie


3 Answers

To build upon @Wes answer, you can make the error message clearer by telling Visual Studio to automatically break at any exception:

  • Go to Debug > Windows > Exception Settings to open the Exception Settings window
  • Select the checkbox for the base exception System.Exception
  • Start debugging again.

enter image description here

like image 94
NearHuscarl Avatar answered Nov 11 '22 13:11

NearHuscarl


In general, I've noticed that any syntax errors in XAML may show up as this exception.

like image 39
Wes Avatar answered Nov 11 '22 12:11

Wes


You have mistake in your Carousel page

<?xml version="1.0" encoding="utf-8" ?>
<CarouselPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="ForTesting.TestPage">
  <Label Text="{Binding MainText}" VerticalOptions="Center" HorizontalOptions="Center" />
  <ContentPage>
    <ContentPage.Padding>
      <OnPlatform x:TypeArguments="Thickness" iOS="0,40,0,0" Android="0,40,0,0" />
    </ContentPage.Padding>
  </ContentPage>
</CarouselPage>

Carousel page should have only one child, and it should be a ContentPage, you won't be able to add both label and content page. Remove this line

 <Label Text="{Binding MainText}" VerticalOptions="Center" HorizontalOptions="Center" />

If you want to have both label and content in a Carousel, I would suggest using something like CarouselView.

EDIT 1

I've create a sample Carousel project with latest Xamarin.Forms (2.2.0.31), I've tested it on iOS and Android and it works. You can use it as a starter to implement your version. I use this control in production app.

like image 10
kyurkchyan Avatar answered Nov 11 '22 11:11

kyurkchyan