在Dryioc中注册和注销服务

时间:2018-12-19 12:09:33

标签: xamarin.forms prism dryioc

我正在使用Prism.Forms在Xamarin.Forms中创建应用程序。使用Unity IOC效果很好。因为Unity缺少某些功能,所以我想转到Dryioc。

因为应用程序的组件(例如页面)在运行时发生更改,所以我需要动态注册和注销此组件。在Dryioc中如何做?似乎可以使用Container.OpenScope(),但是这种方法创建了Prism无法使用的新容器。 Prism在整个应用程序生命周期内创建并使用单个容器。我说的对吗?

那么还有其他解决方法吗?

1 个答案:

答案 0 :(得分:1)

  

似乎可以使用Container.OpenScope()

不,DryIoc不会在范围之间分隔注册,它们始终转到根容器

您可以通过传递IfAlreadyRegistered.Replace参数来重新注册新服务。为此,您还需要同时修改旧注册和新注册以使用Setup.With(asResolutionCall: true)。这是reason why

初始设置:

container.Register<MyServiceUser>();
container.Register<IService, MyService>(
    setup: Setup.With(asResolutionCall: true));

替换IService

container.Register<IService, MyReplacementService>(
    ifAlreadyRegistered: IfAlreadyRegistered.Replace,
    setup: Setup.With(asResolutionCall: true));
相关问题