View Not Shown, althought it is loaded !! SOLVED

Topics: Prism v4 - Silverlight 4
Feb 27, 2011 at 3:06 PM

Dear All,

I load a view using the following code:

RegionManager.RegisterViewWithRegion(RegionNames.AccountsDetailsRegion, () => ServiceLocator.Current.GetInstance<AccountsView>());

This view is showing a list of accounts, when the user select any and click edit, the view needs to load another View(with specific parameters) 
within the same region to show the details of the selected account
so I load the view using the following code:

  RegionManager.RequestNavigate(RegionNames.AccountsDetailsRegion, new Uri(builder.ToString(), UriKind.Relative),NavigationCompleted);

In the debugger the region manager successfully loads the view and I can see it, also the NavigationCompleted event handler shows no errors...

What is possibly causing this .... !!

Best regards

Waleed

Feb 28, 2011 at 7:35 AM

I have found that I am using ItemsControl to host the region not a ContentControl ...

What is the difference, ItemsControl should be fine, right ...

One more thing related to the issue;

in the navigated view, I have a goback command and the navigationJournal is okay, but not going back, any help on this .

Best regards

Waleed

Feb 28, 2011 at 12:37 PM

Dear all

If anyone is intrested, the fix is load the first view using RequestNavigate in order to register this view as the start of the navigation.

Best regards

Waleed

Developer
Feb 28, 2011 at 6:07 PM

Hi Waleed,

I'm glad that you've solved your problem regarding navigation, and I thank you for sharing your insight with the rest of the community.

As regarding your initial concern, there should be no difference between using an ItemsControl and a ContentControl in your case. For example, you could check the HelloWorld QuickStart, which has a region that is hosted on an ItemsControl, in which a single item is added.

We've also tried adding a view to a region hosted in an ItemsControl using navigation, as you've done, and we faced no difficulties. Note that if you're using Unity as the Dependency Injection container, you should register a type mapping similar to the following in order for region navigation to work:

 

ServiceLocator.Current.GetInstance<IUnityContainer>().RegisterType(typeof(object),typeof(Views.HelloWorldView),"HelloWorldView");

 

This is necessary since the default implementation of the RegionNavigationContentLoader internally tries to resolve an instance of type object with the name specified in the RequestNavigate method (which, in case you're using MEF, is the contract name specified in the Export attribute).

Here's the code of the RegionNavigationContentLoader.CreateNewRegionItem method to illustrate the aforementioned:

 

        protected virtual object CreateNewRegionItem(string candidateTargetContract)
        {
            object newRegionItem;
            try
            {          
                newRegionItem = this.serviceLocator.GetInstance<object>(candidateTargetContract);
            }
            catch (ActivationException e)
            {
                throw new InvalidOperationException(
                    string.Format(CultureInfo.CurrentCulture, Resources.CannotCreateNavigationTarget, candidateTargetContract),
                    e);
            }
            return newRegionItem;
        }

 

If you are blocked by this behavior and can't find the reason why it happens, it could be helpful if you could provide us with a repro sample, so that we can help you diagnose what could be causing your issue.

I hope you find this helpful.

Guido Leandro Maliandi
http://blogs.southworks.net/gmaliandi

Mar 1, 2011 at 4:09 PM

I agree with you the ItemsControl is the same as the ContentControl.

Appreciate your feedback...

I need to ask about the CreationPolicy of the views (shared). how can this be used ... I always get the view already exists when I call the RequestNavigate.

Any advice will be great.

Waleed

 

Developer
Mar 1, 2011 at 4:20 PM

In order to specify that an export should not act as a singleton, you need to decorate your exported class with the PartCreationPolicy attribute in the following manner:

[PartCreationPolicy(CreationPolicy.NonShared)]

I hope you find this helpful.

Guido Leandro Maliandi
http://blogs.southworks.net/gmaliandi

Mar 1, 2011 at 4:27 PM

Yes Guido, this is what I am doing ... but when I try to load the same view again using RequestNavigate I got an error saying view already exists !!

Is there any handling script to this, or this should be working automatically by PRISM .. as I assume.

Please advice

Waleed

Developer
Mar 1, 2011 at 4:46 PM

Waleed,

The problem you're experiencing could be happening because the INavigationAware.NavigationTarget method in your view is returning true, hence the navigation service considers that the view already exists.

You might find the explanation in the Navigation chapter of the Prism MSDN documentation useful for this.

Guido Leandro Maliandi
http://blogs.southworks.net/gmaliandi

Mar 1, 2011 at 6:54 PM

Dear Guido,

The explanation is understood ... what is the solution .. should I activate the existing view or what ...

Regards

Waleed

Developer
Mar 2, 2011 at 1:29 PM

Hi Waleed,

The solution I referred to in my previous answer was to make the INavigationAware.NavigationTarget method in your view to return false.

In case that doesn't work, it would be helpful if you could provide us with further details regarding how you're implementing Navigation, or a repro sample for us to help you further.

I hope you find this helpful.

Guido Leandro Maliandi
http://blogs.southworks.net/gmaliandi

Mar 5, 2011 at 8:39 PM

Dear Guido,

I found the solution in this post : http://compositewpf.codeplex.com/discussions/247930

I implemented the IRegionMemberLifetime interface and everthing is working as excpected.

Thanks

Waleed