![](/img/trans.png)
[英]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.