deactivate a view

Topics: Prism v2 - WPF 3.5
Jun 5, 2009 at 3:46 PM
Edited Jun 5, 2009 at 4:02 PM

Hi,

In my application I am loading the different Module On-Demand by clicking on a button which appear when the user identify himself in the AuthenticateModule.

         private void MemberModuleButton_Click(object sender, RoutedEventArgs e)
        {
            Bootstrapper.LoadModule("memberModule");
        }

        private void ContratModuleButton_Click(object sender, RoutedEventArgs e)
        {
             Bootstrapper.LoadModule("contratModule");

        }

All my module are stored in the ModuleCatalog in my Bootstrapper and I created some region in the Shell :

<ItemsControl  cal:RegionManager.RegionName="{x:Static infrastructure:RegionNames.ContratRegion}" />

 

Now, when the user is logged and clicked to the "ContractModuleButton", I registered the View with the region :

 regionManager.RegisterViewWithRegion(RegionNames.ContratRegion,typeof(ContratsView));

 

Everything work fine but when the user logged off, I want to deactivate the view in order to return to the basic shell.

I tried to use this method without success and I had a error message : "Deactivation is not possible in this type of region. "

var view = container.Resolve<ContratsView>();         

region.Add(view);
region.Deactivate(view);

 

Thanks for your help !

Best regards

 

Vlado

 

 

 

 

Jun 5, 2009 at 9:38 PM

Hi Vlado,

 

This exception is most likely being thrown because you are using an ItemsControl as your region. This kind of control is mapped to an AllActiveRegion, which can hold multiple views and all of them are active at the same time. Therefore, there is no way to activate a view in an AllActiveRegion. There are two options to avoid the exception:

1.       If you want to stop showing that view and still use the AllActiveRegion you should remove the view from the region or make it not visible.

2.       If you consider a ContentControl would suit your scenario, you will be able to deactivate views, since only one view can be active in a region adapted in a ContentControl (SingleActiveRegion).

 

Please let me know if this helps.

 

Damian Schenkelman

http://blogs.southworks.net/dschenkelman

Jun 9, 2009 at 3:44 PM

Another time, thanks for your help.

It's work well now with just a simple method :

        public static void UnloadModule() //deactivate View
        {
            foreach (var region in regionManager.Regions)
            {
                if (region.Views.Count() > 0 && region.Name.CompareTo("AuthenticateRegion")!=0)
                {
                    region.Deactivate(region.Views.First());
                }             
            }
        }

and exactly the opposite in order to reactivate the view when another user is logged.

Thanks.

 

Vlado