【发布时间】:2016-12-25 06:18:09
【问题描述】:
我在 WPF 中编写代码,其中有两个视图模型。在一个视图模型中,我正在关闭一个 prism 弹出窗口,并在关闭时调用我的发布方法,例如:
private void OnCancelInteraction()
{
_eventAggregator.GetEvent<PubSubEvent>().Publish();
this.FinishInteraction();
}
而我的订阅事件是这样的:
public ResidentListViewModel(IResidentService residentService, IUserService userService, IEventAggregator eventAggregator)
{
var res = eventAggregator.GetEvent<PubSubEvent>().Subscribe(InitializeCollectionView);
_residentService = residentService;
_userService = userService;
_eventAggregator = eventAggregator;
InitializeCollectionView();
//***The InteractionRequest class
FormDialogOpenRequest = new InteractionRequest<INotification>();
ConfirmationRequest = new InteractionRequest<IConfirmation>();
//*** Commands for each of the buttons
RaiseFormDialogOpenCommand = new DelegateCommand<object>(this.RaiseFormDialogOpen);
aiseConfirmationCommand = new DelegateCommand<object>(this.RaiseConfirmation);
}
以下是这两个类接收事件聚合器的方式:
UnityContainer container = new UnityContainer();
var aggregator = container.Resolve<EventAggregator>();
this.DataContext = new ResidentFormDialogViewModel(residentService, userService, unitService, aggregator);
这是我的第二节课:
UnityContainer container = new UnityContainer();
var aggregator = container.Resolve<EventAggregator>();
DataContext = new ResidentListViewModel(residentService, userService, aggregator);
每次我取消交互时,都会发布发布,但不会调用 ResidentListViewModel 来订阅。
ResidentListViewModel 在其他具有 subscribe 方法的类之前被调用。会不会是个问题?我想要的只是在交互完成时初始化我的集合视图而不破坏 MVVM 模式?如果有其他方法,请提出建议。
【问题讨论】: