Prism-MEF: how to navigate to view in different module using UriQuey

Topics: Prism v4 - WPF 4
Feb 13, 2013 at 5:11 PM
Edited Feb 13, 2013 at 5:17 PM
if i have this module structure:
ModuleA
      *View/View_A
      *Controller/ControllerA
ModuleB
      *View/View_B
      *ViewModel/ViewModel_B  (view model for View_B)
      *Controller/Controller_B
1-how could ControllerA use UriQuery to display View_B in a region inside View_A
2-how could controllerB use UriQuery to display View_B in a region inside View_A
3-which is better for displaying View_B in a region in View_A

Thanks in advance
Feb 14, 2013 at 2:41 PM
Edited Feb 14, 2013 at 3:24 PM
Feb 15, 2013 at 6:37 AM
Hi isakavis
Thanks for your effort.

I used following code in ControllerA to navigate to View_B (TestPacksView) and send the parameter
SubsystemNo
 var uriQuery = new UriQuery();
            if (Subsys != null)
            {
                uriQuery.Add("SubsystemNo", Subsys.SubsystemNo);

            }

            var uri = new Uri("TestPacksView" + uriQuery.ToString(), UriKind.Relative);
           
            regionManager.RequestNavigate("TestPackGridRegion", uri);

        }
then i got this ActivationException:


"Activation error occured while trying to get instance of type Object, key "TestPacksView"

i don't know if this is because View_B (TestPacksView) in different module or because the parameter
"SubsystemNo" is not received by the ViewModel_B hence View_B not constructed ...
Feb 15, 2013 at 6:37 AM
Hi isakavis
Thanks for your effort.

I used following code in ControllerA to navigate to View_B (TestPacksView) and send the parameter
SubsystemNo
 var uriQuery = new UriQuery();
            if (Subsys != null)
            {
                uriQuery.Add("SubsystemNo", Subsys.SubsystemNo);

            }

            var uri = new Uri("TestPacksView" + uriQuery.ToString(), UriKind.Relative);
           
            regionManager.RequestNavigate("TestPackGridRegion", uri);

        }
then i got this ActivationException:


"Activation error occured while trying to get instance of type Object, key "TestPacksView"

i don't know if this is because View_B (TestPacksView) in different module or because the parameter
"SubsystemNo" is not received by the ViewModel_B hence View_B not constructed ...
Feb 15, 2013 at 6:37 AM
Hi isakavis
Thanks for your effort.

I used following code in ControllerA to navigate to View_B (TestPacksView) and send the parameter
SubsystemNo
 var uriQuery = new UriQuery();
            if (Subsys != null)
            {
                uriQuery.Add("SubsystemNo", Subsys.SubsystemNo);

            }

            var uri = new Uri("TestPacksView" + uriQuery.ToString(), UriKind.Relative);
           
            regionManager.RequestNavigate("TestPackGridRegion", uri);

        }
then i got this ActivationException:


"Activation error occured while trying to get instance of type Object, key "TestPacksView"

i don't know if this is because View_B (TestPacksView) in different module or because the parameter
"SubsystemNo" is not received by the ViewModel_B hence View_B not constructed ...
Feb 15, 2013 at 6:37 AM
Hi isakavis
Thanks for your effort.

I used following code in ControllerA to navigate to View_B (TestPacksView) and send the parameter
SubsystemNo
 var uriQuery = new UriQuery();
            if (Subsys != null)
            {
                uriQuery.Add("SubsystemNo", Subsys.SubsystemNo);

            }

            var uri = new Uri("TestPacksView" + uriQuery.ToString(), UriKind.Relative);
           
            regionManager.RequestNavigate("TestPackGridRegion", uri);

        }
then i got this ActivationException:


"Activation error occured while trying to get instance of type Object, key "TestPacksView"

i don't know if this is because View_B (TestPacksView) in different module or because the parameter
"SubsystemNo" is not received by the ViewModel_B hence View_B not constructed ...
Feb 15, 2013 at 6:37 AM
Hi isakavis
Thanks for your effort.

