1

こんにちは、Caliburn Micro で Windosor Castle を使用してみます。今まで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 I ユーザー属性[エクスポート]および[インポート] で。この問題について誰か助けてもらえますか?

別の質問は、いくつかのツール クラスがあることです。

public interface ITooll{}

public class Tool:ITool{}

これを ShellViewModel クラスにインポートしたいと思います。

CastleBoostraper でどうすればできますか?

4

1 に答える 1

2

ビュー モデルとビューをコンテナーに登録する必要があります。古い W​​indsor バージョンは属性に基づいて機能していましたが、最新バージョンでは流暢な 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;
    }
}

コンテナーに型を登録するインストーラーを作成して、Bootstrapper コードが多くの登録コードで終わらないようにすることもできます。

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 に答える