Method linked with Subscribe in EventAggregator pattern is never fired

529 views Asked by At

I have two ViewModels called ViewModelOne and ViewModelTwo.

I also have a class called SelectedTileChangedEvent in my Infrastructure Project. Here is the code:

public class SelectedTileChangedEvent : CompositePresentationEvent<int> { }

In ViewModelOne:

_eventAggregator.GetEvent<SelectedTileChangedEvent>().Publish(1);

After calling this Publish method instance of ViewModelOne is destroyed.

In ViewModelTwo's constructor:

_eventAggregator.GetEvent<SelectedTileChangedEvent>().Subscribe(SelectedTileChanged);

private void SelectedTileChanged(int selectedTileId)
{
    //some code
}

But the SelectedTileChanged Method is never fired.

I have tried the following two possible solutions:

  1. I have passed in an extra parameter to Subscribe Method as keepSubscriberReferenceAlive = true. So, my code looks like:

    _eventAggregator.GetEvent<SelectedTileChangedEvent>().Subscribe(SelectedTileChanged, true);
    
  2. I thought that the instance of Publisher is always destroyed before any Subscriber can listen to the Event. So, I Implemented IRegionMemberLifetime on ViewModelOne as follows:

    private bool KeepAlive()
    {
         get { return true; }
    }
    

Both the above mentioned attempts does not give me the required result.

I really need to subscribe to the SelectedTileChangedEvent and thus I would like my SelectedTileChanged Method to be called from ViewModelTwo....

Thanks

0

There are 0 answers