【问题标题】:CarouselPage, Children.Add generates exception 'Value cannot be null. Parameter name: key'CarouselPage, Children.Add 生成异常'值不能为空。参数名称:key'
【发布时间】:2020-09-28 15:02:25
【问题描述】:

我正在开发一个 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
  • 感谢您的评论,我将尝试轮播视图,我刚刚发现给定的异常引发了以下异常:异常:应用程序的 PagerAdapter 更改了适配器的内容,而没有调用 PagerAdapter#notifyDataSetChanged! ,所以也许我必须从轮播页面切换到轮播视图。
  • 我在一个基本的演示中测试了你的代码,它运行良好。 forDevice.BeginInvokeOnMainThread 命令可能有问题。试试下面的代码:public CustomCarouselPage() { InitializeComponent(); string[] imgPaths = GetImages(); foreach (var imgPath in imgPaths) { ... Image img = new Image() { Source = imgPath, }; Device.BeginInvokeOnMainThread(() =&gt; { this.Children.Add(new ContentPage() { ... }); }); } }.
  • 非常感谢@Jarvan Zhang 的想法,但我看不出我写的代码和你的建议有什么区别。您的意思是我应该尝试在 Device.BeginInvokeOnMainThread 中嵌入“for 循环”吗?
  • for (string imgPath in imgPaths){} 代码应为 foreach (string imgPath in imgPaths){}。而且你好像没有在BeginInvokeOnMainThread方法中添加一个动作,比如Device.BeginInvokeOnMainThread(()=&gt;{});。当我测试代码时,出现了编译错误。

标签: xamarin exception null carousel children


【解决方案1】:

我将明确尝试用 CarouselView 替换已弃用的 CarouselPage,以查看异常 'Value cannot be null。参数键'消失了,因为它有时会无缘无故地被提升(我会说 1% 的时间)真的很奇怪。 之后我会通知您并发布我的代码,以便遇到此异常的人有解决方案。

谢谢

【讨论】:

    猜你喜欢
    • 2015-11-05
    • 2019-08-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-02
    • 1970-01-01
    相关资源
    最近更新 更多