How do you debug a XamlParseException?

前端 未结 5 1752
情深已故
情深已故 2020-12-17 16:22

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:

{Syst         


        
相关标签:
5条回答
    1. Set the debugger to break on XamlParseException (Debug -> Exceptions -> Common Language Runtime Exceptins -> System.Windows.Markup -> XamlParseException line -> set tick in Thrown column)
    2. Take a look at the exception in debugger (especially on InnerException and LineNumber properties)
    3. Try to go through the callstack line by line. While selecting a line look at the Locals window. It reveals some variables even in Framework code that is might be helpful.
    4. Try to delete some part of XAML and see if exception happens again. This will help you to locate code that cause problems.
    0 讨论(0)
  • 2020-12-17 16:36

    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>
    
    0 讨论(0)
  • 2020-12-17 16:39

    Check what ResourceDictionary references you got - the propblem is usually that one one of them got an error that needs to be fixed.

    If your exception is cause from App it's probably one of your merged dirs there, otherwise the control in question.

    Also check that you're not missing resource dir usage for styles (helps if you got ReSharper since you'll get a warning in XAML design time).

    enter image description here

    0 讨论(0)
  • 2020-12-17 16:42

    You should see an error in .cs file while trying to run the app. And the corresponding .xaml file for this .cs file with given line number and column is the right one.

    0 讨论(0)
  • 2020-12-17 16:44

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

    0 讨论(0)
提交回复
热议问题