Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to utilise Event aggregator in MEF?

I am running the latest PRISM 4.2. Unfortunately the Event Aggregator tutorial in the documentation is driven via Unity instead of MEF. And I can't get it running under MEF.

App.xaml.cs

 public partial class App : Application
    {
        [Import]
        public IEventAggregator eventAggregator;

        protected override void OnStartup(StartupEventArgs e)
        {
            base.OnStartup(e);

            Bootstrapper bootstrapper = new Bootstrapper();
            bootstrapper.Run();
        }
    }

Bootstrapper.cs

public class Bootstrapper : MefBootstrapper
    {
        protected override DependencyObject CreateShell()
        {
            return new MainWindow();
        }

        protected override void InitializeShell()
        {
            base.InitializeShell();
            App.Current.MainWindow = (Window)Shell;
            App.Current.MainWindow.Show();
        }

        protected override void ConfigureAggregateCatalog()
        {
            base.ConfigureAggregateCatalog();

            AggregateCatalog.Catalogs.Add(new AssemblyCatalog(this.GetType().Assembly));
        }

        protected override IModuleCatalog CreateModuleCatalog()
        {
            ModuleCatalog moduleCatalog = new ModuleCatalog();

            return moduleCatalog;
        }

    }

MainWindow.xaml.cs

public partial class MainWindow : Window, IView
    {
        public MainWindow()
        {
            InitializeComponent();
            DataContext = new MainViewModel();
        }
    }

MainViewModel.cs:

[ModuleExport(typeof(MainViewModel))]
    public class MainViewModel : BindableBase, IServiceCallback
    {
        [Import]
        public IEventAggregator eventAggregator;

        [ImportingConstructor]
        public MainViewModel()
        {            
            eventAggregator.GetEvent<AppExitEvent>().Publish("");

        }
     }

Despite the [import] the event aggregator is always null both in App.xaml.cs and in MainViewModel. Why is that?
The second question is do I have to export my Viewmodels as a module (as I did above) to use an even aggregator inside them?

UPDATE:

Proof that latest version of PRISM doesn't support ComposeExportedValue anymore.

enter image description here

'System.ComponentModel.Composition.Hosting.CompositionContainer' does not contain a definition for 'ComposeExportedValue' and no extension method ...

like image 681
Houman Avatar asked Feb 21 '14 19:02

Houman


2 Answers

The solution to this would be what SchubertJ replied on your same question at CodePlex:

  • How to utilise Event aggregator in MEF?

As a deeper analyisis, the Import attribute on Properties would not be resolved until the constructor finishes. That is why you would need to inject the EventAggregator dependency through the constructor as a parameter if this dependency would be used on the constructor implementation.

Therefore, if you would like to use the EventAggregator dependency on the ViewModel constructor, you should use [ImportConstructor] attribute instead, and ask the container for the EventAggregator instance by retreiving it as a parameter:

public class MainViewModel
{
    private readonly IEventAggregator eventAggregator;

    [ImportingConstructor]
    public MainViewModel(IEventAggregator eventAggregator)
    {
       this.eventAggregator = eventAggregator;

       this.eventAggregator.GetEvent<MyEvent>().Publish("");
    }
}

You may find more related information regarding both import alternatives in the following post:

  • MEF - [Import] vs [ImportingConstructor]

I hope this helped you, Regards.

like image 162
GOstrowsky Avatar answered Sep 27 '22 23:09

GOstrowsky


In your bootstrapper class have this method :

protected override void ConfigureContainer()
{
     base.ConfigureContainer();
     Container.ComposeExportedValue(new EventAggregator());
}

You should look at this article as it is answer both your first and second question in better details. http://www.gonetdotnet.info/posts/wpf-articles/wpf-module-communication

Update:

If you create a class as below it will match your export with your import.

public class EventAggProvider
   {
     [Export(typeof(IEventAggregator))]
     public IEventAggregator eventAggregator { get { return new EventAggregator(); } }
   }
like image 40
TYY Avatar answered Sep 27 '22 22:09

TYY