Region Hoster become region view (child)

Topics: Prism v4 - Silverlight 4
Apr 21, 2011 at 3:19 PM

Hi, I have a very strange behavior.

In my shell, I have 1 region (rootpageregion).

When LoginModule loaded, in it's Initialize() method I call this:

 

_regionManager.Regions[Uri.Root.RegionName].NavigationService.RequestNavigate(Uri.Login.LoginViewUri);

 

Which register my LoginPage view to rootpageregion and inside this view (LoginPage) there is a region (loginpageregion).

In my LoginPage.OnNavigatedTo I have this:

 

            IRegion region = _regionManager.Regions[Uri.Login.RegionName];

            if (_loginView == null)
            {
                _loginView = Mef.Container.GetExportedValue<LoginView>();
                region.Add(_loginView);
            }

            if (_roleSelectorView == null)
            {
                _roleSelectorView = Mef.Container.GetExportedValue<RoleSelectorView>();
                region.Add(_roleSelectorView);
            }

            if (_testView == null)
            {
                _testView = Mef.Container.GetExportedValue<TestView>();
                region.Add(_testView);
            }

            switch ((Uri.Login.Parameter)Enum.Parse(typeof(Uri.Login.Parameter), navigationContext.Parameters[Uri.Login.Action], true))
            {
                case Uri.Login.Parameter.LoginView:
                    region.Activate(_loginView);
                    break;

                case Uri.Login.Parameter.RoleSelectorView:
                    region.Activate(_roleSelectorView);
                    break;

                case Uri.Login.Parameter.TestView:
                    region.Activate(_testView);
                    break;
            }

Uri.Login.RegionName is a const: "loginpageregion"

Everything working fine until the code in the _loginView run this code:
            _regionManager.RequestNavigate(Uri.Login.RegionName, Uri.Login.LoginViewUri
                , (e2) =>
                {
                    System.Windows.MessageBox.Show(e2.Error.ToString());
                });

e2.Error contains "System.ArgumentException: Value does not fall within the expected range."
The most strange is, before the blue line above executed, "loginpageregion" still has 3 views (see the picture).
After executed, in the red line it has 4 views now. LoginPage view added to it (see the picture).

 

Am I doing something wrong, please help...

I'm new to prism, thanks.

Apr 23, 2011 at 8:10 AM

It looks like this behavior is related to this thread. Just take a look to my post there.

So everytime I try to navigate back, it will create a new instance.

And since the target is the child region, that's why the new instance is added to it's view.

Spent days to figure this out...