【问题标题】:How to solve "Exception has been thrown by the target of an invocation"如何解决“调用目标引发异常”
【发布时间】:2020-07-09 13:57:08
【问题描述】:

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

我在 CollectionView 中有一个 SwipeView。 CollectionView 位于 StackLayout 内部,而 StackLayout 位于 Stacklayout 内部的 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=null 07-09 15:45:28.132 D/Mono (28233):程序集参考地址 Xamarin.Android.Support.v7.RecyclerView[0x7a3866a480] -> Xamarin.Android.Support.CustomView [0x7a47e79a80]:2 System.Reflection.TargetInvocationException: '调用目标抛出异常。'

这是调试中断的截图:

我使用 Xamarin.Forms 4.7 和 VisualStudio 2019。

我错过了什么?资源中的东西? SwipeView 不能在 ScrollView 中工作吗?

【问题讨论】:

  • 首先,您需要查看 InnerException 属性以确定根本原因是什么
  • 我该怎么做?
  • 使用调试器检查异常对象
  • 我知道如何调试,但我没有来或得到这个内部异常。后面的代码工作正常,只是可视化不工作
  • 你得到一个应该包含 Exception 对象的异常。该对象有一个 InnerException 属性,该属性将包含根本原因的详细信息

标签: android xamarin xamarin.forms


【解决方案1】:

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

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

【讨论】:

  • 没问题。如果更新到最新版本的 Xamarin.Forms 4.7.0.1179,请小心使用 SwipeView。如果 SwipeView 中的子视图元素之一使用 GestureRecognizer,即 TapGestureRecognizer,则 SwipeView 不会滑动以暴露底层 SwipeItem。 Android 上针对此问题的一项任务目前正在审核中
【解决方案2】:

删除 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");

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-01-22
    • 2015-11-16
    • 2018-11-08
    • 2018-02-14
    • 1970-01-01
    • 2016-03-30
    • 1970-01-01
    相关资源
    最近更新 更多