提问者:小点点

Prism发布的事件未被订阅


我在WPF中编写代码,其中有两个视图模型。在一个视图模型中,我正在关闭一个prism弹出窗口,在关闭时,我将调用我的发布方法,如:

private void OnCancelInteraction()
{
   _eventAggregator.GetEvent<PubSubEvent>().Publish();
   this.FinishInteraction();
}

而我的subscribe事件如下所示:

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模式?如果有其他方法,请建议。


共1个答案

匿名用户

确保将_EventAggregator对象配置为Singleton,并在两个ViewModels中使用此单个实例。您的问题可能与用于发布和订阅事件的不同EventAggregator对象有关。