How to enter the URI to setup the ModuleCatalog in Prism WPF
I am getting a bit frustrated with prism. Something that should be so easy is really getting me stuck!
I am trying to load my ModuleCatalog from a file. I created the ModuleCatalog.xaml file in my shell project. In the file's properties I have removed the Custom Tool action and I have set Build Action to Resource (I also tried Content).
Here is the code I have:
public class Bootstrapper : UnityBootstrapper
{
protected override IModuleCatalog GetModuleCatalog()
{
var uri = new Uri("/ShellProject;component/ModuleCatalog.xaml", UriKind.Relative);
var catalog = ModuleCatalog.CreateFromXaml(uri);
return catalog;
// I have also tried this:
//return (ModuleCatalog.CreateFromXaml(
// new Uri("ModuleCatalog.xaml", UriKind.Relative)));
}
...
When I run I get the following error:
The IModuleCatalog is required and cannot be开发者_StackOverflow null in order to initialize the modules.
I am stumped. The blogs I have read and the videos I have watched seem to indicate I am doing it right.
I can't think I am the only one to ever have loaded my configuration from a xaml file in wpf, but I can't find anyway around this.
Any help would be great!
NOTES:
- I am using a WPF App. There are tons of examples on how to do this in silverlight. I need it in for WPF.
- I 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-a-wpf-application-using-the-composite-application-guidance-for-wpf-silverlight-prism-v2/
This is what I did that finally worked:
protected override IModuleCatalog GetModuleCatalog()
{
FileStream catalogStream = new FileStream(@".\ModuleCatalog.xaml",FileMode.Open);
var catalog = ModuleCatalog.CreateFromXaml(catalogStream);
catalogStream.Dispose();
return catalog;
}
I ran into the same issue, but I was trying to do it with MEF and WPF. Documented samples are still a little weak in this area, but I managed to get it working. Looking at what you have above, it appears that you are overriding the wrong method for what you want to do. You just want to create the module catalog, not configure it. I would recommend the following code in the boostrapper for MEF or Unity users trying to do this in WPF:
protected override IModuleCatalog CreateModuleCatalog()
{
// MEF and Unity **BOTH** use the ModuleCatalog when configuring from a file
return Microsoft.Practices.Prism.Modularity.ModuleCatalog.CreateFromXaml(
System.IO.File.OpenRead("catalog.xaml"));
}
Assuming you have a "WpfModule1.dll" that uses the namespace "WpfModule"1 and has a class named "WpfModule" that inherits from "IModule" you will want the following "catalog.xaml" config file:
<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 Ref="file:///WpfModule1.dll"
ModuleName="WpfModule"
ModuleType="WpfModule1.WpfModule, WpfModule1, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"
InitializationMode="WhenAvailable" />
</Modularity:ModuleCatalog>
Note you also have to change the properties on the config file to Build Action:"None" and Copy To Output Directory:"Copy if Newer". Hope that helps.
To figure out what is going on you could build the Prism projects and use the .pdb files to debug them or you could simply include said projects in your solution and debug them directly from there.
If this is a large solution, just try this using a basic Quickstarts and then update your solution based on your findings from debugging the QS.
One of the huge benefits of Prism releasing its source is that in these kind of situations you can simply dig into it step by step to check where the issue is.
Prism is really picky about this and you must be very precise when using ModuleCatalog.CreateFromXaml()
- Compile the ModuleCatalog.xaml with a Build Action of Page
- Include the Version number (typically 1.0.0.0 if you didn't change anything) in the declaration. e.g.: ModuleType="MyProj.ModuleB.ModuleBModule, MyProj.ModuleB, Version=1.0.0.0"
In our case, we were getting the same error:
The IModuleCatalog is required and cannot be null in order to initialize the modules.
after updating from Prism 4.x to 5.0. It turned out to be because of the assembly reorganization -- the assembly Microsoft.Practices.Prism referenced in the ModuleCatalog.xaml no longer exists; it is now Microsoft.Practices.Prism.Composition. Easy to overlook the squiggly line in the IDE.
So for Prism 5.0, the root element in ModuleCatalog.xaml should look like this:
<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">
精彩评论