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

Ser*_*gio 10 .net c# 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;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

这是我在Caliburn.Micro.Test.ViewModel程序集(类库)中的ViewModel:

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

    public class ShellViewModel : IShell
    {
    }
}
Run Code Online (Sandbox Code Playgroud)

你能帮我解决一下我的问题吗?谢谢!:d

Cha*_*leh 20

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

这里的文档有一个例子:

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

protected override IEnumerable<Assembly> SelectAssemblies()
{
    return new[] {
        Assembly.GetExecutingAssembly()
    };
}
Run Code Online (Sandbox Code Playgroud)

编辑:

好吧,你不仅需要选择程序集来告诉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


Ser*_*gio 7

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

编辑:既然你将你的回复与我的回复整合了,我将接受的答案改为你的.

  • 链接已经死了.域名已过期.:( (3认同)