C# Xamarin Forms:System.Reflection.TargetInvocationException:调用的目标已引发异常
我正在努力解决这个问题。我只创建了一个简单的跨平台页面,下面是XAML代码:C# Xamarin Forms:System.Reflection.TargetInvocationException:调用的目标已引发异常,c#,xaml,xamarin,xamarin.forms,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.T
<?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>
为了测试,我创建了简单的标签,但即使没有标签,它也不起作用
我在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>
错误:
未处理的异常:System.Reflection.TargetInvocationException:
调用的目标已引发异常
我还有另一个跨平台页面,它与TestPage.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>
旋转木马页面应该只有一个子页面,并且应该是内容页面,您将无法同时添加标签和内容页面。删除此行
<Label Text="{Binding MainText}" VerticalOptions="Center" HorizontalOptions="Center" />
如果您希望在旋转木马中同时包含标签和内容,我建议您使用类似的方式
编辑1
我用最新的Xamarin.Forms(2.2.0.31)创建了一个应用程序,我在iOS和Android上进行了测试,效果良好。您可以使用它作为启动程序来实现您的版本。我在生产应用程序中使用此控件。一般来说,我注意到XAML中的任何语法错误都可能显示为此异常。要建立此异常,您可以告诉Visual Studio在任何异常时自动断开,以使错误消息更清晰:
- 转到
Debug
Windows
打开异常设置窗口异常设置
- 选中基本异常
系统的复选框。异常
- 再次开始调试
感谢您的回复,事实上我昨天尝试实施CarouselView,但不幸的是,当我更新到最新版本时,没有任何建议。我更新到了稳定版,不是预发布版,不是Xamarin.Forms。它在XLabs开源项目中。我已经发布了链接。看一看。看一看我的编辑,你可以抓取一个旋转木马项目的样本。是的,我们在这里也一样。在我的例子中,这是因为我的“{StaticResource…}”或“DymanicResource”没有在XAML的ResourceDictionary部分中定义。
<?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" />