我正在尝试使用Prism autofac注册服务。但是,我似乎无法找到一种方法来使这项工作。我在互联网上找到的所有东西都是旧版本的棱镜,并没有真正告诉我现在如何使用它。
protected override void RegisterTypes(IContainerRegistry containerRegistry)
{
containerRegistry.RegisterForNavigation<NavigationPage>();
containerRegistry.RegisterForNavigation<CashRegister>();
containerRegistry.RegisterForNavigation<HamburgerMenu>("Index");
containerRegistry.RegisterForNavigation<Inventory>();
containerRegistry.RegisterForNavigation<Navigation>();
containerRegistry.RegisterForNavigation<Start>();
containerRegistry.RegisterForNavigation<InventoryItemDetail>();
containerRegistry.RegisterForNavigation<FolderCreate>();
//containerRegistry.Register<SQLiteService>().As<ISQLiteService>();
var builder = new ContainerBuilder();
builder.RegisterType<SQLiteService>().As<ISQLiteService>();
builder.Build();
}
正如您所看到的,我尝试使用注册容器本身,但由于它无法识别.As
方法,因此无法正常工作。尝试使用新的构建器也给了我错误。
public class StartViewModel : BindableBase
{
public StartViewModel(ISQLiteService sqliteService)
{
}
}
这里我尝试使用注册类型。
有人能告诉我我做错了吗?
我发现您可以使用GetBuilder()
方法使用内置构建器,但仍然会出错。
答案 0 :(得分:0)
堆栈跟踪告诉我们,在NullReferenceException
的构造函数中中有SQLiteService
抛出。在没有看到您的代码的情况下,我猜测有一些必需的构造函数参数,但Autofac正在传递null
。
请参阅the Autofac documentation,了解如何将参数传递给已注册的依赖项。假设我们必须传递数据库文件的名称,我们注册SQLiteService
如下
containerRegistry.RegisterType<SQLiteService>()
.As<ISQLiteService>()
.WithParameter("databaseFile", "database.sqlite");