Wpf Caliburn Micro和Windsor Castle Boostraper{没有支持服务的组件}
嗨,我试着用Caliburn Micro使用Windosor Castle。直到现在我只使用MEF 我在这座城堡里发现了Boostraper: 我已将此调用添加到我的项目并修改了App.xaml文件:Wpf Caliburn Micro和Windsor Castle Boostraper{没有支持服务的组件},wpf,dependency-injection,castle-windsor,caliburn.micro,bootstrapper,Wpf,Dependency Injection,Castle Windsor,Caliburn.micro,Bootstrapper,嗨,我试着用Caliburn Micro使用Windosor Castle。直到现在我只使用MEF 我在这座城堡里发现了Boostraper: 我已将此调用添加到我的项目并修改了App.xaml文件: <Application x:Class="Chroma_Configer.App" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="ht
<Application x:Class="Chroma_Configer.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:Bootstraper="clr-namespace:Chroma_Configer.Bootstraper">
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary>
<Bootstraper:CastleBootstrapper x:Key="bootstrapper" />
<Style x:Key="MainView_FontBaseStyle" TargetType="{x:Type Control}">
<Setter Property="FontFamily" Value="Arial"/>
</Style>
</ResourceDictionary>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
</Application>
我是温莎城堡的beginer我知道他是这样工作的:
var container = new WindsorContainer();
container.AddComponent("JsonUtil", typeof(IShellViewModel), typeof(ShellViewModel));
var shell = container.Resolve<IShellViewModel>();
我想在ShellViewModel类中导入这个
如何使用CastleBoostraper进行此操作?您需要在容器中注册视图模型和视图。较旧的Windsor版本是基于属性工作的,但在最新版本中,您可以使用fluent API或基于某种约定的大容量寄存器来实现这一点:
public class Bootstrapper : Bootstrapper<IShellViewModel>
{
protected override IServiceLocator CreateContainer()
{
_container = new WindsorContainer();
var adapter = new WindsorAdapter(_container);
_container.Register(Component.For<ITool>().ImplementedBy<Tool>().LifeStyle.Transient);
return adapter;
}
}
var container = new WindsorContainer();
container.AddComponent("JsonUtil", typeof(IShellViewModel), typeof(ShellViewModel));
var shell = container.Resolve<IShellViewModel>();
public interface ITooll{}
public class Tool:ITool{}
public class Bootstrapper : Bootstrapper<IShellViewModel>
{
protected override IServiceLocator CreateContainer()
{
_container = new WindsorContainer();
var adapter = new WindsorAdapter(_container);
_container.Register(Component.For<ITool>().ImplementedBy<Tool>().LifeStyle.Transient);
return adapter;
}
}
public class ShellRegistration : IWindsorInstaller
{
public void Install(IWindsorContainer container, IConfigurationStore store)
{
container.Register(Component.For<ITool>().ImplementedBy<Tool>().LifeStyle.Transient);
//Register other types
}
}
public class Bootstrapper : Bootstrapper<IShellViewModel>
{
protected override IServiceLocator CreateContainer()
{
_container = new WindsorContainer();
var adapter = new WindsorAdapter(_container);
_container.Install(FromAssembly.This());
return adapter;
}
}
public class ShellViewModel
{
public ShellViewModel(IMyDependency dependency)
{
//you'll get an instance of the class implementing IMyDependency
//Logger property will be injected after construction
}
public ILog Logger
{
get; set;
}
}