Xamarin Forms:System.Reflection.TargetInvocationException:调用目标抛出了异常

Bin*_*Tie 6 c# xaml xamarin xamarin.forms

我正在努力解决这个问题.我在这里创建了一个简单的跨平台页面是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>
Run Code Online (Sandbox Code Playgroud)

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

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

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

我在我的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>
Run Code Online (Sandbox Code Playgroud)

在这一行上: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));
        }
    }
Run Code Online (Sandbox Code Playgroud)

错误:

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

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

Nea*_*arl 38

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

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

在此输入图像描述


kyu*_*yan 6

你的Carousel页面有错误

<?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>
Run Code Online (Sandbox Code Playgroud)

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

 <Label Text="{Binding MainText}" VerticalOptions="Center" HorizontalOptions="Center" />
Run Code Online (Sandbox Code Playgroud)

如果你想将标签和内容作为轮播,我建议使用像CarouselView这样的东西.

编辑1

我用最新的Xamarin.Forms(2.2.0.31)创建了一个Carousel示例项目,我已经在iOS和Android上测试了它的工作原理.您可以将其用作启动器来实现您的版本.我在生产应用程序中使用此控件.


Wes*_*Wes 5

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

  • 是的,韦斯在这里也是一样。在我的例子中,这是因为我的“{StaticResource ...}”或“DymanicResource”没有在 XAML 的 ResourceDictionary 部分中定义。 (4认同)