轮播页面,儿童.添加生成异常'Value cannot be null. Parameter name: key'



我正在开发一个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,
},
},
});
}
}

但是这条线。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的情况下更改了适配器的内容,所以也许我必须从转盘页面切换到转盘视图。

我将最终尝试用CarouselView替换不推荐使用的CarouselPage,以查看异常"Value"是否不能为null。参数键消失了,因为它真的很奇怪,有时(我会说1%的时间(会无缘无故地被提起。我会让你知道,然后发布我的代码,这样遇到这个异常的人就有了解决方案。

感谢

相关内容

最新更新