CarouselPage,Children.Add生成异常'值不能为null参数名称:键

问题描述

我正在开发Xamarin.Android应用程序,其主页面是带有3个孩子的轮播页面(实际上在磁盘上存在3个图像)。

这是页面的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="MyApp.StartPage"
          x:Name="CarouselStartPage">
</CarouselPage>

在主页代码的下面,我将3张图片添加到轮播中:

在我的应用程序开始时,我使用以下代码将图像加载到轮播页面中:

string[] imgPaths = GetImages();
for (string imgPath in imgPaths)
{
   // Image creation with a tapgesturerecognizer.
   TapGestureRecognizer tapGestureRecognizer = new TapGestureRecognizer();
   tapGestureRecognizer.SetBinding(TapGestureRecognizer.CommandProperty,"StartCmd");
   
   Image img = new Image()
   {
      Source = imgPath,};
   img.GestureRecognizers.Add(tapGestureRecognizer);

   Device.BeginInvokeOnMainThread()
   {

       this.Children.Add(new ContentPage()
       {
            Content = new StackLayout()
            {
                 Children =
                 {
                   img,},});
   }
}

但是this.Children.Add(...)行有时(并非总是)生成以下异常:

Value cannot be null.
                        // Parameter name: key AT   at System.Collections.Generic.Dictionary`2[TKey,TValue].FindEntry(TKey key)[0x00008] in < 5a13d71824a44a4fb1cc4a6f176c0719 >:0
                        //   at System.Collections.Generic.Dictionary`2[TKey,TValue].ContainsKey(TKey key)[0x00000] in < 5a13d71824a44a4fb1cc4a6f176c0719 >:0
                        //   at Xamarin.Forms.ResourcesExtensions.GetMergedResources(Xamarin.Forms.IElement element)[0x00063] in < 59b03de64f0e485e8c9d8ead0b747e17 >:0
                        //   at Xamarin.Forms.Element.set_Parent(Xamarin.Forms.Element value)[0x0004a] in < 59b03de64f0e485e8c9d8ead0b747e17 >:0
                        //   at Xamarin.Forms.Element.OnChildAdded(Xamarin.Forms.Element child)[0x00000] in < 59b03de64f0e485e8c9d8ead0b747e17 >:0
                        //   at Xamarin.Forms.VisualElement.OnChildAdded(Xamarin.Forms.Element child)[0x00000] in < 59b03de64f0e485e8c9d8ead0b747e17 >:0
                        //   at Xamarin.Forms.MultiPage`1[T].OnChildAdded(Xamarin.Forms.Element child)[0x00000] in < 59b03de64f0e485e8c9d8ead0b747e17 >:0
                        //   at Xamarin.Forms.Page.OnInternalAdded(Xamarin.Forms.VisualElement view)[0x00013] in < 59b03de64f0e485e8c9d8ead0b747e17 >:0
                        //   at Xamarin.Forms.Page.InternalChildrenOnCollectionChanged(System.Object sender,System.Collections.Specialized.NotifyCollectionChangedEventArgs e)[0x0005f] in < 59b03de64f0e485e8c9d8ead0b747e17 >:0
                        //   at(wrapper delegate- invoke) < Module >.invoke_void_object_NotifyCollectionChangedEventArgs(object,System.Collections.Specialized.NotifyCollectionChangedEventArgs)
                        //   at System.Collections.ObjectModel.ObservableCollection`1[T].OnCollectionChanged(System.Collections.Specialized.NotifyCollectionChangedEventArgs e)[0x00018] in < 4335f7a6733349bfa94d12b891eed9c1 >:0
                        //   at System.Collections.ObjectModel.ObservableCollection`1[T].OnCollectionChanged(System.Collections.Specialized.NotifyCollectionChangedAction action,System.Object item,System.Int32 index)[0x00009] in < 4335f7a6733349bfa94d12b891eed9c1 >:0
                        //   at System.Collections.ObjectModel.ObservableCollection`1[T].InsertItem(System.Int32 index,T item)[0x0001a] in < 4335f7a6733349bfa94d12b891eed9c1 >:0
                        //   at System.Collections.ObjectModel.Collection`1[T].Add(T item)[0x00020] in < 5a13d71824a44a4fb1cc4a6f176c0719 >:0
                        //   at Xamarin.Forms.ObservableWrapper`2[TTrack,TRestrict].Add(TRestrict item)[0x0004b] in < 59b03de64f0e485e8c9d8ead0b747e17 >:0
                        //   at BorneLs.Core.Views.StartPage +<> c__DisplayClass11_0.< RefreshWelcomeUi > b__0()[0x00097] in < dd6514dc2c5c4d5da8fd19a9afc9d43a >:0
                        //   at Java.Lang.Thread + RunnableImplementor.Run()[0x00008] in < fcb13297ceea443792cf4dd4e406ba23 >:0
                        //   at Java.Lang.IRunnableInvoker.n_Run(System.IntPtr jnienv,System.IntPtr native__this)[0x00009] in < fcb13297ceea443792cf4dd4e406ba23 >:0
                        //   at(wrapper dynamic - method) System.Object.17(intptr,intptr))'

但是我不明白为什么,因为我测试添加了磁盘上不存在的映像,并且我没有添加此类异常?

有人可以帮助我了解发生了什么,为什么有时会出现此错误? 谢谢您的帮助

编辑:附加信息 我只是发现给定的异常引发了以下异常:例外:应用程序的PagerAdapter更改了适配器的内容,而没有调用PagerAdapter#notifyDataSetChanged! ,所以也许我必须从carouselpage切换到carouselview。

解决方法

我将最终尝试用CarouselView替换不推荐使用的CarouselPage,以查看异常值是否不能为null。参数键”消失了,因为它确实很奇怪,有时无缘无故地将它升高(我说是1%的时间)。 我将让您知道并在之后发布我的代码,以便遇到此异常的人有解决方案。

谢谢

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...