Random programming things I'd want to remember

Wednesday, November 27, 2013

Error HRESULT E_FAIL has been returned from a call to a COM component error on Windows Phone


  1. In your App.xaml.cs "Application_UnhandledException" method, check out the error contained in the e argument
  2. Mine mentioned the error in MeasureOverride method, so more than likely something was wrong with XAML
  3. My error happened because I 1) misspelled converter key and 2) declared converter as a resource after the declaration for the DataTemplate that used it

Long version:

I am working on a Windows Phone app. Many developers face this: you code, everything works fine. Then you change a couple of things and boom! you get a cryptic error message. In my case, the phone app kept hitting this code without no apparent reason:

        private void Application_UnhandledException(object sender, ApplicationUnhandledExceptionEventArgs e)
            if (System.Diagnostics.Debugger.IsAttached)
                // An unhandled exception has occurred; break into the debugger

I hovered my mouse cursor over the e argument of the method, and I got the following information:

MS.Internal.WrappedException: Error HRESULT E_FAIL has been returned from a call to a COM component. ---> System.Exception: Error HRESULT E_FAIL has been returned from a call to a COM component.
   at MS.Internal.XcpImports.CheckHResult(UInt32 hr)
   at MS.Internal.XcpImports.UIElement_Measure_WithDesiredSize(UIElement element, Size availableSize)
   at System.Windows.UIElement.Measure_WithDesiredSize(Size availableSize)
   at System.Windows.Controls.VirtualizingStackPanel.MeasureChild(UIElement child, Size layoutSlotSize)
   at System.Windows.Controls.VirtualizingStackPanel.MeasureOverride(Size constraint)
   at System.Windows.FrameworkElement.MeasureOverride(IntPtr nativeTarget, Double inWidth, Double inHeight, Double& outWidth, Double& outHeight)
   --- End of inner exception stack trace ---}

After a closer look, I noticed that the code mentions MeasureOverride method. I immediately turned to my last XAML document that I edited. Lo and behold, one of the lines was highlighted blue. I added a Converter to one of the values, and I misspelled the Key parameter inside the element. I fixed the typo and voila! same error. Here is an important detail about my scenario: I have a page with resources. I use a converter within a DataTemplate resource declaration. I also declared my local converter resource after the DataTemplate (that uses it). My error kept occuring (now) because I use a resource before I declared it. I changed my page to the following (just an idea):

  <local:myConverter x:Key="...
  <DataTemplate x:Key="...

And everything started working.

Tuesday, November 26, 2013