I have been trying to load a resource dictionary XAML file in my view model. I am able to instantiate it, and calls to it do not result in an immediate error, but after control returns to the UI, there is an error popup “Error HRESULT E_FAIL has been returned from a call to COM component”.
I am doing the following:
- ResourceDictionary file is
ViewModelsResources.xamllocated inMyApp/ViewModelswhereMyAppis the root folder of my Silverlight application ViewModelsResources.xamlis marked Build Action:Content, Copy to Output Directory:Copy always, Custom Tool:MSBuild:Compile- The ResourceDictionary object is instantiated with (and this code is in a class in the same folder as the resource dictionary file)
ResourceDictionary VMResources = new ResourceDictionary() { Source = new Uri("/ViewModels/ViewModelsResources.xaml", UriKind.Relative) }; - The object is then referenced through index based on x:Key values:
Template1 = VMResources["myTemplate"] as ControlTemplate;(same class as code sample above)
Debugging shows that VMResources and Template1 are being assigned good values. I don’t know why this would be throwing errors about COM components, but I have isolated it to when this ResourceDictionary is referenced. If I take out the lines referencing VMResources[x] there is no error. Any help would be much appreciated.
Turns out this does work as I described, but you can’t have events specified in the templates found in the resource dictionary. I should have realized that was going to cause problems, COM was throwing me off though. To get around the need for event handlers, I am using Behaviors. There’s tons of reading out there on Behaviors. I started here and here.
note: If anyone believes I should delete this question since it actually works as proposed, just comment as so. I figure leaving this might help someone trying to do the same thing as me though.