[英]Xamarin Forms System.Reflection.TargetInvocationException: 'Exception has been thrown by the target of an invocation.'
[英]Xamarin Forms: System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation
我正在努力解决这个问题。 我创建了一个简单的跨平台页面,这里是 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 相同的跨平台页面,但它在我执行时正在工作。
通常,我注意到 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>
Carousel 页面应该只有一个子页面,并且它应该是一个 ContentPage,您将无法同时添加标签和内容页面。 删除此行
<Label Text="{Binding MainText}" VerticalOptions="Center" HorizontalOptions="Center" />
如果您想在 Carousel 中同时包含标签和内容,我建议您使用CarouselView 之类的东西。
编辑 1
我已经使用最新的 Xamarin.Forms (2.2.0.31) 创建了一个示例 Carousel 项目,我已经在 iOS 和 Android 上对其进行了测试,并且可以正常工作。 您可以将其用作启动器来实现您的版本。 我在生产应用程序中使用此控件。
要建立在@Wes answer之上,您可以通过告诉 Visual Studio 在任何异常时自动中断来使错误消息更清晰:
Debug
> Windows
> Exception Settings
以打开异常设置窗口System.Exception
的复选框
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.