Need help navigating to seperate modules

Topics: Prism v4 - Silverlight 4
Apr 12, 2011 at 4:20 PM

Hi,

I am new to Prism and MEF but I am in need of assistance with an application that I am creating. I have a large application that is built with multiple modules. The first module that shows is a login screen. Once user has entered a login, I need the application to load a view from another module within the shell. I have been stuck on this for the past few days. There are plenty of examples of navigating views within the same module but I have not found any examples of navigating views within seperate modules. Can someone direct me to an example please.

Thanks!

Developer
Apr 12, 2011 at 4:52 PM
Edited Apr 12, 2011 at 4:53 PM

Hi,

As Region Navigation is designed to be able to navigate between loosely coupled views, there should be no difference between navigating to a view in a separate module, as you only need to pass a Uri to the RequestNavigate method.

You could check the View-Switching Navigation QuickStart, which might be useful to learn more about this.

I hope you find this helpful.

Guido Leandro Maliandi
http://blogs.southworks.net/gmaliandi

Apr 12, 2011 at 7:20 PM
Edited Apr 12, 2011 at 7:22 PM

Thanks for the reply Guido but I am coming across some annoying errors. I cant even seem to load up the login module as the first page. Im not even on the navigation part yet. I have copied the code exactly as it shows on the View-Switching Navigation Quickstart solution. Here is what I have, maybe you can see what I am not:

LoginView

[Export(

"LoginView")]
    public partial class LoginView : UserControl
    {
        public LoginView()
        {
          
            InitializeComponent();
        }


      }

 

Bootstrapper

public class Bootstrapper : MefBootstrapper
    {

        protected override DependencyObject CreateShell()
        {
            return this.Container.GetExportedValue<Shell>();
        }

        protected override void ConfigureAggregateCatalog()
        {
            base.ConfigureAggregateCatalog();
            this.AggregateCatalog.Catalogs.Add(new AssemblyCatalog(typeof(Bootstrapper).Assembly));
        }

        protected override void InitializeShell()
        {
            base.InitializeShell();
            Application.Current.RootVisual = (UIElement)this.Shell;
           
        }

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

 

LoginModule

[ModuleExport(typeof(LoginModule))]
    public class LoginModule : IModule
    {
        [Import]
        public IRegionManager regionManager;

        public void Initialize()
        {
           this.regionManager.RegisterViewWithRegion(RegionNames.MainRegion, typeof(LoginView));
        }
    }

Apr 12, 2011 at 7:23 PM

and the shell:

[Export]
    public partial class Shell : UserControl, IPartImportsSatisfiedNotification
    {
        public Shell()
        {
            InitializeComponent();
        }

        [Import(AllowRecomposition = false)]
        public IModuleManager moduleManager;

        [Import(AllowRecomposition = false)]
        public IRegionManager regionManager;

        /// <summary>
        /// Called when a part's imports have been satisfied and it is safe to use.
        /// </summary>
        public void OnImportsSatisfied()
        {
            this.moduleManager.LoadModuleCompleted += (s, e) =>
                {
                    if (e.ModuleInfo.ModuleName == "LoginModule")
                    {
                        this.regionManager.RequestNavigate(
                            RegionNames.MainRegion, new Uri("LoginView", UriKind.Relative));
                    }
                };
        }
    }

 

Please let me know what I am missing....it looks so simple but is turning out to be more trouble than benefit.

Apr 12, 2011 at 10:31 PM

Anyone see what I am missing?

Developer
Apr 13, 2011 at 2:11 PM

Hi,

The RegisterViewWithRegion method makes your view to be resolved and added to the region once the region is created. That method, however, will try to get an export with the contract name that is the type you've specified. However, your view is registered with another contract type ("LoginView"), therefore that method will cause an error because the view wont' be found by the service locator.

You could try replacing the call to RegisterViewWithRegion in your module's Initialize method with the call to RequestNavigate you have on the LoadModuleCompleted event handler, and performing no navigation in the LoadModuleCompleted event.

You might find the following chapters of the Prism MSDN documentation useful:

I hope you find this helpful.

Guido Leandro Maliandi
http://blogs.southworks.net/gmaliandi