我正在尝试在WPF应用程序中使用prism。到目前为止,我已经配置了我的容器和View模型定位器,如下所示:
protected override void ConfigureContainer()
{
Container.RegisterType<IEventAggregator, EventAggregator>(new ContainerControlledLifetimeManager());
Container.RegisterType<IUnitOfWork, UnitOfWork>();
Container.RegisterType<IDbFactory, DbFactory>();
Container.RegisterType<IUnitOfWork, UnitOfWork>("UnitOfWork", new ContainerControlledLifetimeManager());
Container.Resolve<IUnitOfWork>("UnitOfWork");
Container.RegisterType<IDbFactory, DbFactory>("DbFactory", new ContainerControlledLifetimeManager());
Container.Resolve<IDbFactory>("DbFactory");
Container.RegisterType<IRegionManager, RegionManager>();
Container.RegisterInstance<IRegionManager>(new RegionManager(), new ContainerControlledLifetimeManager());
#region Register Repositories
Container.RegisterType<IVendorRepository, VendorRepository>();
Container.RegisterType<IUserRepository, UserRepository>();
Container.RegisterType<IReceiveablesRepository, ReceiveablesRepository>();
Container.RegisterType<IProductUnitsRepository, ProductUnitRepository>();
Container.RegisterType<IPaymentRepository, PaymentRepository>();
Container.RegisterType<IItemRepository, ItemRepository>();
Container.RegisterType<IInvoiceMasterRepository, InvoiceMasterRepository>();
Container.RegisterType<IInvoiceDetailRepository, InvoiceDetailRepository>();
Container.RegisterType<IExpenseTypeRepository, ExpenseTypeRepository>();
Container.RegisterType<IExpenseRepository, ExpenseRepository>();
Container.RegisterType<ICustomerRepository, CustomerRepository>();
Container.RegisterType<ICategoryRepository, CategoryRepository>();
Container.RegisterType<IAddressesRepository, AddressesRepository>();
#endregion
#region Register Services
Container.RegisterType<IVendorService, VendorService>();
Container.RegisterType<IUserService, UserService>();
Container.RegisterType<IReceiveablesService, ReceiveablesService>();
Container.RegisterType<IProductUnitService, ProductUnitService>();
Container.RegisterType<IPaymentService, PaymentService>();
Container.RegisterType<IItemService, ItemService>();
Container.RegisterType<InvoiceMasterService, InvoiceMasterService>();
Container.RegisterType<IInvoiceDetailService, InvoiceDetailService>();
Container.RegisterType<IExpenseTypeService, ExpenseTypeService>();
Container.RegisterType<IExpenseService, ExpenseService>();
Container.RegisterType<ICustomerService, CustomerService>();
Container.RegisterType<ICategoryService, CategoryService>();
Container.RegisterType<IAddressesService, AddressesService>();
#endregion
Container.RegisterType<IDbFactory, DbFactory>("DbFactory", new InjectionConstructor());
Container.RegisterType<IUnitOfWork, UnitOfWork>();
Container.RegisterType<IDbFactory, DbFactory>();
//Container.RegisterType<IService, >();
Container.RegisterType<IEventAggregator, EventAggregator>(new PerThreadLifetimeManager());
Container.RegisterTypeForNavigation<Billing>();
Container.RegisterTypeForNavigation<Sales>();
Container.RegisterTypeForNavigation<Management>();
Container.RegisterTypeForNavigation<Home>();
Container.RegisterTypeForNavigation<Orders>();
Container.RegisterTypeForNavigation<Purchases>();
}
protected override void ConfigureViewModelLocator()
{
base.ConfigureViewModelLocator();
Container.RegisterInstance(new DbFactory());
Container.RegisterInstance(new UnitOfWork(Container.Resolve<DbFactory>()));
Container.RegisterType<IEventAggregator, EventAggregator>(new ContainerControlledLifetimeManager());
Container.RegisterType<IRegionManager, RegionManager>();
Container.RegisterInstance(new UserRepository(Container.Resolve<DbFactory>()),new ContainerControlledLifetimeManager());
Container.RegisterInstance(new UserService(Container.Resolve<UnitOfWork>(),Container.Resolve<UserRepository>()));
Container.RegisterInstance(new VendorRepository(Container.Resolve<DbFactory>()),new ContainerControlledLifetimeManager());
Container.RegisterInstance(new VendorService(Container.Resolve<UnitOfWork>(),Container.Resolve<VendorRepository>()));
Container.RegisterInstance(new ReceiveablesRepository(Container.Resolve<DbFactory>()),new ContainerControlledLifetimeManager());
Container.RegisterInstance(new ReceiveablesService(Container.Resolve<UnitOfWork>(),Container.Resolve<ReceiveablesRepository>()));
Container.RegisterInstance(new ProductUnitRepository(Container.Resolve<DbFactory>()),new ContainerControlledLifetimeManager());
Container.RegisterInstance(new ProductUnitService(Container.Resolve<UnitOfWork>(), Container.Resolve<ProductUnitRepository>()));
Container.RegisterInstance(new PaymentRepository(Container.Resolve<DbFactory>()),new ContainerControlledLifetimeManager());
Container.RegisterInstance(new PaymentService(Container.Resolve<UnitOfWork>(), Container.Resolve<PaymentRepository>()));
Container.RegisterInstance(new ItemRepository(Container.Resolve<DbFactory>()),new ContainerControlledLifetimeManager());
Container.RegisterInstance(new ItemService(Container.Resolve<UnitOfWork>(), Container.Resolve<ItemRepository>()));
Container.RegisterInstance(new InvoiceMasterRepository(Container.Resolve<DbFactory>()),new ContainerControlledLifetimeManager());
Container.RegisterInstance(new InvoiceMasterService(Container.Resolve<UnitOfWork>(), Container.Resolve<InvoiceMasterRepository>()));
Container.RegisterInstance(new InvoiceDetailRepository(Container.Resolve<DbFactory>()),new ContainerControlledLifetimeManager());
Container.RegisterInstance(new InvoiceDetailService(Container.Resolve<UnitOfWork>(), Container.Resolve<InvoiceDetailRepository>()));
Container.RegisterInstance(new ExpenseRepository(Container.Resolve<DbFactory>()),new ContainerControlledLifetimeManager());
Container.RegisterInstance(new ExpenseService(Container.Resolve<UnitOfWork>(), Container.Resolve<ExpenseRepository>()));
Container.RegisterInstance(new ExpenseTypeRepository(Container.Resolve<DbFactory>()),new ContainerControlledLifetimeManager());
Container.RegisterInstance(new ExpenseTypeService(Container.Resolve<UnitOfWork>(), Container.Resolve<ExpenseTypeRepository>()));
Container.RegisterInstance(new CustomerRepository(Container.Resolve<DbFactory>()),new ContainerControlledLifetimeManager());
Container.RegisterInstance(new CustomerService(Container.Resolve<UnitOfWork>(), Container.Resolve<CustomerRepository>()));
Container.RegisterInstance(new CategoryRepository(Container.Resolve<DbFactory>()),new ContainerControlledLifetimeManager());
Container.RegisterInstance(new CategoryService(Container.Resolve<UnitOfWork>(), Container.Resolve<CategoryRepository>()));
Container.RegisterInstance(new AddressesRepository(Container.Resolve<DbFactory>()),new ContainerControlledLifetimeManager());
Container.RegisterInstance(new AddressesService(Container.Resolve<UnitOfWork>(), Container.Resolve<AddressesRepository>()));
Container.RegisterType<IRegionManager, RegionManager>();
Container.RegisterInstance<IRegionManager>(new RegionManager(), new ContainerControlledLifetimeManager());
ViewModelLocationProvider.SetDefaultViewModelFactory((o, type) => Container.Resolve(type));
}
在此之后,当我尝试运行我的应用程序时,它在bootstrap.run()上出现故障并且抛出以下异常:
依赖项的解析失败,输入=&#34; Microsoft.Practices.ServiceLocation.IServiceLocator&#34;,name =&#34;(none)&#34;。 在解决时发生异常。
异常是:InvalidOperationException - 当前类型Microsoft.Practices.ServiceLocation.IServiceLocator是一个接口,无法构造。你错过了类型映射吗?
在例外时,容器是:
解决Microsoft.Practices.ServiceLocation.IServiceLocator,(无)
答案 0 :(得分:1)
您遇到错误的原因是您使用的是不兼容的版本。 Prism 6.3是针对Unity 4.0.1和CommonServiceLocator 1.3.0构建的。较新版本的CommonServiceLocator和Unity包含几个重大更改,使其与Prism 6.3不兼容,包括更改命名空间。
请更新到Prism 7预览版以与Unity 5和CommonServiceLocator 2兼容。您可以在Prism 7本身here内获得完整的更改列表。我会说,我们在WPF的最新预览中特别添加了许多重大更改。