Can't load modules

Topics: Prism v4 - Silverlight 4
Jul 12, 2012 at 1:01 AM
Edited Jul 12, 2012 at 1:04 AM

My project is unable to load any module. I'm using Silverlight 4 and Prism 4.

This project is intended to practice event aggreation.

It throws an exception at

        private void Application_Startup(object sender, StartupEventArgs e) {
            (new MyBootstrapper()).Run();
        }

The exception: "There is currently no moduleTypeLoader in the ModuleManager that can retrieve the specified module."

My Bootstrapper is:

public class MyBootstrapper : UnityBootstrapper {
    private const string ModuleCatalogUri = "/EventAggregator;component/ModulesCatalog.xaml";

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

    protected override DependencyObject CreateShell() {
        // Use the container to create an instance of the shell.
        Shell view = this.Container.TryResolve<Shell>();

        // Set it as the root visual for the application.
        Application.Current.RootVisual = view;

        return view;
    }
}

My ModuleCatalog.xaml is:

<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 ModuleName="ModuleA"
                           ModuleType="ModuleA.ModuleA, ModuleA, Version=1.0.0.0, Culture=neutral,
                PublicKeyToken=null">
    </Modularity:ModuleInfo>
    <Modularity:ModuleInfo ModuleName="ModuleB"
                           ModuleType="ModuleB.ModuleB, ModuleB, Version=1.0.0.0, Culture=neutral,
                PublicKeyToken=null">
    </Modularity:ModuleInfo>
</Modularity:ModuleCatalog>

My Shell.xaml is:

<UserControl x:Class="EventAggregator.Shell"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
    d:DesignHeight="300" d:DesignWidth="400"
             xmlns:Prism="http://www.codeplex.com/prism"
    >
    
    <Grid x:Name="LayoutRoot" Background="White">
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="192*" />
            <ColumnDefinition Width="208*" />
        </Grid.ColumnDefinitions>
        <Border BorderBrush="Silver"
                BorderThickness="1"
                Height="300"
                HorizontalAlignment="Left"
                Name="LeftBorder"
                VerticalAlignment="Top"
                Width="192"
                Grid.Column="0">
            <ContentControl x:Name="LeftRegion"
                            Prism:RegionManager.RegionName="LeftRegion"
                            VerticalContentAlignment="Stretch"
                            HorizontalContentAlignment="Stretch" />
        </Border>
        <Border BorderBrush="Silver"
                BorderThickness="1"
                Height="300"
                HorizontalAlignment="Left"
                Name="RightBorder"
                VerticalAlignment="Top"
                Width="192"
                Grid.Column="1">
            <ContentControl x:Name="RightRegion"
                            Prism:RegionManager.RegionName="RightRegion"
                            VerticalContentAlignment="Stretch"
                            HorizontalContentAlignment="Stretch" />
        </Border>
    </Grid>
</UserControl> 

ModuleA.cs is:

public class ModuleA : IModule {
    private readonly IUnityContainer container;
    private readonly IRegionManager regionManager;

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

    public void Initialize() {
        this.regionManager.RegisterViewWithRegion("LeftRegion",
                                                    () => this.container.Resolve<ModuleAView>());
    }
}

And ModuleB is very similar.

Developer
Jul 12, 2012 at 12:45 PM

Hi Rafael,

Based on my understanding, a possible cause for the error you are mentioning could be that your are not defining the Ref property for your modules in your Modulecatalog. In this property you have to define the Xap File Name, where your modules reside.

For example like this:

<Modularity:ModuleInfo Ref="MyModuleProject.xap" ModuleName="ModuleA" ModuleType="ModuleA.ModuleA, ModuleA, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null">

Regards,

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

Jul 12, 2012 at 1:47 PM

Thank you Agustin. That was it.

But I have some other questions,

  • Do I always have to put each module in a separate xap file?,
  • I have two modules, ModuleA and ModuleB, can I have both modules in one module named ModuleA.xap?,
  • Can I set the xap files arbitrarily, or do I have to name them after each module?
Developer
Jul 12, 2012 at 4:47 PM
Edited Jul 12, 2012 at 4:52 PM

Hi,

I'm glad you could solve your problem.

Regarding your questions, the answer will be yes, you can have more than one module per XAP file, for example if you consider to minimize the number and size of download requests required for the application to start up. Hence, you shouldn't have problems to place both of your modules for example in one Xap file named ModuleA.xap, as long as you defined each ModuleInfo with its corresponding properties in your ModuleCatalog.

Also, as far as I know the name of your Xap file could be arbitrary, and not necessarily  needs to be the same as your module module name.

For more information about this you could check the following section of the Prism documentation:

Additionally, when having modules that are in the same Xap file you could benefit from the use of ModuleInfoGroups, as for example you will be sharing the Ref property for this modules.

Regards,

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

Jul 12, 2012 at 5:36 PM

Thank you