Issue
I am developing a Xamarin.Android app whose main page is a carousel page with 3 children (3 images actually which exist on the disk).
Here is the XAML code of the page:
<?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>
Below within the code-behind of the main page, I add the 3 images to the carousel:
At the start of my application, I load the images into the carousel page using the following code:
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,
},
},
});
}
}
But the line this.Children.Add(...) sometimes (not always) generates the following exception:
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))'
But I do not understand why, because I tested to add an image which does not exist on disk, and I do not add such exception?
Can someone helps me find out what is going on and why I get sometimes this error? Thank you for any help
EDIT: Aditional information I just found that the given exception was raised with the following one:EXCEPTION: The application's PagerAdapter changed the adapter's contents without calling PagerAdapter#notifyDataSetChanged! , so maybe I have to switch from carouselpage to carouselview.
Solution
I will definitively try to replace the deprecated CarouselPage by the CarouselView, in order to see if the exception 'Value cannot be null. Parameter key' disappears, because it is really weird it is raised sometimes (I 'd say 1% of time) for no reason. I will let you know and post my code afterwards so that people who encounter this exception have a solution.
Thanks
Answered By - J.Doe
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.