基本上,情况如下:
我有以下布局:
FooAssembly.dll
: FooUserControl
和 FooViewModel : NotificationObject
BarAssembly.dll
: BarUserControl
和 BarViewModel : NotificationObject
两者都是使用 Unity 或 MEF 导入的。我创建了 FooViewModel 和 BarViewModel 的实例并将它们添加到
ObservableCollection<NotificationObject>
它链接到我的主应用程序中的 TabControl。现在,我得到了它们的完全限定名称,而不是适当的 View 。解决方案...使用 DataTemplate 对吗?问题是我不知道在我的应用程序中提前期待 FooUserControl/FooViewModel,即使我做了它们的类型也是未知的,这意味着我无法编译该应用程序。那么,当使用 Unity 或 MEF 导入 View / View 模型时,如何注入(inject)正确的 View ?
最佳答案
我创建了一个接口(interface)调用 IAppResources
使用名为 AddResourceDictionary(ResourceDictionary rd)
的方法.该接口(interface)在 Modules 中用于加载 ResourceDictionaries,并在应用程序中实现以获取资源字典并将它们添加到应用程序的 MergedDictionaries
可能有更好的方法,但这对我有用:)
通用接口(interface)
public interface IAppResources
{
void AddResourceDictionary(ResourceDictionary resourceDictionary);
}
模块使用接口(interface)加载资源
[ImportingConstructor]
public MyModule(IAppResources appResources)
{
appResources.AddResourceDictionary(new MyModuleResources());
}
应用程序实现接口(interface)加载资源
[Export(typeof(IAppResources ))]
public class AppResources: IAppResources
{
#region IAppResources Members
void IAppResources.AddResourceDictionary(ResourceDictionary resource)
{
App.Current.MainWindow.Resources.MergedDictionaries.Add(resource);
}
#endregion
}
关于WPF Prism - 如何能够从外部程序集将正确的 View 加载到未知的 View 模型?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6512969/