I used following code in ControllerA to navigate to View_B (TestPacksView) and send the parameter
SubsystemNo
 var uriQuery = new UriQuery();
            if (Subsys != null)
            {
                uriQuery.Add("SubsystemNo", Subsys.SubsystemNo);

            }

            var uri = new Uri("TestPacksView" + uriQuery.ToString(), UriKind.Relative);
           
            regionManager.RequestNavigate("TestPackGridRegion", uri);

        }
then i got this ActivationException:


"Activation error occured while trying to get instance of type Object, key "TestPacksView"

i don't know if this is because View_B (TestPacksView) in different module or because the parameter
"SubsystemNo" is not received by the ViewModel_B hence View_B not constructed ...
Feb 15, 2013 at 12:52 PM
Look at prism 4.1 samples UI Composition <Your Root Directory>\Projects\Prism\Quickstarts\View-Switching Navigation

var uriQuery = new UriQuery();
        var contact = this.contactsView.CurrentItem as Contact;
        if (contact != null && !string.IsNullOrEmpty(contact.EmailAddress))
        {
            uriQuery.Add(ToQueryItemName, contact.EmailAddress);
        }

        var uri = new Uri(ComposeEmailViewName + uriQuery.ToString(), UriKind.Relative);
        this.regionManager.RequestNavigate(RegionNames.MainContentRegion, uri);
Developer
Feb 15, 2013 at 1:06 PM
Edited Feb 15, 2013 at 1:07 PM
Hi,

Also, I believe that this problem could be related to how you are registering your view to the container.
Take into account that as mentioned in this section of the documentation:

_When the region navigation service creates a view, it requests a type of Object from the container with a name that matches the one supplied in the navigation URI. Various containers provide different registration mechanisms to support this. For instance, in Unity you will need to register your views against the Object type, map it to your view type, and provide a name that matches the one used in the navigation URI.
In MEF, it is implemented differently and only the contract name is used. Therefore, as long as the view is exported with a contract name that matches the name in the URI request, the view can be successfully constructed._

Hence if you are using MEF you should export your view with a contract name for example like this:
[Export("TestPacksView")]
public partial class TestPacksView: UserControl
In the case of Unity container will have to register the views against the Object type:
container.RegisterType<object,TestPacksView>("TestPacksView");
Regards,

Agustin Adami
http://blogs.southworks.net/aadami
Feb 15, 2013 at 1:22 PM
Agustin't comment is correct. Registering the view in the module as he mentioned is to be checked.
Feb 15, 2013 at 1:44 PM
Hi All,
Thanks for your help
I revised the Export attribute and it is ok.
I think the root cause of my problem comes from RequestNavigate that lie in ModuleA and
want to navigate to View_B in ModuleB and display it within View_A in ModuleA.
maybe i need to pass the full path of View_B to the uri object
Developer
Feb 15, 2013 at 4:24 PM
Hi,

As far as I know, using just the contract name string specified in the export attribute should be enough to navigate the view based on this string.

On the other hand, perhaps what you are experiencing is a timing issue, when loading your modules as if your ModuleA loads before your ModuleB the RequestNavigate method may be called before the View_B defined in ModuleB is exported to the corresponding container. If this is the case you will have to ensure that the modules are loaded in the correct order. For example you could achieve this by specifying module dependencies like mentioned in this section of the documentation:
Regards,

Agustin Adami
http://blogs.southworks.net/aadami
Feb 15, 2013 at 6:17 PM
Edited Feb 15, 2013 at 6:42 PM
Hi Agustin,
My scenario is triggered after the program already started an all modules configured, and it is triggred via event
aggregator handled in the controller.So i don't think it is timing problem.

*I tried different scenario: instead of letting ControllerA navigate to View_B, i let ControllerB via EA navigate to View_B
and display it in "TestPackGridRegion" within View_A, but regretfully i received the same exception???!!!

Best Regards.
Developer
Feb 18, 2013 at 5:01 PM
Hi,

It seems that we are unable to find the cause behind the exception with the information provided so far. If it's neither a timing issue nor a problem with the Export attribute, it would be helpful if you could isolate this problem in a repro-sample application that we could analyze in order to have a better understanding of this problem and help you find the cause behind it.

Thanks,

Damian Cherubini
http://blogs.southworks.net/dcherubini