15

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.

BinaryTie
  • 281
  • 1
  • 21
  • 49

3 Answers3

35

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

NearHuscarl
  • 66,950
  • 18
  • 261
  • 230
  • 1
    Thanks man you helped me alot! – Kajot Dec 06 '21 at 12:59
  • Omg, thanks. Why is this not checked by default? That gave me a straightforward answer I was looking for and I fixed the error in seconds. – Eriml Feb 23 '22 at 21:38
  • wait what... WHAT? I have been programming an android app for 2 months now with painfully debugging errors, now trying an iOS app with a weird error that brought me here. Now with this option I see a totally different error :0 thanks man! – RolandMakkelie Mar 08 '22 at 14:21
  • Thank you, the exceptions now are totally straightforward. There was a service that i forgot to register which resulted in a null exception. – Ladrillo Mar 25 '22 at 17:07
  • Thank you! you just saved my life, my friend! – Bryce Friha May 01 '22 at 14:09
  • @NearHuscarl you get a gold star! If I could upvote this answer a thousand times I would. Absolutely one of the best pieces of information I have come across. Anybody out there doing Xamarin Forms development, do yourself a favor and turn this on. It will save you A LOT of time and heartache. – jfielaidof May 09 '22 at 20:53
  • you're a star! that saved me and everyone else hours of debugging issues! – Alaa Abouzeid Jan 10 '23 at 17:49
14

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

Wes
  • 1,059
  • 13
  • 18
  • 6
    Yes, Wes the same here. In my instance, it was because my "{StaticResource ...}" or "DymanicResource" was not being defined in your ResourceDictionary section of XAML. – Damian Jul 06 '18 at 18:11
  • For me, the only change made to the xaml which triggered this error on iOS, was setting a custom Entry's Keyboard property from the code-behind. Works fine on Android, not iOS 15.x devices/emulators. #xamarin-hacks required – chri3g91 Dec 06 '22 at 13:01
10

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.

kyurkchyan
  • 2,260
  • 2
  • 23
  • 37
  • Thank you for your response , actually I was tried to implement CarouselView yesterday, but unfortunately when I updated to latest version there wasn't any suggestions about it. I updated to stable version, not prerelease. – BinaryTie May 26 '16 at 10:39
  • It's not in Xamarin.Forms. It's in XLabs open source project. I've posted the link. Take a look. – kyurkchyan May 26 '16 at 12:03
  • Have a look at my Edit, you can grab a sample carousel project. – kyurkchyan May 26 '16 at 13:09