Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Xamarin 4.8 ArgumentException: An item with the same key has already been added. Key: background #107

Closed
mauro-dasilva opened this issue Aug 10, 2020 · 4 comments · Fixed by #108 or #109
Assignees
Labels
🍄 bug Something isn't working
Milestone

Comments

@mauro-dasilva
Copy link

mauro-dasilva commented Aug 10, 2020

Hi,

I recently upgraded to Xamarin Forms 4.8 and now I see that whenever the application loads it throws the following exception:

Unhandled Exception:
System.TypeInitializationException: The type initializer for 'MagicGradients.GradientView' threw an exception. ---> System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.ArgumentException: An item with the same key has already been added. Key: background
  at System.Collections.Generic.Dictionary`2[TKey,TValue].TryInsert (TKey key, TValue value, System.Collections.Generic.InsertionBehavior behavior) [0x0015a] in /Users/builder/jenkins/workspace/archive-mono/2020-02/android/release/external/corefx/src/Common/src/CoreLib/System/Collections/Generic/Dictionary.cs:572 
  at System.Collections.Generic.Dictionary`2[TKey,TValue].Add (TKey key, TValue value) [0x00000] in /Users/builder/jenkins/workspace/archive-mono/2020-02/android/release/external/corefx/src/Common/src/CoreLib/System/Collections/Generic/Dictionary.cs:240 
  at (wrapper managed-to-native) System.Reflection.RuntimeMethodInfo.InternalInvoke(System.Reflection.RuntimeMethodInfo,object,object[],System.Exception&)
  at System.Reflection.RuntimeMethodInfo.Invoke (System.Object obj, System.Reflection.BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) [0x0006a] in /Users/builder/jenkins/workspace/archive-mono/2020-02/android/release/mcs/class/corlib/System.Reflection/RuntimeMethodInfo.cs:395 
   --- End of inner exception stack trace ---
  at System.Reflection.RuntimeMethodInfo.Invoke (System.Object obj, System.Reflection.BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) [0x00081] in /Users/builder/jenkins/workspace/archive-mono/2020-02/android/release/mcs/class/corlib/System.Reflection/RuntimeMethodInfo.cs:409 
  at System.Reflection.MethodBase.Invoke (System.Object obj, System.Object[] parameters) [0x00000] in /Users/builder/jenkins/workspace/archive-mono/2020-02/android/release/external/corefx/src/Common/src/CoreLib/System/Reflection/MethodBase.cs:53 
  at MagicGradients.StyleSheets.RegisterStyle (System.String name, System.Type targetType, System.String bindablePropertyName) [0x000a7] in <b9be2afa08cb4dab98018a1af7e6eeba>:0 
  at MagicGradients.GradientView..cctor () [0x00035] in <b9be2afa08cb4dab98018a1af7e6eeba>:0 
   --- End of inner exception stack trace ---
  at (wrapper dynamic-method) Android.Runtime.DynamicMethodNameCounter.8(intptr,intptr,intptr)
  at (wrapper native-to-managed) Android.Runtime.DynamicMethodNameCounter.8(intptr,intptr,intptr)
[ERROR] FATAL UNHANDLED EXCEPTION: System.TypeInitializationException: The type initializer for 'MagicGradients.GradientView' threw an exception. ---> System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.ArgumentException: An item with the same key has already been added. Key: background
  at System.Collections.Generic.Dictionary`2[TKey,TValue].TryInsert (TKey key, TValue value, System.Collections.Generic.InsertionBehavior behavior) [0x0015a] in /Users/builder/jenkins/workspace/archive-mono/2020-02/android/release/external/corefx/src/Common/src/CoreLib/System/Collections/Generic/Dictionary.cs:572 
  at System.Collections.Generic.Dictionary`2[TKey,TValue].Add (TKey key, TValue value) [0x00000] in /Users/builder/jenkins/workspace/archive-mono/2020-02/android/release/external/corefx/src/Common/src/CoreLib/System/Collections/Generic/Dictionary.cs:240 
  at (wrapper managed-to-native) System.Reflection.RuntimeMethodInfo.InternalInvoke(System.Reflection.RuntimeMethodInfo,object,object[],System.Exception&)
  at System.Reflection.RuntimeMethodInfo.Invoke (System.Object obj, System.Reflection.BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) [0x0006a] in /Users/builder/jenkins/workspace/archive-mono/2020-02/android/release/mcs/class/corlib/System.Reflection/RuntimeMethodInfo.cs:395 
   --- End of inner exception stack trace ---
  at System.Reflection.RuntimeMethodInfo.Invoke (System.Object obj, System.Reflection.BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) [0x00081] in /Users/builder/jenkins/workspace/archive-mono/2020-02/android/release/mcs/class/corlib/System.Reflection/RuntimeMethodInfo.cs:409 
  at System.Reflection.MethodBase.Invoke (System.Object obj, System.Object[] parameters) [0x00000] in /Users/builder/jenkins/workspace/archive-mono/2020-02/android/release/external/corefx/src/Common/src/CoreLib/System/Reflection/MethodBase.cs:53 
  at MagicGradients.StyleSheets.RegisterStyle (System.String name, System.Type targetType, System.String bindablePropertyName) [0x000a7] in <b9be2afa08cb4dab98018a1af7e6eeba>:0 
  at MagicGradients.GradientView..cctor () [0x00035] in <b9be2afa08cb4dab98018a1af7e6eeba>:0 
   --- End of inner exception stack trace ---
  at (wrapper dynamic-method) Android.Runtime.DynamicMethodNameCounter.8(intptr,intptr,intptr)
  at (wrapper native-to-managed) Android.Runtime.DynamicMethodNameCounter.8(intptr,intptr,intptr)

If I migrate back to V4.7 the application works as expected.

I have also attached a sample application, which is using Xamarin Forms 4.8, and shows the exception.

Xamarin Forms 4.8 Sample.zip

@mgierlasinski
Copy link
Owner

Hi @mauro-dasilva, thanks for reporting. This issues is caused by Brushes introduced in Forms 4.8 which are using background property for CSS styling, same as GradientView and we have a conflict. Issue is being investigated, registering CSS attributes outside Forms assembly is a bit messy, they made everything internal for some reason.

@mgierlasinski
Copy link
Owner

@mauro-dasilva this issue has been fixed in latest release of Magic Gradients.

@mauro-dasilva
Copy link
Author

Awesome! Thanks very much for all your hard work.

@darrabam
Copy link

Thank you very much for the fix, much appreciated, I can finally update to 4.8.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🍄 bug Something isn't working
Projects
None yet
3 participants