Caliburn.Micro无法匹配来自不同程序集的View和ViewModel

时间:2013-07-04 14:50:15

标签: c# .net mvvm caliburn.micro

我刚开始使用Caliburn.Micro。

我正在尝试引导我的简单示例解决方案,将ShellView(usercontrol)放在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 个答案:

答案 0 :(得分:20)

通过覆盖引导程序中的SelectAssemblies,检查是否已为CM选择了程序集。

这里的文档有一个例子:

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

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

编辑:

好的,你不仅需要选择程序集来告诉CM在哪里看 - 在你的情况下,你的VM和你的视图可能位于不同的命名空间,因为你将它们放在不同的库中。您可以在两个库中使用相同的根命名空间,并且标准视图解析应该可以正常工作 - 但是,您需要确保在引导程序中选择了程序集,以告诉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

答案 1 :(得分:7)

感谢这篇文章 http://www.jerriepelser.com/blog/split-views-and-viewmodels-in-caliburn-micro/

编辑:由于您将自己的回复与我的回复整合后,我将接受的回答更改为您的回复。