Load view from another

Topics: Prism v2 - WPF 3.5
Oct 1, 2009 at 7:57 AM

Hi,

I'm new to prism and try to build my first app. So, Apologizes if my question sounds stupid.

Got two views in the same module. The first one is loaded at module init and displayed as a RibbonTab.

In this tab, I've got a simple button which I want to load another view of the module in another region than the Ribbon.

How can I achieve that ? How can I get access to regionManager from the first view in order to load the second one ? GlobalCommand ? Service ?

 

Many thanks for your help.

 

Oct 2, 2009 at 12:17 AM

Hi

To access the RegionManager you should use either constructor or property injection in the class that will need access to the RegionManager..

The thread below explains the code required to add the view to the region on demand:

You can check the ViewDiscovery/ViewInjection Quickstarts and the Prism RI as they show views on demand in different scenarios.

Please let me know if this helps.

Damian Schenkelman
http://blogs.southworks.net/dschenkelman

Oct 5, 2009 at 10:13 AM

Create an ICommand property in your ViewModel and bind it to Button in the View:

public ICommand Command
{
    get
    {
        if (_command == null)
        {
            _command = new DelegateCommand<object>(this.Execute, this.CanExecute);
        }
        return _command;
    }
}

Raise an event from the Execute function using an EventHandler declared in the related ViewModel:

public event EventHandler<MenuEventArgs> MenuHandler;

private void Execute(object param)
{
    EventHandler<MenuEventArgs> handler = this.MenuHandler;
    if (handler != null)
        handler(this, new MenuEventArgs("viewClass", "presenterClass"));
}

Since you have declared the EventHandler public, the Controller class which loads the first View can gain access to it and respond to events raised by it.

public HeaderController(IRegionManager regionManager, IMenuViewModel menuViewModel)
{
this.regionManager = regionManager;
this.menuViewModel = menuViewModel;
this.menuViewModel.MenuHandler += new EventHandler<MenuEventArgs>(menuViewModel_MenuHandler);
this.regionManager.Regions["HeaderRegion"].Add(menuViewModel.View, "MenuView");
}

Something like the above code should be able to handle events raised by your first View. I have used EventHandler<T> to pass parameters, a Generic EventHandler should also work.

HTH.