Caliburn Micro - 在单独的 DLL 中查看和查看模型

Caliburn Micro - View & viewmodel in separate DLL

我已经尝试了一段时间,但遇到了一些问题。我有一个动态加载 1 个或多个 DLL 的项目,但我无法使视图绑定工作。

我已经重写了 SelectAssemblies 方法:

 protected override IEnumerable<Assembly> SelectAssemblies()
    {
        string[] AppFolders = Directory.GetDirectories(Config.AppsFolder);

        List<Assembly> assemblies = new List<Assembly>();
        assemblies.Add(Assembly.GetExecutingAssembly());

        foreach (string f in AppFolders)
        {
            Assembly ass = Directory.GetFiles(f, "*.dll", SearchOption.AllDirectories).Select(Assembly.LoadFrom).SingleOrDefault();
            if (ass != null)
            {
                assemblies.Add(ass);
            }
        }
        Apps = assemblies;
        return assemblies;
    }

这按预期工作,然后我有一个在单击按钮时运行的方法:

public void OpenApp(string appName)
    {
        //AppName should be the same as the dll.

        string assName = string.Format("TabletApp.{0}", appName);

        Assembly ass = AppBootstrapper.Apps.SingleOrDefault(x => x.GetAssemblyName() == assName);

        if (ass != null)
        {
            dynamic vm = ass.CreateInstance(string.Format("TabletApp.{0}.ViewModels.{0}ViewModel", appName));
            IoC.Get<IWindowManager>().ShowDialog(vm);
        }
    }

这发现视图模型很好,但是当我加载 ExampleViewModel 时出现错误 "unable to find contract for 'ExampleView'"。自从我进行了此更改后,我还必须为基础程序集中的每个视图添加 [Export(typeof(view)]。Caliburn micro 似乎已停止自动初始化视图。

有人知道我做错了什么吗?

事实证明我没有做错任何事,一路上我已经将 caliburn.micro 更新到 3.0.2。事实证明,他们所做的一个小改动变成了一个重大的突破性更新。除了指出需要更改的引导程序中的 GetInstance 之外,我不会在这里完全介绍它。

protected override object GetInstance(Type service, string key)
    {
        // Skip trying to instantiate views since MEF will throw an exception
        if (typeof(UIElement).IsAssignableFrom(service))
            return null;

        var contract = string.IsNullOrEmpty(key) ? AttributedModelServices.GetContractName(service) : key;
        var exports = container.GetExportedValues<object>(contract);

        if (exports.Any())
            return exports.First();

        throw new Exception(string.Format("Could not locate any instances of contract {0}.", contract));
    }

请查看以下内容link 了解更多详细信息。

https://github.com/Caliburn-Micro/Caliburn.Micro/pull/339