我一直在尝试在视图模型中加载资源字典XAML文件。我能够实例化它,并且调用它不会导致立即错误,但是在控制返回到UI之后,会出现错误弹出“错误HRESULT E_FAIL已从调用COM组件返回”。
我正在做以下事情:
ViewModelsResources.xaml
位于MyApp/ViewModels
,其中MyApp
是我的Silverlight应用程序的根文件夹ViewModelsResources.xaml
标记为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;
(与上面的代码示例相同的类)调试显示VMResources
和Template1
被分配了良好的值。我不知道为什么这会抛出关于COM组件的错误,但我已经将它隔离到引用此ResourceDictionary时。如果我取出引用VMResources[x]
的行,则没有错误。任何帮助将不胜感激。