[英]MEF + WPF + MVVM: Where should AggregateCatalog reside?
我正在尝试使用MEF,但是我似乎无法确定应该在哪里创建AggregateCatalog
和CompositionContainer
。 我发现了很多示例来说明代码应为:
var moduleCatalog = new AggregateCatalog(
new AssemblyCatalog(System.Reflection.Assembly.GetExecutingAssembly()),
new DirectoryCatalog(moduleDir));
var container = new CompositionContainer(moduleCatalog);
container.ComposeParts(this);
但是现在我只需要知道放在哪里。 应该去:
App.xaml.cs
MainWindow.xaml
编辑:应该已经谷歌了一段时间,我想我找到了我想要的。
阅读此内容后, 在应用程序和库中托管MEF ,我决定可以在App.xaml.cs
中使用MEF
的组成。
public partial class App : Application
{
[Import]
public Window TheMainWindow { get; set; }
protected override void OnStartup(StartupEventArgs e)
{
var moduleCatalog = new AggregateCatalog(
new AssemblyCatalog(typeof(App).Assembly),
new DirectoryCatalog(moduleDir));
var container = new CompositionContainer(moduleCatalog);
container.ComposeParts(this);
base.MainWindow = TheMainWindow;
TheMainWindow.Show();
}
}
注意事项:
App.xaml
删除StartupUri="MainWindow.xaml"
import
其import
,并将base.MainWindow
设置base.MainWindow
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.