Region.Activate problem

Dec 9, 2008 at 6:19 PM
Hi

I'm building an application, that has a MainRegion that will be used by all the modules.

When each module is loaded, it adds a button to a toolbar, that activates de module.

I've wired a DelegateCommand to this button, that calls this code on the module:

private

 

void ActivateView()

 

{
    

IChampionshipsPresenter presenter = _container.Resolve<IChampionshipsPresenter>();
    
IRegion mainRegion = _regionManager.Regions["MainRegion"];

    
if (!mainRegion.Views.Contains(presenter.View) )
        mainRegion.Add(presenter.View);

    mainRegion.Activate(presenter.View);
}

The problem is that each time this code gets called, it adds a new view. I've debugged the app, and the View is in Reagions.Views collection, but the Views.Contains(presenter.View) always returns false.

The xaml for the MainRegion is:

 

<

 

TabControl cwpf:RegionManager.RegionName="MainRegion" Margin="1,1,1,1"/>

 

 

 

Any ideia on whats going on?

Thanks

Dec 11, 2008 at 2:06 PM

Hi Pedro,

 

The Contains method verifies is the view instance passed as a parameter is inside the region. The problem is that you are creating a new instance of the view (through the presenter) every time that command is executed.

To solve this, you can add a named view using the overload in the Add method of the region. Then you can check if that named view is in the region by using the GetView method without need of creating a new instance every time. Check the following sample code:

 

private void ActivateView()

{

IRegion mainRegion = _regionManager.Regions["MainRegion"];

object view = mainRegion.GetView("MyView");

 

if (view == null)

{

IChampionshipsPresenter presenter = _container.Resolve<IChampionshipsPresenter>();

view = presenter.View;

mainRegion.Add(view, "MyView");

}

 

mainRegion.Activate(view);

}

 

You can find more samples in the UI composition Quickstart, where named views are used to show the information of every employee.

 

Please let me know if this helps.

 

Mariano Converti

http://blogs.southworks.net/mconverti