Losing Events?

Topics: Prism v4 - WPF 4
May 30, 2011 at 3:58 PM
Edited May 30, 2011 at 3:59 PM

Hi!, 
I have a code that shows a LoginPage ([Login dialog in Prism + WPF])
When my code removes the loginPageView (on LoginPageViewModel) seems that  the eventAggregator loses the original event and other subscribers will not see the event.

This is my LoginViewModel code:

 

public LoginViewModel(IUnityContainer container, IEventAggregator eventAggregator)
{
	// ... some code here ... 
	this._eventAggregator.GetEvent<LoginSuccessEvent>().Subscribe(this.LoginSuccessful);
}

public void LoginSuccessful(object payload)
{
	var loginView = this._container.Resolve<MyDesktopApp.Modules.Security.Views.LoginView>();
	if (loginView != null)
	{
		this._coreServices.RegionManager.Regions[RegionNames.PopupRegion].Remove(loginView);
	}            
}

 

 

...and this is a sample module waiting for a "LoginSuccessEvent":
public ModuleSample(IUnityContainer container, IEventAggregator eventAggregator)
{
	this._eventAggregator = eventAggregator;
	
	//... some code here ...
	
	this._eventAggregator.GetEvent<LoginSuccessEvent>().Subscribe(this.LoadViews);
}

void LoadViews(object payload)
{
	var sampleView = this._container.Resolve<SampleListView>();
	this._regionManager.RegisterViewWithRegion(RegionNames.MainRegion, () => sampleView);
	this._regionManager.Regions[RegionNames.MainRegion].Activate(sampleView);
}
If I publish the event again, the modules work well ... any idea about this behavior?
perhaps my code is wrong?
May 30, 2011 at 7:49 PM

Hi Jose,

Based on my understanding of your scenario, one thing you could try, which might help you solve this issue, would be to subscribe to the event with a keepSubscriberReferenceAlive option set to true to force a strongly-held subscription. By doing this the event instance keeps a strong reference to the subscriber instance, avoiding it to get garbage collected.

You should also make sure that, at the moment the event is published, the module that subscribed to that event had already been loaded. That is to say, this could be cause by a timing issue.

If the aforementioned suggestions don’t help you, you could provide us with a repro sample, so that we can help you further.

You can read more about Event Aggregation concepts here.

Thanks,

Miguel Bronzovic
http://blogs.southworks.net/mbronzovic

 

May 31, 2011 at 8:03 AM

Hi Miguel,

I set the parameter keepsubscriberReferenceAlive in each subscription to the event LoginSuccessEvent and now the code works perfect.
 

I think I need to learn more about the use of eventsI'll re-read the documentation.

Thank you very much for answering my question.

 

Regards,
Jose