多次注册Castle Windsor

时间:2015-05-28 15:29:43

标签: wpf castle-windsor windsor-3.0

我在WPF应用程序中遇到以下情况:

public class ExpenseView : UserControl, IAccountingView {}

// just a marker, the contract is in IViewWindow
public interface IAccountingView : IViewWindow {}

我需要能够以两种方式注册ExpenseView,如具体类型,ExpenseView和IAccountingView(也可能是另一个界面)。

我正在注册这样的类型:

// I need a collection of IViewWindow to be used
Container.Register( Classes.FromAssemblyInDirectory( myTypes )
    .BasedOn<IViewWindow>()
    .WithServiceFromInterface()
    .LifestyleTransient()
    );

// all other types don't have interfaces, are used as concrete types.
Container.Register( Classes.FromAssemblyInDirectory( myTypes )
    .Where( type => type.IsPublic )
    .WithServiceSelf()
    .LifestyleTransient()
    );

现在,这个工作,ExpenseView和所有其他类型都被实例化,除了我需要使用时

var newInstance = container.Resolve( iView.ViewType );

获取另一个实例,其中iView.ViewType是具体类型ExpenseView(根据示例),我得到此异常:

'Castle.MicroKernel.ComponentNotFoundException'  
No component for supporting the service ExpenseView was found.

任何想法为什么会发生这种情况以及如何使其发挥作用?

1 个答案:

答案 0 :(得分:1)

这里有各种注册选项的很好解释:http://docs.castleproject.org/Windsor.Registering-components-by-conventions.ashx

要使用具体类型注册所有IViewWindow实现器,您可以这样做:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />