我在这里使用 Caliburn.Micro Bootstrap :
https://gist.github.com/1127914
如果我将所有 View 和 View 模型与 Bootstrap 放在同一个项目中,一切都会正常进行。
但我想将 Views 和 ViewModels 文件夹推送到另一个程序集/项目,我这样做了,更改命名空间,更新 Bootstrap 以找到该 View 模型。现在,当我运行时,出现关于
的错误“没有找到支持服务 MVVMBook.ViewModules.ViewModels.MainViewModel 的组件”
在 Bootstrap 的这一部分:
return string.IsNullOrWhiteSpace(key)
? _container.Resolve(service)
: _container.Resolve(key, service);
显然它无法连接 ViewModel,即使将 VM 设置为 Bootstrap 的通用参数也是如此:
public class CastleBootstrapper : Bootstrapper<MainViewModel>
我使用的命名约定是一个名为 Views 的文件夹和一个名为 ViewModels 的文件夹,文件是 MainView.xaml 和 MainViewModel.cs
我可以告诉它在这个程序集中的什么地方看?
还将这部分添加到 Bootstrap 中,因为当 View 和 View 模型位于单独的程序集中时建议这样做但没有解决问题:
// needed if views and viewmodels are in a seperate assembly
protected override IEnumerable<Assembly> SelectAssemblies()
{
return new[]
{
Assembly.GetExecutingAssembly()
};
}
最佳答案
未找到您的 ViewModel,因为它未注册。 Bootstrap 附带的 ApplicationContainer 类有一个 RegisterViewModels 方法,如下所示:
private void RegisterViewModels()
{
Register(AllTypes.FromAssembly(GetType().Assembly)
.Where(x => x.Name.EndsWith("ViewModel"))
.Configure(x => x.LifeStyle.Is(LifestyleType.Transient)));
}
这只会在 ApplicationContainer 所在的程序集中注册 ViewModel。
我想您已将这些类粘贴到您的项目中,以便您可以修改它们。如果是这种情况,您可以修改应用程序容器 RegisterViewModels 或修改 CaSTLeBootstrapper 并覆盖 Configure() 方法,如下所示:
protected override void Configure()
{
_container = new ApplicationContainer();
_container.AddFacility<TypedFactoryFacility>();
_container.Register(AllTypes.FromAssembly(typeof(MainViewModel).Assembly)
.Where(x => x.Name.EndsWith("ViewModel") || x.Name.EndsWith("View"))
.Configure(x => x.LifeStyle.Is(LifestyleType.Transient)));
}
以上将注册所有 View 模型和 View 。要让 Caliburn 正确定位 View ,请更新 SelectAssemblies() 方法:
protected override IEnumerable<Assembly> SelectAssemblies()
{
return new[]
{
Assembly.GetExecutingAssembly(),
typeof(MainViewModel).Assembly
};
}
有关 CaSTLe.Windsor 的更多信息,请点击此处 http://stw.castleproject.org/Windsor.MainPage.ashx
关于c# - 让 Caliburn.Micro 使用 Windsor 在单独的程序集中绑定(bind)我的 View 和 VM,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10340809/