Problem with EventAggregator

Topics: Prism v4 - Silverlight 4
Sep 8, 2011 at 9:58 PM

Dear all,

recently began to study the Prism. There was a problem with EventAggregator. After subscribing to an event (ea.GetEvent<Events>().Subscribe (eh)) in plugin silverlight library, and after an event is published in Shell.xaml (Method Publish), the event is not generated (is not execute, the error also does not occur). I need to organize the relationship between Shell and the plugin Control. Where am I wrong?

Thank you.

Bootstrapper.cs:

public class Bootstrapper : MefBootstrapper
    {
        protected override IModuleCatalog CreateModuleCatalog()
        {
            return Microsoft.Practices.Prism.Modularity.ModuleCatalog.CreateFromXaml(new Uri("/SilverlightApplication1;component/ConfigModules.xaml", UriKind.Relative));
        }

        protected override void ConfigureAggregateCatalog()
        {
            base.ConfigureAggregateCatalog();
            AggregateCatalog.Catalogs.Add(new AssemblyCatalog(this.GetType().Assembly));
        }

        protected override DependencyObject CreateShell()
        {
            return Container.GetExportedValue<Shell>();
        }

        protected override void InitializeShell()
        {
            base.InitializeShell();
            Application.Current.RootVisual = (Shell)this.Shell;
        }
    }

ConfigModules.xaml:

 <Modularity:ModuleCatalog
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:sys="clr-namespace:System;assembly=mscorlib"
    xmlns:Modularity="clr-namespace:Microsoft.Practices.Prism.Modularity;assembly=Microsoft.Practices.Prism">
        <Modularity:ModuleInfo
        Ref="SilverlightApplication2.xap"
        ModuleName="ModuleQuiz"
        ModuleType="SilverlightApplication2.ModuleQuiz, SilverlightApplication2, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" />
    </Modularity:ModuleCatalog>

Container in Shell.xaml:
  <ContentControl x:Name="MainRegion" prism:RegionManager.RegionName="MainRegion" 
                        VerticalContentAlignment="Stretch"
                        HorizontalContentAlignment="Stretch"
                        VerticalAlignment="Stretch" HorizontalAlignment="Stretch">
        </ContentControl>
        <Button Content="Button" Height="23" Name="button1" Width="75" Click="button1_Click" />

Shell.cs:
[Export] public partial class Shell : UserControl { public Shell() { InitializeComponent(); } [Import(AllowRecomposition = false)] public IModuleManager ModuleManager; [Import(AllowRecomposition = false)] public IRegionManager RegionManager; [Import] public IEventAggregator ea { get; set; } private void button1_Click(object sender, RoutedEventArgs e) { ea.GetEvent<Events>().Publish("temp string"); }
}

EventArgs class:

public class Events:CompositePresentationEvent<string>
    {

    }


Plugin side:

Boot plugin:

  [ModuleExport(typeof(ModuleQuiz))]
        public class ModuleQuiz : IModule
        {
            [Import(AllowRecomposition = false)]
            public IRegionManager regionManager;

            public void Initialize()
            {
                this.regionManager.RegisterViewWithRegion("MainRegion", typeof(UC));
            }
        }

Eventargs class on plugin side:

        public class Events : CompositePresentationEvent<string>
        {

        }

Plugin control:

[Export]
    public partial class UC : UserControl
    {
        [ImportingConstructor]
        public UC(IEventAggregator a)
        {
            InitializeComponent();

            this.ea = a;
            ea.GetEvent<Events>().Subscribe(eh);
        }

        public void eh(string str)
        {
        }

        [Import]
        public IEventAggregator ea { get; set; }
    }

* method eh is not executed.

Developer
Sep 9, 2011 at 5:38 PM

Hi,

It is likely that the problem you're experiencing could be related to the fact that you are declaring your Events class twice. The class that inherits from CompositePresentationEvent is responsible for identifying an event (so that when you publish and subcribe events using that class as the type parameter in the GetEvent method, it is clear that you are referring to the same event).

Therefore you could try declaring a unique Events class in an Infrastructure project (which would be referenced in both your shell and your "plugin" modules).

You might find the Prism Training Kit useful, specially the Communication Lab Exercise 2 which shows how to implement Event Aggregator.

Also you will find more information regarding that in the Prism documentation:

I hope you find this useful

Agustin Adami
http://blogs.southworks.net/aadami