Thursday, July 14, 2011

The XAML Message From Hell: "Error HRESULT E_FAIL has been returned from a call to a COM component."

The most irritating thing in XAML-based projects (Silverlight, WPF) is the infamous "Error HRESULT E_FAIL has been returned from a call to a COM component."

Why Microsoft didn't decide to catch it and give a more appropriate exception escapes me.  However, some things I've learned about this:

Your XAML is failing to load!

Now, your XAML may actually be correct, is the funny thing -- but maybe your project is missing the references.  For example, I'm using the Silverlight Toolkit and setting styles at runtime on the fly, allowing for potential theming.  I kept getting this error, even though my other projects which use the same code are building fine -- so why is my latest project failing to build?

I missed a reference that was used in the other project.  When it came to load the XAML style, it was likely getting hung up on the missing reference and saying, "OK, I see you want to use System.Windows.Controls.Layout.Toolkit...  but what in the world is that?"

To rectify this issue, it was necessary to add references all over again.

So, my troubleshooting advice is:

  1. Make sure the XAML is correct.  When writing cross-platform (i.e. Silverlight and WPF), some of the controls/targets behave differently or have different properties.  Visual Studio generally points this out as a build error.
  2. Make sure that you are referencing all dependent assemblies.  Due to Silverlight's dependencies, if I write a library on top of Silverlight Toolkit, which then gets used in an application, that application needs to have the references to Silverlight Toolkit.  
Good luck and when in doubt -- get an extra set of eyes!

1 comment:

  1. Oh yes, this is a nightmarish message, no clues to the real cause whatsoever. I got this when trying to assign the same Image to multiple controls, hoping to improve the performance reusing Image objects. Failed with this very message.