tags:

views:

998

answers:

2

I'm starting a new project in Wpf, and am now looking into using Prism. For now I'm simply trying to set up the navigation of the application using Prism. Unfortunatelly my lack of experience with the framework makes it a bit difficult to get started..

To be more precise about my first challenge I have an application with a "navigation/menu" region and a "main" region. The navigation region will be the same for all different main region views, and I therefore define the menu in the shell.xaml. When clicking any menu item I'd like to add a view to the region using Prism. First; is this something one typically will use Prism for? If so; what's the typical approach? And I mean on a more structural level..

My impression is that Prism will make my application much more scalable in the end, and I see that I get some other advantages from it - like the IoC container. So I would like to use it - if I could only get through the first steps..

Thx in advance for any feedback!

+1  A: 

Bumbuska,

Prism will be a great way for you to achieve this functionality and it is pretty easy to do once you understand the principals.

The way I will do it is to add event listeners in the start up event of your Main Region. When you select your item in the menu, you fire the event. When that happens your Main Region will handle the event and you clear the current view from the Main Region. Then create the new view you want to use and add it.

Your main region should look something like this:

public void Initialize()
{
    Events.PageEvents.ClickedEvent1 ce1 = this.eventAggregator.GetEvent<Events.PageEvents.ClickedEvent1>();
    ce1.Subscribe(LoadView, ThreadOption.UIThread, true);
}

private void LoadView(Events.HomePageEvents.Clicked clicked1)
{
    IRegion mainRegion = RegionManager.Regions["MainRegion"];
    foreach (object view in new List<object>mainRegion.Views))
    {
        RegionManager.Regions["MainRegion"].Remove(view);
    }

    IModule firstModule = Container.Resolve<Modules.FirstModule>();
    firstModuleModules.Initialize();

}

I hope that points you in the right direction. Please let me know if you need any more info.

Cornelius Kruger
Thx! This is good help for me. I got it working like I wanted now, but I'm not using event aggregation. I'm using command bindings on the menu items, which are some viewModel-objects holding information about which component to open in the main region. This seems fine to me - but is event aggregation a more preferred way? Thx for the region clearing example. I was hoping to find a function for setting an element in a view instead of clearing all exising and adding the new one though. But your example illustrates the typical approach?
stiank81
It really depends on your application wheather command or events are better. I think for the main Navigation Menu commands would probably work better.You can have all your views active in your Main region and just activate the one that you want to use. Again this will depend on your application, but for me I would not load something that might not get used.
Cornelius Kruger
Okay - for me it seems reasonable without the events for now. Maybe I'll change my opinion later on, but it works for for now. Thx again for your help!
stiank81
+1  A: 

I've got a sample that uses a little more of the CAG feel for modules contributing to a menu and how to add views to a region. It ought to make things a little more clear.

http://dl.getdropbox.com/u/376992/CAGMenus.zip

Hope this helps, Anderson

Anderson Imes
Thx. I already got this sample. I got the concepts, but I couldn't really get my arms around it and put it all together in my own applications. But with some further investigation this sample really helped me after all - so I'm on the right track now.
stiank81
Glad I could help.
Anderson Imes
Marking this as the answer as the provided sample code was what lead to the solution. Thx!
stiank81