1

嗨,我尝试将 Windosor Castle 与 Caliburn Micro 一起使用。到目前为止,我只使用 MEF。

我发现了这个 Castle Boostraper:https ://gist.github.com/1127914

我将此调用添加到我的项目并修改了 App.xaml 文件:

<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>

我创建 ShellView (WPF) 和 ShellViewModel:

public interface IShellViewModel
{

}


public class ShellViewModel : Conductor<IScreen>.Collection.OneActive,
    IShellViewModel
{

}

当我运行时,我收到此错误:

{"No component for supporting the service Chroma_Configer.ViewModels.IShellViewModel was found"}

我是温莎城堡的初学者,我知道他的工作是这样的:

        var container = new WindsorContainer();
        container.AddComponent("JsonUtil", typeof(IShellViewModel), typeof(ShellViewModel));

        var shell = container.Resolve<IShellViewModel>();

在 MEF 中,我使用属性[Export][Import]。有人可以帮我解决这个问题吗?

另一个问题是我有一些工具类:

public interface ITooll{}

public class Tool:ITool{}

我想在 ShellViewModel 类中导入它。

我如何使用 CastleBoostraper 来做到这一点?

4

1 回答 1

2

您需要在容器中注册您的视图模型和视图。较旧的 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;
    }
}

您还可以创建将在容器中注册类型的安装程序,这样您的引导程序代码就不会以大量注册代码结束:

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;
    }
}

查看我创建的 Silverlight示例应用程序,了解如何使用 Castle Windsor。

您可以使用构造函数注入或属性注入来获取依赖项的实例:

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;
    }
}
于 2011-10-17T11:30:16.380 回答