Wednesday, September 2, 2009

Specify Modules in XAML

When I first started PRISM my thought was how to specify the modules through a xaml file or an xml fille.The existing code is written as like something hard coded in the Bootstraper.cs.

protected override Microsoft.Practices.Composite.Modularity.IModuleCatalog GetModuleCatalog()
{
    var catalog = new ModuleCatalog();
    catalog.AddModule(typeof(MarkModule));
    catalog.AddModule(typeof(StudentsListModule));
    return catalog;
}

We can easily setup an xml file and create module objects using reflection.But I was sure, there will be an implementation of same somewhere in the framework.

After spending some time, I got a new way to implement my scenario.Why don't I have a new xaml file derived from ModuleCatalog and make the entries in xaml ? Yes that did the trick.

I added a new file which derives from ModuleCatelog and added my module entries.The xaml looks as below.

<modularity:ModuleCatalog 
    x:Class="Students.StudentsModuleCatalog"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:modularity="clr-namespace:Microsoft.Practices.Composite.Modularity;assembly=Microsoft.Practices.Composite">
 
    <modularity:ModuleInfo 
        ModuleName="StudentsListModule"
        ModuleType="Students.StudentsListModule, Students, Version=1.0.0.0" />
    <modularity:ModuleInfo 
        ModuleName="MarKModule"
        ModuleType="Students.MarkModule, Students, Version=1.0.0.0" />
 
</modularity:ModuleCatalog>

Make sure that the code behind class should inherit from ModuleCatalog.

Now you can Rewrite the Bootstraper.GetModuleCatalog() as follows

protected override Microsoft.Practices.Composite.Modularity.IModuleCatalog GetModuleCatalog()
{
    return  new StudentsModuleCatalog();
}
This makes the code more neat and we can easily add or remove modules.

No comments: