EventAggregator and Application.Deactivated event

Topics: Prism v2 - WPF 4
Jul 19, 2011 at 4:12 PM

I'm having an interesing problem with CAL and the event aggregator.  I am attempting to publish an event when the app is deactivated or activated (application.activated and application.deactivated).  Some of my modules have popup boxes that a I want to hide when the application loses focus.

this is a small code sample below

app.xaml.cs

    protected override void OnStartup(StartupEventArgs e)
    {
        eventAggregator.GetEvent<AppDeactivatedEvent>().Subscribe(RunIt);

        Deactivated += (s, a) => eventAggregator.GetEvent<AppDeactivatedEvent>().Publish(EmptyPayload.Empty);
        Deactivated += (s, a) => Console.WriteLine("Deactivated - in app.xaml");
        Activated += (s, a) => eventAggregator.GetEvent<AppActivatedEvent>().Publish(EmptyPayload.Empty);

        base.OnStartup(e);
    }

Module.cs

    ea.GetEvent<AppActivatedEvent>().Subscribe(presenter.AppDeactivated);

presenter

    public void AppDeactivated(EmptyPayload empty)
    {
        Console.WriteLine("App Deactivated - Module");
    }


What happens is that when the app loses focus I get the following in the console
Deactivated - in app.xaml
When the App regains focus I get the below
App Deactivated - Module

Any ideas on why the event aggregator would wait until the app regains focus for the event to fire. 

Thanks

 


 

Developer
Jul 19, 2011 at 6:12 PM

Hi,

You could try changing the subscription in your module to subscribe from a background thread instead of from the UI thread. That might be the reason why this is happening.

If that doesn't work, it would be helpful if you could provide us with a repro sample portraying this behavior, so that we can help you identify the cause of this problem.

I hope you find this helpful.

Guido Leandro Maliandi
http://blogs.southworks.net/gmaliandi

Jul 21, 2011 at 10:13 PM

Turns out I had a typo. 

ea.GetEvent<AppActivatedEvent>().Subscribe(presenter.AppDeactivated);

wrong event, oops, thanks though