【问题标题】:Xamarin Forms: System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocationXamarin Forms:System.Reflection.TargetInvocationException:调用的目标已引发异常
【发布时间】:2016-05-26 08:09:33
【问题描述】:

我正在努力解决这个问题。我只创建了一个简单的跨平台页面,这里是 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>

这是相同的跨平台页面类:

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

为了测试,我创建了简单的标签,但即使没有标签,它也不起作用。

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

在这一行类: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));
        }
    }

错误:

未处理的异常:System.Reflection.TargetInvocationException: 调用的目标已抛出异常。

我还有另一个与 TestPage.xaml 相同的跨平台页面,但在我执行时它正在工作。

【问题讨论】:

    标签: c# xaml xamarin xamarin.forms


    【解决方案1】:

    要以@Wes answer 为基础,您可以通过告诉 Visual Studio 在任何异常时自动中断来使错误消息更清晰:

    • 转到Debug > Windows > Exception Settings 打开异常设置窗口
    • 选中基本异常System.Exception的复选框
    • 再次开始调试。

    【讨论】:

    • 谢谢你帮了我很多忙!
    • 天哪,谢谢。为什么默认不勾选这个?这给了我一个我正在寻找的直截了当的答案,我在几秒钟内就修复了错误。
    • 等等……什么?我已经编写一个 android 应用程序 2 个月了,但调试错误很痛苦,现在尝试一个带有奇怪错误的 iOS 应用程序把我带到这里。现在有了这个选项,我看到了一个完全不同的错误:0 谢谢伙计!
    【解决方案2】:

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

    【讨论】:

    • 是的,这里也一样。在我的例子中,这是因为我的“{StaticResource ...}”或“DynamicResource”没有在 XAML 的 ResourceDictionary 部分中定义。
    【解决方案3】:

    你的轮播页面有错误

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

    轮播页面应该只有一个子页面,并且应该是一个内容页面,您将无法同时添加标签和内容页面。删除此行

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

    如果你想在轮播中同时拥有标签和内容,我建议使用CarouselView 之类的东西。

    编辑 1

    我已经使用最新的 Xamarin.Forms (2.2.0.31) 创建了一个 sample Carousel project,我已经在 iOS 和 Android 上对其进行了测试,并且可以正常工作。您可以使用它作为启动器来实现您的版本。我在生产应用程序中使用此控件。

    【讨论】:

    • 感谢您的回复,其实我昨天尝试实现 CarouselView,但不幸的是当我更新到最新版本时没有任何建议。我更新到稳定版,而不是预发布版。
    • 它不在 Xamarin.Forms 中。它在 XLabs 开源项目中。我已经发布了链接。看看吧。
    • 看看我的编辑,你可以获取一个示例轮播项目。
    猜你喜欢
    • 2018-11-08
    • 2021-03-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-09
    • 2019-01-22
    • 2018-02-14
    • 1970-01-01
    相关资源
    最近更新 更多