Naveed Zahoor
Naveed Zahoor

Reputation: 13

Prism - Published Event not being subscribed

I am writing my code in WPF where I have two view models. In one view model, I am closing a prism pop up window and on close I am calling my publishing method like:

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

while my subscribe event goes like this:

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);
 }

Here is how both of these classes are receiving event aggregator:

UnityContainer container = new UnityContainer();
var aggregator = container.Resolve<EventAggregator>();
this.DataContext = new ResidentFormDialogViewModel(residentService, userService, unitService, aggregator);

here is my second class:

UnityContainer container = new UnityContainer();
var aggregator = container.Resolve<EventAggregator>();
DataContext = new ResidentListViewModel(residentService, userService, aggregator);

Every time I cancel my interaction, the publishing gets published but ResidentListViewModel is not being called as for subscribing.

ResidentListViewModel is being called before the other class which has subscribe method. Could it be a problem? All I want is to Initialize my collection view whenever the interaction gets finished without breaking the MVVM Pattern? If there is any other method please do suggest.

Upvotes: 1

Views: 1788

Answers (1)

Rohit Garg
Rohit Garg

Reputation: 493

Make sure to configure _eventAggregator object to be Singleton and use this single instance in both the viewmodels. The problem in your case may be related to different EventAggregator objects being used to Publish and Subscribe the event.

Upvotes: 2

Related Questions