【问题标题】:Locating View for a ViewModel, both in an external assembly在外部程序集中定位 ViewModel 的视图
【发布时间】:2014-01-15 14:00:41
【问题描述】:

我在我的应用程序中使用 MEF 来加载一些简单的插件。每个插件由一个 ViewModel 和一个对应的 View 组成。

我能够成功创建此类插件的 ViewModel 实例,但是 Caliburn.Micro 说它无法找到它的视图。插件中的 ViewModel 被称为 SimpleValueDisplayViewModel 和视图 SimpleValueDisplayView,具有相同的命名空间。

我的引导程序中的相关代码:

public class MefBootstrapper : Bootstrapper<ShellViewModel>
{   
    protected override void Configure()
    {
        string pluginPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Plugins");
        if (!Directory.Exists(pluginPath))
            Directory.CreateDirectory(pluginPath);

        var catalog = new AggregateCatalog(
            AssemblySource.Instance.Select(x => new AssemblyCatalog(x)).OfType<ComposablePartCatalog>()
            .Concat(new ComposablePartCatalog[] { new DirectoryCatalog("Plugins")})
            );

        _container = new CompositionContainer(catalog);
        var batch = new CompositionBatch();

        batch.AddExportedValue<IWindowManager>(new WindowManager());
        batch.AddExportedValue<IEventAggregator>(new EventAggregator());
        batch.AddExportedValue(_container);

        _container.Compose(batch);            
    }
}

我是否需要以某种方式通知 Caliburn.Micro MEF 在“插件”目录中找到的程序集?

编辑:我尝试覆盖 SelectAssemblies 并将“插件”目录中的所有程序集添加到 AssemblySource.Instance。但是,然后我遇到了 MEF 两次查找程序集的问题,这反过来又在我实例化 ViewModel 时产生了问题。

【问题讨论】:

  • 视图是嵌入在插件程序集中的xaml文件?

标签: wpf mvvm mef caliburn.micro


【解决方案1】:

好的,在阅读和理解这一点之后,我得到了它的工作。我从 AggregateCatalog 中删除了 DirectoryCatalog,并在创建 AggregateCatalog 之前将 Plugins 文件夹中的 .dll 添加到 AssemblySource.Instance。

这种方式适用于 Caliburn.Micro 和 MEF。

新的引导程序代码:

protected override void Configure()
{
    string pluginPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Plugins");
    if (!Directory.Exists(pluginPath))
        Directory.CreateDirectory(pluginPath);

    var fi = new DirectoryInfo(pluginPath).GetFiles("*.dll");
    AssemblySource.Instance.AddRange(fi.Select(fileInfo => Assembly.LoadFrom(fileInfo.FullName)));

    var catalog = new AggregateCatalog(
        AssemblySource.Instance.Select(x => new AssemblyCatalog(x)).OfType<ComposablePartCatalog>()
        );

    _container = new CompositionContainer(catalog);

    var batch = new CompositionBatch();
    batch.AddExportedValue<IWindowManager>(new WindowManager());
    batch.AddExportedValue<IEventAggregator>(new EventAggregator());
    batch.AddExportedValue(_container);

    _container.Compose(batch);            
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-02-25
    • 1970-01-01
    • 2018-03-27
    • 2015-06-15
    • 1970-01-01
    • 2015-11-18
    相关资源
    最近更新 更多