Caliburn.Micro无法匹配来自不同程序集的视图和视图模型

11

我刚刚开始使用Caliburn.Micro。

我试图引导我的简单示例解决方案,将ShellView(用户控件)放置在Test.App程序集中,而将ShellViewModel放置在Test.ViewModel程序集中。

我得到的是一个窗口,上面显示着以下文本:“找不到 Caliburn.Test.ViewModel.ShellViewModel 的视图”。

但如果我将ViewModel移动到.App程序集中,它就可以完美地工作。

这是Caliburn.Micro.Test程序集(可执行文件)中的Bootstraper:

namespace Caliburn.Micro.Test
{
    public class AppBootstrapper : BootstrapperBase
    {
        SimpleContainer container;

        public AppBootstrapper()
        {
            this.Start();
        }

        protected override void Configure()
        {
            container = new SimpleContainer();

            this.container.Singleton<IWindowManager, WindowManager>();
            this.container.Singleton<IEventAggregator, EventAggregator>();
            this.container.PerRequest<IShell, ShellViewModel>();
        }

        protected override object GetInstance(Type service, string key)
        {
            var instance = this.container.GetInstance(service, key);
            if (instance != null)
                return instance;

            throw new InvalidOperationException("Could not locate any instances.");
        }

        protected override IEnumerable<object> GetAllInstances(Type service)
        {
            return this.container.GetAllInstances(service);
        }

        protected override void BuildUp(object instance)
        {
            this.container.BuildUp(instance);
        }

        protected override void OnStartup(object sender, System.Windows.StartupEventArgs e)
        {
            this.DisplayRootViewFor<IShell>();
        }

        protected override IEnumerable<System.Reflection.Assembly> SelectAssemblies()
        {

            var assemblies = new List<Assembly>()
            {
                Assembly.GetExecutingAssembly(),
                Assembly.Load("Caliburn.Micro.Test.ViewModel"),
            };

            return assemblies;
        }
    }
}

这是我在 Caliburn.Micro.Test.ViewModel 组件(类库)中的 ViewModel:

namespace Caliburn.Micro.Test.ViewModel
{
    public interface IShell
    {
    }

    public class ShellViewModel : IShell
    {
    }
}

请问您能帮我解决问题吗?谢谢!:D


2
你是否重写了 SelectAssemblies 方法?你需要提供包含视图的所有程序集给 CM。 - Charleh
2个回答

20

检查您是否通过在启动引导程序中覆盖SelectAssemblies来选择了CM的组装件。

此处的文档有一个例子:

http://caliburnmicro.codeplex.com/wikipage?title=Customizing%20The%20Bootstrapper

protected override IEnumerable<Assembly> SelectAssemblies()
{
    return new[] {
        Assembly.GetExecutingAssembly()
    };
}

编辑:

好的,你不仅需要选择组件告诉CM去哪里寻找——在你的情况下,由于你将它们放在不同的库中,你的VM和Views可能在不同的命名空间中。你可以在两个库中使用相同的根命名空间,标准的视图解析应该能正常工作——但是你需要确保在引导程序中选择了这个组件,以告诉CM要尝试在哪些组件中解析视图。

如果出于某种原因,你想将你的视图/VM放在不同的命名空间中,那么你需要自定义CM用于解析视图的逻辑。它使用命名约定来根据视图模型(或者反之,如果你使用了视图优先的方法)的完全限定类型名称来定位View

我建议阅读入门文档:

http://caliburnmicro.codeplex.com/wikipage?title=Basic%20Configuration%2c%20Actions%20and%20Conventions&referringTitle=Documentation

然后按照文档进行操作。如果你想直接跳到命名约定部分,请查看以下页面:

http://caliburnmicro.codeplex.com/wikipage?title=View%2fViewModel%20Naming%20Conventions&referringTitle=Documentation

http://caliburnmicro.codeplex.com/wikipage?title=Handling%20Custom%20Conventions&referringTitle=Documentation


“CM”是什么意思? 无论如何,我读到这是为了汇编而做的,它必须搜索Views,但是Views在Bootstrapper的同一程序集中,而ViewModels则在另一个程序集中。 你能展示一个导入的例子吗?谢谢! - Sergio
1
啊,我看错了,所以你在不同的程序集中拥有视图,你需要提供逻辑让视图定位器搜索额外的命名空间,因为它默认不知道要在那里查找。我会更新我的答案。 - Charleh
1
我更新了我的答案- 我建议先阅读一些相关内容,这并不需要花费太多时间去熟悉一些简单的东西,而且大约需要几天的时间才能掌握更复杂的部分。 - Charleh
我已经编辑了主贴并进行了修改,但仍然无法正常工作 :(是的,我阅读了文档,并且到达了引导程序配置章节,但在尝试将其应用于分离的程序集时遇到了困难。 - Sergio
太好了 - 我的问题类似,只不过 Bootstrapper 在一个引用的 DLL 中,所以我需要使用 Assembly.GetEntryAssembly()。 - Joe Mayo
显示剩余3条评论

7

3
链接失效,域名已过期。 :( - Perry Tew
1
我修复了链接,请查看http://www.jerriepelser.com/blog/the-killing-of-a-rockstar/。 - wimh

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接