views:

459

answers:

3

I'm trying to use a 3rd party component in my Silverlight application and when I try to create an instance of the control, I get a XamlParseException:

{System.Windows.Markup.XamlParseException: Set property 'System.Windows.FrameworkElement.Style' threw an exception. [Line: 0 Position: 0] ---> System.Windows.Markup.XamlParseException: Elements in the same ResourceDictionary cannot have the same x:Key [Line: 1739 Position: 47] at MS.Internal.XcpImports.CreateFromXaml(UnmanagedMemoryStream stream, String sourceAssemblyName, Boolean createNamescope, Boolean requireDefaultNamespace, Boolean allowEventHandlers) at System.Windows.Controls.Control.GetBuiltInStyle(IntPtr nativeTarget, IntPtr& nativeStyle) --- End of inner exception stack trace --- at System.Windows.Application.LoadComponent(Object component, Uri resourceLocator) at SpellCheckerSample.StandardSpellDialog.InitializeComponent() at SpellCheckerSample.StandardSpellDialog..ctor()}

How can I debug this? How do I know what file line 1739, Position 47 is in?

+2  A: 

Could be a bit of a bugger to find. Basically try to gather as many details as possible from the debugger.

  1. Set the debugger to break on XamlParseException.
  2. Have a look at the callstack. It could be possible that the offending control's constructor is on the callstack.
  3. When paused go to the Locals debug window to see if any parameters to the function reveal more about which component this is.
  4. If not double-click the next stack entry down and go to step 3.
  5. Repeat steps 3 and 4.

After I wrote this I realised that the control's constructor is indeed on the callstack and it is SpellCheckerSample. Very likely it is .XAML page for that control. If you can get access to the source, the file name is most likely something like SpellCheckerSample.xaml.

The error itself is pretty straight forward, looks like there multiple things defined with the same key in the same ResourceDictionary. The below code will cause this to happen:

<Window.Resources>
  <myConverters:BananaToCarrotConverter x:Key="StupidestConverterEver" />
  <myConverters:BananaToAppleConverter x:Key="StupidestConverterEver" />
<Window.Resources>
Igor Zevaka
Yeah, I agree. Would be nice if you could see what key it was that was causing the problem. I appreciate your help, but it hasn't led me anywhere useful yet :(
Craig Shearer
Do you have the source for this library? If you have the right file, Line: 1739 Position: 47 will get you to the resource that is causing the issue.
Igor Zevaka
Actually it's the ComponentOne Silverlight SpellChecker component. I've just hacked my app to pieces - removing all the resources in my App.xaml file, and still it fails. Yet, I have a sample app from ComponentOne that works fine. Very weird. However, I'll keep hacking away until I find it!
Craig Shearer
A: 

Turns out my specific problem was that the ComponentOne component only works under Silverlight 4. Once I changed to target SL4 it all worked.

Craig Shearer
What a strange error to report though.
Igor Zevaka
Yes, and a big waste of time! Thanks for your suggestions.
Craig Shearer
A: 

I’ve got the same problem. Really “nice” of MS to include Line number but not the file name. In an application that loads dozens if not hundreds of styles and templates from multiple resource files – how are you supposed to know which one is causing the problem?

    System.Windows.Markup.XamlParseException occurred
  Message=Set property 'System.Windows.FrameworkElement.Style' threw an exception. [Line: 17 Position: 63]
  LineNumber=17
  LinePosition=63
  StackTrace:
       at System.Windows.Application.LoadComponent(Object component, Uri resourceLocator)
       at HB.View.Framework.Page..ctor()
  InnerException: System.Windows.Markup.XamlParseException
       Message=Items added to a dictionary must have a key. [Line: 10 Position: 36]
       LineNumber=10
       LinePosition=36
       StackTrace:
            at MS.Internal.XcpImports.CreateFromXaml(UnmanagedMemoryStream stream, String sourceAssemblyName, Boolean createNamescope, Boolean requireDefaultNamespace, Boolean allowEventHandlers)
            at System.Windows.Controls.Control.GetBuiltInStyle(IntPtr nativeTarget, IntPtr& nativeStyle)
       InnerException: 
govis