• How to: Populate the Module Catalog from XAML in a WPF application using the Composite Application Guidance for WPF & Silverlight (Prism-v2)

    Published by Damian Schenkelman on August 9th, 2009 7:31 am under Composite Application Guidance for WPF & SL, Patterns & Practices, Prism-v2, WPF, Windows Presentation Foundation, p&p

    6 Comments

    The other day I was trying to create a new WPF Prism application and I wanted to be able to load modules from XAML. To make things simpler I decided to check the documentation, but was not able to find any example on how to get this done, as the sample is for Silverlight applications. After this “revelation” I started my research on how this could be done. The most important question was what to put in the Ref attribute, which is where the module assembly is located.

    First, and as it is done in the Silverlight approach, the GetModuleCatalog method of the UnityBootstrapper must be overriden with code similar to this:

    protected override IModuleCatalog GetModuleCatalog()
          {
              return
                 ModuleCatalog.CreateFromXaml(
                     new Uri("/ProjectName;component/ModulesCatalog.xaml", UriKind.Relative));
          }

    It came to my mind that I had once answered a related question in the Prism forums, so I thought that would be a good place to start. As it is explained in the question, the Ref attribute must start “file://”, but what else should I add to this attribute?

    It turns out the answer is quiet simple (fortunately). You simply have to make sure the module’s assembly is copied when built in the same folder as your Shell project is and since one of the module’s I wanted to load was called SearchModule, this is the XAML outcome:

    <prism:ModuleInfo Ref="file://SearchModule.dll"
                ModuleName="SearchModule"
                ModuleType="SearchModule.ModuleInit, SearchModule, Version=1.0.0.0"
                InitializationMode="WhenAvailable">
    </prism:ModuleInfo>

    The rest of the attributes remain the same as in the Silverlight approach.

    I hope this is useful and saves you some trouble for you if you take this approach to load modules in your Prism WPF application.

    kick it on DotNetKicks.com

    Shout it

    Tags: , , , , ,

  • 6 Comments:

    1. DotNetShoutout said on August 9, 2009:

      How to: Populate the Module Catalog from XAML in a WPF application using the Composite Application Guidance for WPF & Silverlight (Prism-v2)…

      Thank you for submitting this cool story – Trackback from DotNetShoutout…

    2. stu said on January 21, 2010:

      So I’m starting to use prism and I’m wondering what the advantages are loading from XAML instead of from a coded (and type safe) myCatalog.AddModule( typeof (Foo) ) in GetModuleCatalog.

      Any ideas?

    3. Damian Schenkelman said on January 25, 2010:

      Hi Stu,

      The main advantage to using any other kind of module loading other than the in-code approach, is that there is no need to reference your module assemblies from your Shell. This allows you to decouple your Shell from your application modules and load different modules without the need to re-build the application, as you can just modify a configuration file.

      You can take a look at the Modularity Quickstarts to see how this works. Take into account that there is no Modularity QS that shows how to load modules from XAML in WPF.

      Please let me know if this helps.

      Damian

    4. Tobias Walter said on January 27, 2010:

      Thank you for that help, but I’m still having troubles in loading a xaml-catalog with WPF.

      I’ve added a Catalog.xaml as Content to my project and if I do:

      var uri = new Uri(“/Assembly;component/Catalog.xaml”, UriKind.Relative);
      var foo = System.Windows.Application.GetResourceStream(uri);

      foo is null. As well as the catalog when calling ModuleCatalog.CreateFromXaml(uri). It seems that the Catalog.xaml is found because if I change the uri to, say, Catalog.xml, an Exception will be thrown.

      Any ideas?

      Tobias

    5. How to enter the URI to setup the ModuleCatalog in Prism WPF | DeveloperQuestion.com said on October 24, 2010:

      [...] have looked at this post and have not found it helpful for my issue: http://blogs.southworks.net/dschenkelman/2009/08/09/how-to-populate-the-module-catalog-from-xaml-in-... Question by This question originated from [...]

    6. Scott Rudy said on December 28, 2010:

      Update for Prism 4 with MEF. There is an existing thread that discusses this topic at stack overflow:

      http://stackoverflow.com/questions/4007149/how-to-enter-the-uri-to-setup-the-modulecatalog-in-prism-wpf

    Leave a comment

    Your email address will not be published.

Categories

Archives

Map

SO Profile

Certifications

Microsoft Certified Professional Microsoft Technology Specialist (Silverlight 4 Development)