我以前曾使用DryIOC作为我的IOC,但我想尝试使用Splat作为我的IOC,是否可以将这两者结合起来?
我曾尝试创建一个继承IModule的LoginModule,因此在我的LoginModule类中包含以下内容:
public void OnInitialized(IContainerProvider containerProvider)
{
Locator.CurrentMutable.RegisterLazySingleton(() => new ServiceEntityMapper(), typeof(IServiceEntityMapper));
Locator.CurrentMutable.RegisterLazySingleton(() => new LoginAPIService(), typeof(ILoginAPIService));
Locator.CurrentMutable.RegisterLazySingleton(() => new LoginManager(
Locator.Current.GetService<IServiceEntityMapper>(),
Locator.Current.GetService<ILoginAPIService>()), typeof(ILoginManager));
}对于我的视图模型构造器,我有这个:
public LoginViewModel(INavigationService navigationService, ILoginManager loginManager = null) : base(navigationService)
{
LoginManager = loginManager ?? Locator.Current.GetService<ILoginManager>();
}结果,每当我导航到页面时,我都会得到这个异常
{System.TypeLoadException: Could not resolve the signature of a virtual method
at System.Lazy`1[T].CreateValue () [0x00081] in <fe08c003e91342eb83df1ca48302ddbb>:0
at System.Lazy`1[T].LazyInitValue () [0x00080] in <fe08c003e91342eb83df1ca48302ddbb>:0
at System.Lazy`1[T].get_Value () [0x0003a] in <fe08c003e91342eb83df1ca48302ddbb>:0
at Splat.DependencyResolverMixins+<>c__DisplayClass7_0.<RegisterLazySingleton>b__0 () [0x00000] in <89c762f12a12451a8970372dc9921547>:0
at Splat.ModernDependencyResolver.GetService (System.Type serviceType, System.String contract) [0x00032] in <89c762f12a12451a8970372dc9921547>:0
at Splat.DependencyResolverMixins.GetService[T] (Splat.IDependencyResolver resolver, System.String contract)发布于 2019-02-07 01:16:39
据我所知,Splat是一个服务定位器,而不是一个实际的DI容器。也就是说,您当然不限于基本的Prism实现,因为提供这些实现是为了使其易于采用和入门。在您的案例中,我可能会建议您创建自己的IContainerExtension实现并继承PrismApplicationBase。
通过查看Unity或DryIoc的实现,您可以发现在您的应用程序类中并没有太多的额外工作……这里有一个使用Grace DI Container的类似示例。请记住,自上次预览以来,已经添加了几个新的应用程序接口,并提出了一个突破性的更改,使IContainerRegistry具有流畅的应用程序接口。
https://stackoverflow.com/questions/54549095
复制相似问题