【问题标题】:Caliburn Micro - View & viewmodel in separate DLLCaliburn Micro - 在单独的 DLL 中查看和查看模型
【发布时间】:2017-01-02 16:29:26
【问题描述】:

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

这发现 viewmodel 很好,但是当我加载 ExampleViewModel 时出现错误“无法找到 'ExampleView' 的合同”。自从我进行了此更改以来,我还必须为基础程序集中的每个视图添加 [Export(typeof(view)]。似乎 Caliburn micro 已停止自动初始化视图。

谁知道我做错了什么?

【问题讨论】:

    标签: c# wpf caliburn.micro


    【解决方案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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-05-07
      • 2011-01-29
      • 2020-09-30
      • 1970-01-01
      • 2020-08-05
      • 1970-01-01
      相关资源
      最近更新 更多