如何使用 Castle Windsor 通过 WPF 应用程序实现正确的 Di

How to implement proper Di with WPF app using Castle Windsor

我想在我的 WPF 应用程序中使用 Castle Windsor,这是一个具有 4 个控件和两个服务的简单程序 运行。问题是我有一个服务,它有一个需要 int 参数的构造函数。 我已经为 CW & 提供了安装程序并将其注入 class 但 CW 没有说无法解析 int 类型的参数。

部分服务构造函数:

public FaceDetectionService(int devinceIndex)
            : base(devinceIndex)
        {

基础服务需要实例化参数。 我该如何处理这种事情?我相信我的理解是无效的。 如果您需要更多代码,请告诉我。

如果参数仅在运行时已知,那么我建议使用类型化工厂:

IFaceDetectionServiceFactory
{
    FaceDetectionService Create(int devinceIndex);
}

kernel.AddFacility<TypedFactoryFacility>();
kernel.Register(Component.For<IFaceDetectionServiceFactory>().AsFactory());
kernel.Register(Component.For<FaceDetectionService>());

然后你可以在运行时注入IFaceDetectionServiceFactory并创建FaceDetectionService

更多信息在这里: https://github.com/castleproject/Windsor/blob/master/docs/typed-factory-facility-interface-based.md