2011-06-28 25 views
1

基本上,这里的情况:WPF Prism - 如何能够从外部程序集加载正确的视图到未知的视图模型?

我有以下布局:

  • FooAssembly.dllFooUserControlFooViewModel : NotificationObject
  • BarAssembly.dllBarUserControlBarViewModel : NotificationObject

两者都使用Unity或MEF进口。我创建了FooViewModel和BarViewModel的实例,并将它们添加到链接到我的主应用程序中的TabControl的ObservableCollection<NotificationObject>

现在,我得到他们完全合格的名称,而不是相应的视图。解决方案...使用DataTemplate的权利?问题是我不知道在我的应用程序中提前期待FooUserControl/FooViewModel,即使我的类型是未知的,这意味着我无法编译该应用程序。那么,如何在使用Unity或MEF导入视图/视图模型时注入正确的视图?

+0

你不应该再是使用的接口,并简单地要求容器,以解决任何/所有类型的接口?模板部分可能会有点棘手...... –

回答

0

我用AddResourceDictionary(ResourceDictionary rd)的方法创建了一个接口调用IAppResources。这个接口在模块中用来加载ResourceDictionaries,并在应用程序中实现以获取资源字典并将它们添加到应用程序的MergedDictionaries

可能有更好的方法,但这对我有用:)

通用接口

public interface IAppResources 
{ 
    void AddResourceDictionary(ResourceDictionary resourceDictionary); 
} 

模块使用接口来加载资源

[ImportingConstructor] 
public MyModule(IAppResources appResources) 
{ 
    appResources.AddResourceDictionary(new MyModuleResources()); 
} 

应用实现了接口加载资源

[Export(typeof(IAppResources))] 
public class AppResources: IAppResources 
{ 
    #region IAppResources Members 

    void IAppResources.AddResourceDictionary(ResourceDictionary resource) 
    { 
     App.Current.MainWindow.Resources.MergedDictionaries.Add(resource); 
    } 

    #endregion 
}