Silverlight 4 XAML Parsing Error

If you are using Silverlight 4, chances are you have encountered the nasty exception while parsing a XAML file, something on the lines of : “Parser internal error: Object writer ‘xClassCanOnlyBeUsedOnLoadComponent'”. It can be quite frustrating to debug this because you can’t step into the Load method and the line number details that the exception provides is basically useless.

Whenever you see the term xClassCanOnlyBeUsedOnLoadComponent, look for x:Class in your XAML file. Check whether the namespace specified is correct and your code behind (xaml.cs) also uses the same namespace. If you are using a XAML file only without a code behind file (xaml.cs), then you do not need the x:Class tag in your XAML file. Remove this and the exception should go away.

Let me know if this solves the problem. That being said, Microsoft should really work on improving its exceptions. The least they could do is provide accurate information.

Advertisements

4 thoughts on “Silverlight 4 XAML Parsing Error

  1. doso says:

    what if I need code file behind xaml file? Removing x:Class would not compile then…
    Grrr…thanks for shedding light

  2. dex3703 says:

    I run into this problem occasionally. It’s user error: I’m not paying attention and try to add a UserControl to a collection of ResourceDictionaries. It’s like this:

    <– this is a ResourceDictionary
    <– this is a UserControl .. .oops!

    • dex3703 says:

      Let’s try that markup again:

      ResourceDictionary.MergedDictionaries
      ResourceDictionary Source=”CheckBox.xaml” <– RD
      ResourceDictionary ource="ValidationBug.xaml" <– UC
      ResourceDictionary.MergedDictionaries

  3. d says:

    Thanks! it helped!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: