wpf - Prism WPF中按名称解析接口的依赖关系
问题描述
我有一个要求,我需要在应用程序的不同页面之间共享 3 个 ZmqService 实例。为此,我注册了 IZmqService 接口,如下所示: -
container.RegisterInstance<IZmqService>("liveViewCaptureService", new ZmqService());
container.RegisterInstance<IZmqService>("faceMatchCaptureService", new ZmqService());
container.RegisterInstance<IZmqService>("captureServiceTrigger", new ZmqService());
现在,对于我的视图模型,我已经解决了这样的问题:-
IZmqService liveViewCaptureService = container.Resolve<IZmqService>("liveViewCaptureService");
IZmqService faceMatchCaptureService = container.Resolve<IZmqService>("faceMatchCaptureService");
IZmqService captureServiceTrigger = container.Resolve<IZmqService>("captureServiceTrigger");
container.RegisterType<IManualVerificationPageViewModel, ManualVerificationPageViewModel>(
new PerResolveLifetimeManager(),
new InjectionConstructor(
new InjectionParameter<IRegionManager>(container.Resolve<IRegionManager>()),
new InjectionParameter<IEventAggregator>(container.Resolve<IEventAggregator>()),
new InjectionParameter<IDialogService>(container.Resolve<IDialogService>()),
new InjectionParameter<ICaptureService>(container.Resolve<ICaptureService>()),
new InjectionParameter<IAppConfiguration>(container.Resolve<IAppConfiguration>()),
new InjectionParameter<IMessageQueueService>(container.Resolve<IMessageQueueService>()),
new InjectionParameter(liveViewCaptureService),
new InjectionParameter(faceMatchCaptureService)
)
);
在我的 XAML 中,我设置了prism:ViewModelLocator.AutoWireViewModel="True"
我的视图模型的构造函数是:-
public ManualVerificationPageViewModel(
IRegionManager regionManager,
IEventAggregator eventAggregator,
IDialogService dialogService,
ICaptureService captureService,
IAppConfiguration appConfiguration,
IMessageQueueService messageQueueService,
IZmqService liveViewCaptureService,
IZmqService faceMatchCaptureService
)
{
_regionManager = regionManager;
_eventAggregator = eventAggregator;
_dialogService = dialogService;
_captureService = captureService;
_appConfiguration = appConfiguration;
_messageQueueService = messageQueueService;
_liveViewCaptureService = liveViewCaptureService;
_faceMatchCaptureService = faceMatchCaptureService;
SelectIdentityCommand = new DelegateCommand(DoSelectIdentity);
VerifyCommand = new DelegateCommand(DoVerify, CanVerify)
.ObservesProperty(() => SelectedIdentityType)
.ObservesProperty(() => IdentityNumber);
CancelCommand = new DelegateCommand(DoCancel);
RecaptureCommand = new DelegateCommand(DoRecapture);
}
如果我尝试在我的 App.xaml.cs 中解析 ViewModel,如下所示,
IManualVerificationPageViewModel vm = container.Resolve<IManualVerificationPageViewModel>();
我得到了一个有效的 ViewModel。但是当 AutoWire 在我的页面中发生时,它会给出以下错误:-
ResolutionFailedException: Resolution of the dependency failed, type = 'NEC.AAI.EGate.Checkin.ViewModels.ManualVerificationPageViewModel', name = '(none)'.
Exception occurred while: while resolving.
Exception is: InvalidOperationException - The current type, NEC.AAI.EGate.Services.IZmqService, is an interface and cannot be constructed. Are you missing a type mapping?
-----------------------------------------------
At the time of the exception, the container was:
Resolving NEC.AAI.EGate.Checkin.ViewModels.ManualVerificationPageViewModel,(none)
Resolving parameter 'liveViewCaptureService' of constructor NEC.AAI.EGate.Checkin.ViewModels.ManualVerificationPageViewModel(Prism.Regions.IRegionManager regionManager, Prism.Events.IEventAggregator eventAggregator, NEC.AAI.EGate.UI.Common.Services.IDialogService dialogService, NEC.AAI.EGate.Services.ICaptureService captureService, NEC.AAI.EGate.Services.IAppConfiguration appConfiguration, NEC.AAI.EGate.Services.MessageQueue.IMessageQueueService messageQueueService, NEC.AAI.EGate.Services.IZmqService liveViewCaptureService, NEC.AAI.EGate.Services.IZmqService faceMatchCaptureService)
Resolving NEC.AAI.EGate.Services.IZmqService,(none)
如果我正在注册这样的 IZmqService 类型,AutoWire 可以正常工作:-
IZmqService liveViewCaptureService = container.Resolve<IZmqService>("liveViewCaptureService");
IZmqService faceMatchCaptureService = container.Resolve<IZmqService>("faceMatchCaptureService");
IZmqService captureServiceTrigger = container.Resolve<IZmqService>("captureServiceTrigger");
container.RegisterType<IZmqService, ZmqService>(new InjectionConstructor());
但是这会为每个构造函数参数创建一个新的 IZmqService 实例,而不是使用注册的类型。
我在这里做错了什么?
解决方案
好的。我得到了决议。
我正在做的是将我的界面注册到视图模型,并且 AutoWireViewModel 和界面之间没有链接。
我所要做的就是将其更改为:-
container.RegisterType<ManualVerificationPageViewModel>(
new PerResolveLifetimeManager(),
new InjectionConstructor(
new InjectionParameter<IRegionManager>(container.Resolve<IRegionManager>()),
new InjectionParameter<IEventAggregator>(container.Resolve<IEventAggregator>()),
new InjectionParameter<IDialogService>(container.Resolve<IDialogService>()),
new InjectionParameter<ICaptureService>(container.Resolve<ICaptureService>()),
new InjectionParameter<IAppConfiguration>(container.Resolve<IAppConfiguration>()),
new InjectionParameter<IMessageQueueService>(container.Resolve<IMessageQueueService>()),
new InjectionParameter(container.Resolve<IZmqService>("liveViewCaptureService")),
new InjectionParameter(container.Resolve<IZmqService>("faceMatchCaptureService"))
)
);
它工作得很好。我仍在寻找如何将界面、视图和视图模型链接在一起。一旦我发现它会发布。
推荐阅读
- node.js - For 循环在 fs.readfile 完成之前迭代
- javascript - 在数组中排序数组时遇到问题:这里的东西正在创建一个未定义的数组元素
- typescript - TypeScript 方法覆盖规则
- python - 统计 Heroku-Streamlit Web 应用的请求数
- javascript - 运行 Javascript 以从 JSX 中根据 switch 语句加载某个组件
- python - PicklingError:无法序列化对象:将汇率 API 调用到我的 pyspark 数据帧中时
- android - ReleaseTexImage 在 Android 9 中崩溃
- java - Android handler.postDelayed() 每次迭代都会加快速度
- java - 当我从 VSCode 创建新的 Java 项目时未创建包
- reactjs - NextJS redux-saga 状态未更新