Prismv4 + MEF + Regions (View won't load into region)

Topics: Prism v4 - WPF 4
Feb 9, 2011 at 10:23 PM
Edited Feb 9, 2011 at 10:25 PM

I'm not sure what I'm missing here... just trying to get a simple HelloWorld to work.

 

App.xaml

<Application x:Class="MyProj.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             />

App.xaml.cs

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

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

Bootstrapper.cs

 

public sealed class ClientBootstrapper : MefBootstrapper
{
    protected override void ConfigureAggregateCatalog()
    {
        base.ConfigureAggregateCatalog();

        AggregateCatalog.Catalogs.Add(new AssemblyCatalog(Assembly.GetExecutingAssembly()));
    }

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

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

        Application.Current.MainWindow = (Window)Shell;
        Application.Current.MainWindow.Show();
    }
}

 

Shell.xaml

 

<Window x:Class="MyProj.Shell" Title="First Prism Run"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:cal="http://www.codeplex.com/prism"
        WindowStyle="SingleBorderWindow" ResizeMode="CanResizeWithGrip"
        MinWidth="600" MinHeight="400">
    <Grid Name="LayoutRoot">
        <ItemsControl Name="PrimaryRegion" cal:RegionManager.RegionName="PrimaryRegion" Background="Yellow" />
    </Grid>
</Window>

 

HelloWorldView.xaml

 

<UserControl x:Class="MyProj.HelloWorldView"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             Width="200" Height="200">
    <Grid>
        <TextBlock Text="Hello World" Foreground="Green" HorizontalAlignment="Center" VerticalAlignment="Center" FontFamily="Calibri" FontSize="24" FontWeight="Bold"></TextBlock>
    </Grid>
</UserControl>

 

ModulaA.cs

 

[ModuleExport(typeof(HelloWorldModule), InitializationMode = InitializationMode.OnDemand)]
public class HelloWorldModule : IModule
{
    [Import()]
    public HelloWorldView HelloWorld { get; set; }

    [Import(AllowRecomposition = false)]
    private IRegionManager Manager;

    [ImportingConstructor()]
    public HelloWorldModule(IRegionManager mgr)
    {
        Manager= mgr;
    }

    public void Initialize()
    {
        Manager.AddToRegion("PrimaryRegion", HelloWorld);
        Manager.Regions["PrimaryRegion"].Activate(HelloWorld);
        //Manager.RegisterViewWithRegion("PrimaryRegion", typeof(HelloWorldView));
    }
}

This just doesn't seem to work. I'm not really sure how to get this HelloWorldView to load into the region properly.

 

Feb 10, 2011 at 5:57 AM

Where are you informing the container of the existence of the module?  Are you placing the module in a folder "Modules" or are you using a configuration file?

Feb 10, 2011 at 9:23 AM

Where are you informing the container of the existence of the module?  Are you placing the module in a folder "Modules" or are you using a configuration file?


I wasn't aware that I had to let the container know about the Module if it's in the same assembly.

Feb 10, 2011 at 3:00 PM

Here is the page for that

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

            this.AggregateCatalog.Catalogs.Add(new AssemblyCatalog(typeof(MefBootstrap).Assembly));            
            this.AggregateCatalog.Catalogs.Add(new AssemblyCatalog(typeof(SomeProject.Infrastructure.Controllers.EntityController).Assembly));

            this.AggregateCatalog.Catalogs.Add(new AssemblyCataLog(typeof(HelloWorldModule).Assembly));
            //DirectoryCatalog catalog = new DirectoryCatalog("Modules");
            //this.AggregateCatalog.Catalogs.Add(catalog);
            
        }
This is just an example of how you can load the Modules in a seperate directory or assembly that is referenced by the main project. DirectoryCatalog is part of MEF. You all might want to reference
this page for additional options for loading your modules
Feb 10, 2011 at 3:16 PM
Edited Feb 10, 2011 at 3:30 PM

drop the InitializationMode you are specifying its only to load when you tell it to.

Feb 10, 2011 at 3:19 PM

Also is the HelloWorldView.cs attributed with export?


I'm sorta understanding how to mark the necessary classes and load modules and views and regions, but I'm not 100% there yet. I did manage to load a view into a region though using some additional asset classes I found online. In fact, here's a reference to my other question that goes a bit beyond this one: http://compositewpf.codeplex.com/Thread/View.aspx?ThreadId=245448