我有一个要求,我需要在应用程序的不同页面之间共享3个ZmqService实例。为此,我已经注册了IZmqService接口,如下所示:
container.RegisterInstance<IZmqService>("liveViewCaptureService", new ZmqService());
container.RegisterInstance<IZmqService>("faceMatchCaptureService", new ZmqService());
container.RegisterInstance<IZmqService>("captureServiceTrigger", new ZmqService());
现在,对于我的viewModels,我已经解决了这样的问题: -
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"))
)
);
它工作得很好。我仍然在寻找如何将界面,视图和视图模型链接在一起。一旦我找到它就会发布。