如何解决“调用目标已抛出异常”

时间:2020-07-09 13:57:08

标签: android xamarin xamarin.forms

我的Xamarin表单应用程序中的SwipeView有问题。

我在CollectionView中有一个SwipeView。 CollectionView位于StackLayout内部,而StackLayout位于StackView内部的ScrollView中。

   <ScrollView Margin="10,5,10,5" x:Name="MyViewList">
                <StackLayout>
                    <Frame BorderColor="Teal">
                        <StackLayout>
                            <Label Text="Deine Vokabel" HorizontalTextAlignment="Center" FontSize="Large" TextColor="Teal"/>
                            <CollectionView x:Name="VList">
                                <CollectionView.ItemTemplate>
                                    <DataTemplate>
                                        <SwipeView>
                                            <SwipeView.LeftItems>
                                                <SwipeItems>
                                                    <SwipeItem Text="Delete" BackgroundColor="LightPink"/>
                                                    <SwipeItem Text="Edit" BackgroundColor="LightGreen"/>
                                                </SwipeItems>
                                            </SwipeView.LeftItems>
                                            <Grid>
                                                <Grid.ColumnDefinitions>
                                                    <ColumnDefinition 
                                                        Width="*"
                                                    />
                                                </Grid.ColumnDefinitions>

                                                <Grid.RowDefinitions>
                                                    <RowDefinition 
                                                        Height="*"
                                                    />
                                                </Grid.RowDefinitions>
                                                <BoxView Grid.Row="0" Grid.Column="0" Grid.ColumnSpan="2" BackgroundColor="Teal" HeightRequest="1" VerticalOptions="End" HorizontalOptions="FillAndExpand"/>
                                                <Label Grid.Row="1" Grid.Column="0" Text="Number:" FontAttributes="Bold"/>
                                                <Label Grid.Row="1" Grid.Column="1" Text="{Binding MyBinding}"/>
                                                <Label Grid.Row="2" Grid.Column="0" Text="MYText:" FontAttributes="Bold"/>
                                                <Label Grid.Row="2" Grid.Column="1" Text="{Binding MyBinding}"/>
                                                <Label Grid.Row="3" Grid.Column="0" Text="MyText:" FontAttributes="Bold"/>
                                                <Label Grid.Row="3" Grid.Column="1" Text="{Binding MyBinding}"/>
                                                <Label Grid.Row="4" Grid.Column="0" Text="MyText1:" FontAttributes="Bold"/>
                                                <Label Grid.Row="4" Grid.Column="1" Text="{Binding MyBinding}"/>
                                                <Label Grid.Row="5" Grid.Column="0" Text="MyText2:" FontAttributes="Bold"/>
                                                <Label Grid.Row="5" Grid.Column="1" Text="{Binding MyBinding}"/>
                                                <Label Grid.Row="6" Grid.Column="0" Text="MyText:" FontAttributes="Bold"/>
                                                <Label Grid.Row="6" Grid.Column="1" Text="{Binding MyBinding}"/>
                                                <BoxView Grid.Row="7" Grid.Column="0" Grid.ColumnSpan="2" BackgroundColor="Teal" HeightRequest="1" VerticalOptions="End" HorizontalOptions="FillAndExpand"/>
                                            </Grid>
                                        </SwipeView>
                                    </DataTemplate>
                                </CollectionView.ItemTemplate>
                            </CollectionView>
                        </StackLayout>
                    </Frame>
                </StackLayout>
            </ScrollView>

如果我在Android上运行我的应用程序(到目前为止尚未测试iOS),则会出现此错误:

07-09 15:45:28.131 D / Mono(28233):请求加载参考4 (共6个) / storage / emulated / 0 / Android / data / MyApp / files /。覆盖 /Xamarin.Android.Support.v7.RecyclerView.dll 07-09 15:45:28.132 D / Mono(28233):正在加载参考4 / storage / emulated / 0 / Android / data / MyApp / files /。覆盖 /Xamarin.Android.Support.v7.RecyclerView.dll asmctx DEFAULT,寻找Xamarin.Android.Support.CustomView, 版本= 1.0.0.0,文化=中性,PublicKeyToken =空07-09 15:45:28.132 D / Mono(28233):程序集参考addref Xamarin.Android.Support.v7.RecyclerView [0x7a3866a480]-> Xamarin.Android.Support.CustomView [0x7a47e79a80]:2 System.Reflection.TargetInvocationException:'调用的目标已引发异常。'

这是调试中断的屏幕截图:

enter image description here

我使用Xamarin.Forms 4.7和VisualStudio 2019。

我缺少什么?资源中有东西吗? SwipeView是否不能在ScrollView中使用?

2 个答案:

答案 0 :(得分:2)

请确保已为swipeview添加了标志,因为它仍处于实验阶段。该代码段位于Xamarin Forms共享项目的App.xaml.cs文件中。

    public partial class App : Application
    {           
        public App()
        {
            InitializeComponent();
            
            Device.SetFlags(new string[] { "CollectionView_Experimental", "SwipeView_Experimental" });
        }
   }

答案 1 :(得分:0)

在MainActivity中删除FastRenderers_Experimental。 或将FastRenderers_Experimental移至最后。

global::Xamarin.Forms.Forms.SetFlags("SwipeView_Experimental");
global::Xamarin.Forms.Forms.SetFlags("CollectionView_Experimental");
global::Xamarin.Forms.Forms.SetFlags("FastRenderers_Experimental");

代替

global::Xamarin.Forms.Forms.SetFlags("FastRenderers_Experimental");
global::Xamarin.Forms.Forms.SetFlags("SwipeView_Experimental");
global::Xamarin.Forms.Forms.SetFlags("CollectionView_Experimental");