WPF throws System.Reflection.TargetInvocationException

  .net, .net-framework-version, c++, windows, wpf

I recently changed to a new computer at work and now my development environment has a weird problem. WPF throws an exception while opening certain views. Note that these views/programs work fine with my old computer.


 EXCEPTION: Exception has been thrown by the target of an invocation.
TYPE: System.Reflection.TargetInvocationException 
STACK TRACE:    at System.RuntimeTypeHandle.CreateInstance(RuntimeType type, Boolean publicOnly, Boolean noCheck, Boolean& canBeCached, RuntimeMethodHandleInternal& ctor, Boolean& bNeedSecurityCheck)
   at System.RuntimeType.CreateInstanceSlow(Boolean publicOnly, Boolean skipCheckThis, Boolean fillCache, StackCrawlMark& stackMark)
   at System.RuntimeType.CreateInstanceDefaultCtor(Boolean publicOnly, Boolean skipCheckThis, Boolean fillCache, StackCrawlMark& stackMark)
   at System.Activator.CreateInstance(Type type, Boolean nonPublic)
   at System.Activator.CreateInstance(Type type)
Inner EXCEPTION (1): 'Add value to dictionary of type 'System.Windows.ResourceDictionary' threw an exception.' Line number '1065' and line position '21'.
TYPE: System.Windows.Markup.XamlParseException 
STACK TRACE:    at System.Windows.Markup.WpfXamlLoader.Load(XamlReader xamlReader, IXamlObjectWriterFactory writerFactory, Boolean skipJournaledProperties, Object rootObject, XamlObjectWriterSettings settings, Uri baseUri)
   at System.Windows.Markup.WpfXamlLoader.LoadBaml(XamlReader xamlReader, Boolean skipJournaledProperties, Object rootObject, XamlAccessLevel accessLevel, Uri baseUri)
   at System.Windows.Markup.XamlReader.LoadBaml(Stream stream, ParserContext parserContext, Object parent, Boolean closeStream)
   at System.Windows.Application.LoadComponent(Object component, Uri resourceLocator)
Inner EXCEPTION (2): Item has already been added. Key in dictionary: 'ControlBrush'  Key being added: 'ControlBrush'
TYPE: System.ArgumentException 
STACK TRACE:    at System.Collections.Hashtable.Insert(Object key, Object nvalue, Boolean add)
   at System.Collections.Hashtable.Add(Object key, Object value)
   at System.Windows.ResourceDictionary.AddWithoutLock(Object key, Object value)
   at System.Windows.ResourceDictionary.Add(Object key, Object value)
   at System.Xaml.Schema.XamlTypeInvoker.AddToDictionary(Object instance, Object key, Object item)
   at MS.Internal.Xaml.Runtime.ClrObjectRuntime.AddToDictionary(Object collection, XamlType dictionaryType, Object value, XamlType valueXamlType, Object key)

What I have already checked:

  • My .NET Framework is 4.8 (Same as the old computer)
  • Installed .NET framework 4.8 SDK (Also 4.5.1 because my old computer had it)
  • Installed same VC++ Redistributables as my old computer (2010, 2013 and 2015-2019)
  • I have also checked that my regional settings are same as my old computer (maybe unrelated)
  • Computer has all the updates (Version 20H2)
  • Visual studio has all the same components installed as my old computer which is a standard .NET desktop development package + .NET Core 2.1 development tools

Code itself builds fine but the problems occur when running the programs.
Also now one of our customers stated that they have the same problem (started without a change to the code), which supports my suspicion that this is some kind of Windows/.Net problem unrelated to the code itself.

Source: Windows Questions