Caliburn Micro - View& viewmodel在单独的DLL

时间:2017-01-02 16:29:27

标签: c# wpf caliburn.micro

我已经尝试了一段时间,但我遇到了一些问题。我有一个动态加载一个或多个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);
        }
    }

这样可以查看视图模型,但是我收到错误&#34;无法找到&#39; ExampleView&#39;&#34;当我加载ExampleViewModel。我还必须为基本程序集中的每个视图添加[Export(typeof(view)],因为我已经进行了此更改。看来Caliburn micro已经停止自动初始化视图。

任何人都知道我做错了什么?

1 个答案:

答案 0 :(得分:1)

所以事实证明我没有做错任何事情,一路上我将我的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));
    }

请查看以下链接以获取更多详细信息。

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

相关问题