WPF:如何在Prism + MEF中重新加载AggregateCatalog?

时间:2015-02-18 06:06:21

标签: c# wpf prism mef

如何正确重新加载Prism的AggregateCatalog?当文件发生变化时,我需要Prism重新加载其目录,但它一直给我这个错误:

  

未处理的类型异常   ' System.ComponentModel.Composition.ChangeRejectedException'发生了   在System.ComponentModel.Composition.dll

这是Bootstrapper片段。清除AggregateCatalog时失败。

class Bootstrapper : MefBootstrapper
{
    private readonly FileSystemWatcher _fileWatcher;

    public Bootstrapper()
    {
        _fileWatcher = new FileSystemWatcher(Path.Combine(System.AppDomain.CurrentDomain.BaseDirectory, "Modules"));
        _fileWatcher.Created += fileWatcher_Changed;
        _fileWatcher.Changed += fileWatcher_Changed;
        _fileWatcher.Deleted += fileWatcher_Changed;
        _fileWatcher.Renamed += fileWatcher_Changed;
        _fileWatcher.EnableRaisingEvents = true;
    }

    private void fileWatcher_Changed(object sender, FileSystemEventArgs e)
    {
        this.AggregateCatalog.Catalogs.Clear(); //It fails here!
        ConfigureAggregateCatalog();
        ConfigureContainer();
    }

1 个答案:

答案 0 :(得分:0)

不可能这样做。 当应用程序启动时,模块目录在初始化期间构建,之后不再使用甚至销毁。 因此动态地,无需重新启动应用程序,使用棱镜无法添加模块。