.Net Maui ArgumentException: 'An item with the same key has already been added. Key: Microsoft.Maui.Controls.BindableProperty' - What does that mean?

Fredrik Vestergaard 5 Reputation points
2024-08-07T11:49:35.9666667+00:00

I'm really at a loss here.

I'm working on a .Net Maui Application (.Net 8.0) for Android and iOS for work. When I open a specific page I sometimes (like, maybe 1 out of 6-10 times) get an Error with this message: "System.ArgumentException: 'An item with the same key has already been added. Key: Microsoft.Maui.Controls.BindableProperty'". No further information... The call stack in VS points me to external code and googling keyword from the call stack didn't help me further.

Here's the call stack:

0xFFFFFFFFFFFFFFFF in Android.Runtime.RuntimeNativeMethods.monodroid_debugger_unhandled_exception	C#
0x1A in Android.Runtime.JNINativeWrapper._unhandled_exception at /Users/runner/work/1/s/xamarin-android/src/Mono.Android/Android.Runtime/JNINativeWrapper.g.cs:12,5	C#
0x1D in Android.Runtime.JNINativeWrapper.Wrap_JniMarshal_PP_V at /Users/runner/work/1/s/xamarin-android/src/Mono.Android/Android.Runtime/JNINativeWrapper.g.cs:23,26	C#
0xB in System.ThrowHelper.ThrowAddingDuplicateWithKeyArgumentException<Microsoft.Maui.Controls.BindableProperty>	C#
0x137 in System.Collections.Generic.Dictionary<Microsoft.Maui.Controls.BindableProperty,Microsoft.Maui.Controls.BindableObject.BindablePropertyContext>.TryInsert	C#
0x4 in System.Collections.Generic.Dictionary<Microsoft.Maui.Controls.BindableProperty,Microsoft.Maui.Controls.BindableObject.BindablePropertyContext>.Add	C#
0x49 in Microsoft.Maui.Controls.BindableObject.CreateAndAddContext at /_/src/Controls/src/Core/BindableObject.cs:698,4	C#
0xD in Microsoft.Maui.Controls.BindableObject.GetOrCreateContext at /_/src/Controls/src/Core/BindableObject.cs:706,76	C#
0x102 in Microsoft.Maui.Controls.BindableObject.SetValueCore at /_/src/Controls/src/Core/BindableObject.cs:553,4	C#
0x5B in Microsoft.Maui.Controls.BindableObject.SetValue at /_/src/Controls/src/Core/BindableObject.cs:503,4	C#
0x55 in Microsoft.Maui.Controls.BindableObjectExtensions.RefreshPropertyValue at /_/src/Controls/src/Core/BindableObjectExtensions.cs:26,5	C#
0x26 in Microsoft.Maui.Controls.VisualElement.Microsoft.Maui.Controls.IPropertyPropagationController.PropagatePropertyChanged at /_/src/Controls/src/Core/VisualElement/VisualElement.cs:1662,5	C#
0x29 in Microsoft.Maui.Controls.Element.OnParentSet at /_/src/Controls/src/Core/Element/Element.cs:627,4	C#
0x1 in Microsoft.Maui.Controls.NavigableElement.OnParentSet at /_/src/Controls/src/Core/Shell/NavigableElement.cs:97,4	C#
0x161 in Microsoft.Maui.Controls.Element.SetParent at /_/src/Controls/src/Core/Element/Element.cs:430,4	C#
0x2 in Microsoft.Maui.Controls.Element.OnChildAdded at /_/src/Controls/src/Core/Element/Element.cs:588,4	C#
0x2 in Microsoft.Maui.Controls.VisualElement.OnChildAdded at /_/src/Controls/src/Core/VisualElement/VisualElement.cs:1253,4	C#
0x1E in Microsoft.Maui.Controls.Compatibility.Layout.OnInternalAdded at /_/src/Controls/src/Core/Layout.cs:601,4	C#
0x88 in Microsoft.Maui.Controls.Compatibility.Layout.InternalChildrenOnCollectionChanged at /_/src/Controls/src/Core/Layout.cs:591,6	C#
0x1B in System.Collections.ObjectModel.ObservableCollection<Microsoft.Maui.Controls.Element>.OnCollectionChanged	C#
0x9 in System.Collections.ObjectModel.ObservableCollection<Microsoft.Maui.Controls.Element>.OnCollectionChanged	C#
0x23 in System.Collections.ObjectModel.ObservableCollection<Microsoft.Maui.Controls.Element>.InsertItem	C#
0x22 in System.Collections.ObjectModel.Collection<Microsoft.Maui.Controls.Element>.Add	C#
0x48 in Microsoft.Maui.Controls.TemplateUtilities.OnContentChanged at /_/src/Controls/src/Core/TemplateUtilities.cs:68,6	C#
0x188 in Microsoft.Maui.Controls.BindableObject.SetValueActual at /_/src/Controls/src/Core/BindableObject.cs:644,5	C#
0x168 in Microsoft.Maui.Controls.BindableObject.SetValueCore at /_/src/Controls/src/Core/BindableObject.cs:569,5	C#
0x22F in Microsoft.Maui.Controls.BindingExpression.ApplyCore at /_/src/Controls/src/Core/BindingExpression.cs:163,5	C#
0x4E in Microsoft.Maui.Controls.BindingExpression.Apply at /_/src/Controls/src/Core/BindingExpression.cs:58,5	C#
0x7 in Microsoft.Maui.Controls.BindingExpression.BindingExpressionPart.<PropertyChanged>b__50_0 at /_/src/Controls/src/Core/BindingExpression.cs:742,46	C#
0x6 in Microsoft.Maui.Dispatching.Dispatcher. at /_/src/Core/src/Dispatching/Dispatcher.Android.cs:24,24	C#
0xE in Java.Lang.Thread.RunnableImplementor.Run at /Users/runner/work/1/s/xamarin-android/src/Mono.Android/Java.Lang/Thread.cs:36,6	C#
0x8 in Java.Lang.IRunnableInvoker.n_Run at /Users/runner/work/1/s/xamarin-android/src/Mono.Android/obj/Release/net8.0/android-34/mcw/Java.Lang.IRunnable.cs:84,4	C#
0x8 in Android.Runtime.JNINativeWrapper.Wrap_JniMarshal_PP_V at /Users/runner/work/1/s/xamarin-android/src/Mono.Android/Android.Runtime/JNINativeWrapper.g.cs:22,5	C#

I've tried it both on my physical device and with the Standard Pixel 5 - API 34 Emulator. It happens both in Debug & Release mode.

I've wrapped every bit of code, that I suspect of being the culprit in try/catch statements. None of them seem to catch the exception. I've also tried to see if I can isolate the problem by observing the parallel stacks. Nothing gives me any usable information, where or what triggers the problem.

Does anyone have any idea what I could do or a tip how to proceed?

Now before anyone critizices the lack of further information: I know, I'm being very vague. But that's mainly because VS itself is extremely vague! I don't even know which part of my xaml or my code behind triggers the exception (if it even is my code that is the problem). I can't really post the code for all the custom views/converters/extensions I use on the page in question because it's a really big page and posting everything would totally break the scope and would take forever for me to rewrite it in a "anonymized" way.

I don't expect concrete solutions to my problem (although that would be awesome) but maybe someone can at least tell me how I could debug this more efficiently.

I tried the old clean, delete bin & obj and rebuild. I do it on the regular anyway because I know this is a common fix. Sadly didn't help.

.NET MAUI
.NET MAUI
A Microsoft open-source framework for building native device applications spanning mobile, tablet, and desktop.
3,412 questions
{count} vote

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.