0

I'm using Unity for my IoC container and in my bootstrapper I have this code:

protected override IModuleCatalog CreateModuleCatalog()
{
  return Microsoft.Practices.Prism.Modularity.ModuleCatalog.CreateFromXaml(
    new Uri("modulecatalog.xaml", UriKind.Relative));
}

and I created an xml file named "modulecatalog.xaml" with this in it:

<?xml version="1.0" encoding="utf-8" ?> 
<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:ModuleCatalog>

And I'm already getting errors in the xaml saying that Microsoft.Practices.Prism.Modularity could not be found and Modularity:ModuleCatalog was not found.

This is extremely frustrating. I have Microsoft.Practices.Prism included in my project. The code in my Bootstrapper compiles so obviously Microsoft.Practices.Prism.Modularity.ModuleCatalog does exist. But even more strangeness, if I change my CreateModuleCatalog function to this:

using Microsoft.Practices.Prism.Modularity;

...

protected override IModuleCatalog CreateModuleCatalog()
{
  return ModuleCatalog.CreateFromXaml(
    new Uri("modulecatalog.xaml", UriKind.Relative));
}

It says that CreateFromXaml doesn't exist on ModuleCatalog. It has no problem finding ModuleCatalog, but that function apparently does not exist if I don't type out the full namespace every time. WTF is going on?

Nick
  • 4,556
  • 3
  • 29
  • 53

2 Answers2

0

Try to explicitly resolve the ModuleCatalogtype like this and it should work

protected override Microsoft.Practices.Prism.Modularity.IModuleCatalog CreateModuleCatalog()
{
   return Microsoft.Practices.Prism.Modularity.ModuleCatalog.CreateFromXaml( new Uri( "Modules.xaml", UriKind.Relative ) );
}

The Errors with not found the Modularity:ModuleCatalog are the same for me but the Applications runs well.

Ah I nearly forgot. Set the modules.xaml file to resource

Mohnkuchenzentrale
  • 5,745
  • 4
  • 30
  • 41
  • You're saying you still get errors in your xaml file asying that Modularity:ModuleCatalog was not found but it still works anyway? – Nick Sep 23 '12 at 21:26
  • Exactly. My Modules.xaml gets parsed correctly bei the ReadFromXaml Method and my modules are being loaded in my app. Just make sure your dll's and namespaces in this File are correct – Mohnkuchenzentrale Sep 24 '12 at 04:41
0

I also struggled with this. I eventually resolved this by the following:

Setup: Within your shell, you should have your regions defined:

<Window x:Class="Prism101.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:prism="http://www.codeplex.com/prism"
        Title="MainWindow" Height="350" Width="525">
    <Grid x:Name="LayoutRoot" Background="White">
        <ContentControl prism:RegionManager.RegionName="MainContent" />
    </Grid>
</Window>

Step 1: Add a reference to your module project / assembly.

Step 2: Declare a module within the Module Catalog: This is will be within your module project.

<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.Composition">

    <Modularity:ModuleInfo 
         Ref="Prism101.Modules.Core.xap"
         ModuleName="CoreModule" 
         ModuleType="Prism101.Modules.Core.CoreModule, Prism101.Modules.Core, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"
     />

</Modularity:ModuleCatalog>

Step 3: Implement your module: Notice how the framework will initialize your RegionManager and UnityContainer for you. Also note that the "WelcomeView" is a usercontrol that gets mapped to the maincontent region of the client's shell.

namespace Prism101.Modules.Core
{
    public class CoreModule : IModule
    {
        private readonly IUnityContainer container;
        private readonly IRegionManager regionManager;

        public CoreModule(IUnityContainer container, IRegionManager regionManager)
        {
            this.container = container;
            this.regionManager = regionManager;
        }

        public void Initialize()
        {
            var view = new WelcomeView();
            regionManager.AddToRegion("MainContent", view);
        }
    }
}

Step 4: Implement Bootstrapper: In the CreateModuleCatalog method, identify the name of the assembly that your catalog module is in as well as it's directory path.

class Bootstrapper : UnityBootstrapper
{
    protected override DependencyObject CreateShell()
    {
        var shell = new MainWindow();
        Application.Current.MainWindow = shell;

        return shell;
    }

    protected override IModuleCatalog CreateModuleCatalog()
    {
        var assemblyName = "Prism101.Modules.Core";
        var filePath = "ModuleCatalog.xaml";
        var uriPath = string.Format("/{0};component/{1}", assemblyName, filePath);

        var uri = new Uri(uriPath, UriKind.Relative);
        var moduleCatalog = Microsoft.Practices.Prism.Modularity.ModuleCatalog.CreateFromXaml(uri);

        return moduleCatalog;
    }

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

Conclusion: After reviewing these steps, one should be able to run the application and observe the results of bootstrapping a prism app with a module getting loaded.

NOTE: Please feel free to provide feedback on how I could've articulated this better.

Scott Nimrod
  • 11,206
  • 11
  • 54
  • 118