RegionManager and MEF

Topics: Prism v4 - WPF 4
Jun 10, 2010 at 7:45 PM

Hi,

I've tried create WPF application using Prism & MEF, but when I create in Shell Window some region I'll get InvalidOperationException.

Shell Window

<Window x:Class="Shell.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
        xmlns:cal="clr-namespace:Microsoft.Practices.Composite.Presentation.Regions;assembly=Microsoft.Practices.Composite.Presentation" 
        Title="Shell" Height="350" Width="525">
    <Grid >
        <ContentControl cal:RegionManager.RegionName="CskModuleRegion"/>
    </Grid>
</Window>

Bootstrapper

public class Bootstrapper : MefBootstrapper
{
    protected override void InitializeShell()
    {
        base.InitializeShell();
        Application.Current.MainWindow = (MainWindow) this.Shell;
        Application.Current.MainWindow.Show();
    }

        /// <summary>
    /// Configures the <see cref="AggregateCatalog"/> used by MEF.
    /// </summary>
    protected override void ConfigureAggregateCatalog()
    {
            base.ConfigureAggregateCatalog();

            DirectoryCatalog catalog = new DirectoryCatalog("Modules");
            this.AggregateCatalog.Catalogs.Add(catalog);
    }

    /// <summary>
    /// Configures the <see cref="IModuleCatalog"/> used by Prism.
    /// </summary>
    protected override void CreateModuleCatalog()
    {
        // Modules are referenced in the configuration
        this.ModuleCatalog = new ConfigurationModuleCatalog();
    }

    protected override DependencyObject CreateShell()
    {
        return new MainWindow();
    }
}

And my module CskModule 

    [Module(ModuleName = "CskModule")]
    public class CskModule : IModule
    {
        private readonly IRegionManager _regionManager;
        private readonly IEventAggregator _eventAggregator;

        public CskModule(IRegionManager regionManager, IEventAggregator eventAggregator)
        {
            this._regionManager = regionManager;
            this._eventAggregator = eventAggregator;
        }

        public void Initialize()
        {
            _regionManager.AddToRegion("CskModuleRegion", new CskDataView());
        }
    }

InvalidOperationException Detail

Exception occured in RegionManager.CreateRegion method, because locator.GetInstance<DelayedRegionCreationBehavior>() call  MefServiceLocatorAdapter.DoGetInstance method which return 2 same objects "{Name = "DelayedRegionCreationBehavior" FullName = "Microsoft.Practices.Composite.Presentation.Regions.Behaviors.DelayedRegionCreationBehavior"}"

Can somebody know how to use MEF and Prism with RegionNames

Thanks, Lukas

Jun 17, 2010 at 3:15 PM
Edited Jun 17, 2010 at 3:15 PM

H lukas please see this thread http://compositewpf.codeplex.com/workitem/6858

Juan Guillermo

Colombia-Medellin