Reputation: 628
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:
ViewModelsResources.xaml
located in MyApp/ViewModels
where MyApp
is the root folder of my Silverlight applicationViewModelsResources.xaml
is marked Build Action: Content
, Copy to Output Directory: Copy always
, Custom Tool: MSBuild:Compile
ResourceDictionary VMResources = new ResourceDictionary()
{
Source = new Uri("/ViewModels/ViewModelsResources.xaml", UriKind.Relative)
};
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.
Upvotes: 1
Views: 1707
Reputation: 628
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.
Upvotes: 1