Xamarin形式:System.Reflection.TargetInvocationException:调用的目标引发了异常

编程入门 行业动态 更新时间:2024-10-26 06:28:04
本文介绍了Xamarin形式:System.Reflection.TargetInvocationException:调用的目标引发了异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在为这个问题而苦苦挣扎.我创建了一个简单的跨平台页面,这里是XAML代码:

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="xamarin/schemas/2014/forms" xmlns:x="schemas.microsoft/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.

我在MainPage.xaml中称呼此页面:

I am calling this page in my MainPage.xaml :

<?xml version="1.0" encoding="UTF-8"?> <MasterDetailPage xmlns="xamarin/schemas/2014/forms" xmlns:x="schemas.microsoft/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在执行程序时出现错误:

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)); } }

错误:

未处理的异常:System.Reflection.TargetInvocationException: 调用的目标已引发异常.

Unhandled Exception: System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation.

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

And I have another cross platform page which is same as TestPage.xaml , but it is working when I am executing.

推荐答案

您的轮播"页面有误

<?xml version="1.0" encoding="utf-8" ?> <CarouselPage xmlns="xamarin/schemas/2014/forms" xmlns:x="schemas.microsoft/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>

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

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" />

如果您想在转盘中同时包含标签和内容,我建议您使用 CarouselView .

If you want to have both label and content in a Carousel, I would suggest using something like CarouselView.

编辑1

我已经用最新的Xamarin.Forms(2.2.0.31)创建了示例轮播项目. ),我已经在iOS和Android上对其进行了测试,并且可以正常工作.您可以将其用作实现您的版本的入门.我在生产应用中使用了此控件.

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.

更多推荐

Xamarin形式:System.Reflection.TargetInvocationException:调用的目标引发了异常

本文发布于:2023-11-04 17:36:18,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1558577.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:引发了   异常   形式   目标   Xamarin

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!