如何让依赖注入在 Prism for Xamarin 上工作?
How do I get dependency injection working on Prism for Xamarin?
我有一个应用程序正在移植到 Prism(来自 XamVvm)。我正在使用 DryIoC。
我的界面是这样的
[assembly: Dependency(typeof(IJoinCongregation))]
namespace MapManPrism.Services
{
public interface IJoinCongregation
{
Task<bool> JoinCongregation(int Identifier, int PIN);
Task<bool> CheckForMapUpdates();
Task<Models.API.Publisher> PostPublisher(string name, string phone, int congregationid);
}
实现这个接口的class长这样
public class JoinCongregation : IJoinCongregation
{
private readonly string JoinCongregationAction = $"{EnvironmentConfiguration.WebEndPoint}Congregations/AuthoriseCongregation";
private readonly string RegisterPublisherAction = $"{EnvironmentConfiguration.WebEndPoint}Publisher/EnrolPublisher";
private readonly IFileStorage _fileStorage;
private readonly IDatabaseService _database;
public JoinCongregation(IFileStorage fileStorage, IDatabaseService database)
{
//var container = new Container();
//container.resolve
_fileStorage = fileStorage;
_database = database;
}
但是,当我的构造函数代码在我的页面视图模型(称为 WelcomeWizardPageViewModel)中运行时,它无法解析 IJoinCongregation 的具体实现。这使得 DryIoC 抛出超时错误。
构造函数代码如下所示...
public WelcomeWizardPageViewModel(IJoinCongregation congregation)
{
//var container = new Container();
//new DatabaseService(); // runs constructor code which we need
_congregation = congregation;
...
我做错了什么?这些是我的猜测。
- 我注册服务有误
- 我以错误的方式将这些服务拉入其他服务
- 通过在我的服务中使用 [assembly: Dependency(typeof(IJoinCongregation))],我以某种方式混合了 Xamarin 的 DI 和 DryIoC 的 DI(我尝试将其删除但没有效果)
如有任何帮助, 将不胜感激。谢谢。
编辑:这些是服务首先注册的方式:
protected override void RegisterTypes(IContainerRegistry containerRegistry)
{
containerRegistry.RegisterForNavigation<NavigationPage>();
containerRegistry.RegisterForNavigation<MainPage>();
containerRegistry.RegisterForNavigation<WelcomeWizardPage>();
containerRegistry.RegisterForNavigation<SettingsPage>();
//containerRegistry.RegisterForNavigation<WelcomeWizard>();
//containerRegistry.register
//containerRegistry.RegisterType<IFileStorage, FileStorage>();
containerRegistry.Register<ICartService, CartService>();
containerRegistry.Register<IDatabaseService, DatabaseService>();
containerRegistry.Register<IJoinCongregation, JoinCongregation>();
containerRegistry.Register<IMapService, MapService>();
}
最后是我忘记注册依赖服务了。我就是这样修复的。
找出页面视图模型有哪些依赖项,然后在页面视图模型中将它们设置为 null 以验证页面视图模型是否正常工作,以及自动视图模型解析。这是有效的,所以它必须是别的东西。
从页面视图模型的构造函数中取出这两个服务。这也有效。
将一项服务添加回页面视图模型。这仍然有效。
向页面视图模型添加第二个服务,轮子脱落并因 DryIoC 错误而损坏。
导航到导致其中断的服务并检查其依赖项。
其中一个其 依赖项未在 DryIoC 中注册。添加它,事情就变得生动了叹息。
我有一个应用程序正在移植到 Prism(来自 XamVvm)。我正在使用 DryIoC。
我的界面是这样的
[assembly: Dependency(typeof(IJoinCongregation))]
namespace MapManPrism.Services
{
public interface IJoinCongregation
{
Task<bool> JoinCongregation(int Identifier, int PIN);
Task<bool> CheckForMapUpdates();
Task<Models.API.Publisher> PostPublisher(string name, string phone, int congregationid);
}
实现这个接口的class长这样
public class JoinCongregation : IJoinCongregation
{
private readonly string JoinCongregationAction = $"{EnvironmentConfiguration.WebEndPoint}Congregations/AuthoriseCongregation";
private readonly string RegisterPublisherAction = $"{EnvironmentConfiguration.WebEndPoint}Publisher/EnrolPublisher";
private readonly IFileStorage _fileStorage;
private readonly IDatabaseService _database;
public JoinCongregation(IFileStorage fileStorage, IDatabaseService database)
{
//var container = new Container();
//container.resolve
_fileStorage = fileStorage;
_database = database;
}
但是,当我的构造函数代码在我的页面视图模型(称为 WelcomeWizardPageViewModel)中运行时,它无法解析 IJoinCongregation 的具体实现。这使得 DryIoC 抛出超时错误。
构造函数代码如下所示...
public WelcomeWizardPageViewModel(IJoinCongregation congregation)
{
//var container = new Container();
//new DatabaseService(); // runs constructor code which we need
_congregation = congregation;
...
我做错了什么?这些是我的猜测。
- 我注册服务有误
- 我以错误的方式将这些服务拉入其他服务
- 通过在我的服务中使用 [assembly: Dependency(typeof(IJoinCongregation))],我以某种方式混合了 Xamarin 的 DI 和 DryIoC 的 DI(我尝试将其删除但没有效果)
如有任何帮助, 将不胜感激。谢谢。
编辑:这些是服务首先注册的方式:
protected override void RegisterTypes(IContainerRegistry containerRegistry)
{
containerRegistry.RegisterForNavigation<NavigationPage>();
containerRegistry.RegisterForNavigation<MainPage>();
containerRegistry.RegisterForNavigation<WelcomeWizardPage>();
containerRegistry.RegisterForNavigation<SettingsPage>();
//containerRegistry.RegisterForNavigation<WelcomeWizard>();
//containerRegistry.register
//containerRegistry.RegisterType<IFileStorage, FileStorage>();
containerRegistry.Register<ICartService, CartService>();
containerRegistry.Register<IDatabaseService, DatabaseService>();
containerRegistry.Register<IJoinCongregation, JoinCongregation>();
containerRegistry.Register<IMapService, MapService>();
}
最后是我忘记注册依赖服务了。我就是这样修复的。
找出页面视图模型有哪些依赖项,然后在页面视图模型中将它们设置为 null 以验证页面视图模型是否正常工作,以及自动视图模型解析。这是有效的,所以它必须是别的东西。
从页面视图模型的构造函数中取出这两个服务。这也有效。
将一项服务添加回页面视图模型。这仍然有效。
向页面视图模型添加第二个服务,轮子脱落并因 DryIoC 错误而损坏。
导航到导致其中断的服务并检查其依赖项。
其中一个其 依赖项未在 DryIoC 中注册。添加它,事情就变得生动了叹息。