简体   繁体   English

Xamarin 表单:System.Reflection.TargetInvocationException:调用的目标已抛出异常

[英]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:我创建了一个简单的跨平台页面,这里是 XAML 代码:

<?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 :我在 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:在这一行类中: ForTesting.MainPage.xaml.g.cs 我在执行程序时遇到错误:

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.未处理的异常:System.Reflection.TargetInvocationException:调用的目标已抛出异常。

And I have another cross platform page which is same as TestPage.xaml , but it is working when I am executing.我还有另一个与 TestPage.xaml 相同的跨平台页面,但它在我执行时正在工作。

通常,我注意到 XAML 中的任何语法错误都可能显示为此异常。

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. Carousel 页面应该只有一个子页面,并且它应该是一个 ContentPage,您将无法同时添加标签和内容页面。 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 .如果您想在 Carousel 中同时包含标签和内容,我建议您使用CarouselView 之类的东西。

EDIT 1编辑 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.我已经使用最新的 Xamarin.Forms (2.2.0.31) 创建了一个示例 Carousel 项目,我已经在 iOS 和 Android 上对其进行了测试,并且可以正常工作。 You can use it as a starter to implement your version.您可以将其用作启动器来实现您的版本。 I use this control in production app.我在生产应用程序中使用此控件。

To build upon @Wes answer , you can make the error message clearer by telling Visual Studio to automatically break at any exception:要建立在@Wes answer之上,您可以通过告诉 Visual Studio 在任何异常时自动中断来使错误消息更清晰:

  • Go to Debug > Windows > Exception Settings to open the Exception Settings window转到Debug > Windows > Exception Settings以打开异常设置窗口
  • Select the checkbox for the base exception System.Exception选中基本异常System.Exception的复选框
  • Start debugging again.重新开始调试。

在此处输入图片说明

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Xamarin Forms System.Reflection.TargetInvocationException: &#39;调用的目标已抛出异常。&#39; - Xamarin Forms System.Reflection.TargetInvocationException: 'Exception has been thrown by the target of an invocation.' System.Reflection.TargetInvocationException: &#39;调用的目标已抛出异常 - System.Reflection.TargetInvocationException: 'Exception has been thrown by the target of an invocation System.Reflection.TargetInvocationException :调用的目标已抛出异常 - System.Reflection.TargetInvocationException : Exception has been thrown by the target of an invocation System.Reflection.TargetInvocationException:调用的目标已引发异常 - System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation Xamarin.form-Masterdetailpage:System.Reflection.TargetInvocationException:调用的目标已引发异常 - Xamarin.form - Masterdetailpage : System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation Xamarin System.Reflection.TargetInvocationException:调用的目标已引发异常 - Xamarin System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation 为什么我收到异常 System.Reflection.TargetInvocationException: &#39;Exception has been throwed by the target of an invocation.&#39;? - Why I'm getting exception System.Reflection.TargetInvocationException: 'Exception has been thrown by the target of an invocation.'? 使用 Task.Run 时如何修复“System.Reflection.TargetInvocationException: 'Exception has been throwed by the target of an invocation.'” - How to fix “System.Reflection.TargetInvocationException: 'Exception has been thrown by the target of an invocation.'” when use Task.Run 如何修复“System.Reflection.TargetInvocationException已被抛出” - How to fix “System.Reflection.TargetInvocationException has been thrown” 目标调用是否引发了异常? Xamarin形式 - Exception has been thrown by the target of an invocation? Xamarin Forms
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM