locked
Prism - unable to use requestNavigate RRS feed

  • Question

  • I am unable to  display a View using requestNavigate of prism. I have been slaving myself trying to find out what I am missing; Your help will be greatly appreciated. Thank you in advance. see errors below

    Error Messages

    An exception of type 'Microsoft.Practices.ServiceLocation.ActivationException' occurred in Microsoft.Practices.ServiceLocation.dll but was not handled in user code

    Additional information: Activation error occurred while trying to get instance of type Object, key "EnrollView"

    The InnerException.Message gives me: 

    An unhandled exception of type 'System.NullReferenceException' occurred in mscorlib.dll
    Additional information: Object reference not set to an instance of an object.

    II - CODE

    1 - ModuleInit (ModuleInit is added into the modulecatalog in the bootstrapper(see at the end of the page)) 

    Public Class ModuleInit
        Implements IModule

        Private ReadOnly _regionManager As IRegionManager
        Private ReadOnly _container As IUnityContainer
        Private ReadOnly _regionNames As RegionNames
        Private _viewcontroller As ViewController

        Public Sub New(regionManager As IRegionManager, container As IUnityContainer)
            Me._regionManager = regionManager
            Me._container = container
            Me._regionNames = New RegionNames()

            With _container
                .RegisterType(Of Object, EnrollView)("EnrollView")
            End With

        End Sub

        Public Sub Initialize() Implements IModule.Initialize
            RegisterViewModule()
        End Sub

        Private Sub RegisterViewModule()
            _regionManager.RegisterViewWithRegion(_regionNames.CommandRegion, Function() _container.Resolve(Of CommandView)())
        End Sub
    End Class

    2- the CommandViewModel (Has a command called Enrollcommand; when the user clicks on it should display the EnrollView) 

    Public Class CommandViewModel
        Inherits NotificationChange
        Implements INavigationAware, IConfirmNavigationRequest, IRegionMemberLifetime

        Private _enrollcommand As ICommand

        Private ReadOnly _eventaggregator As IEventAggregator
        Private ReadOnly _container As IUnityContainer
        Private ReadOnly _regionManager As IRegionManager
        Private ReadOnly _regionNames As RegionNames

    Public Sub New(eventaggregator As IEventAggregator, container As IUnityContainer, _
                       regionManager As IRegionManager, RegionNames As RegionNames)

            If (IsNothing(eventaggregator)) Then Throw New ArgumentNullException("eventAggregator")
            Me._eventaggregator = eventaggregator
            Me._container = container
            Me._regionManager = regionManager
            Me._regionNames = RegionNames

            _enrollcommand = New DelegateCommand(AddressOf cmdenroll)

    End sub

     Property EnrollCommand As ICommand
            Get
                Return _enrollcommand
            End Get
            Set(value As ICommand)
                _enrollcommand = value
                NotifyPropertyChanged("EnrollCommand")
            End Set
        End Property

     Private Sub cmdenroll()

           Dim viewname As Uri = New Uri("EnrollView", UriKind.Relative)

            Me._eventaggregator.GetEvent(Of ButtonSelectedEvent).Publish("Enroll")
            Me._regionManager.RequestNavigate(_regionNames.MainRegion, viewname, AddressOf NavigationCompleted)
      End Sub

     Private Sub NavigationCompleted(result As NavigationResult)

            If result.Result.Equals(False) Then
                Throw New Exception(result.Error.InnerException.Message)
            End If

        End Sub

    3 - Bootstrapper

    Public Class bootstrapper
        Inherits UnityBootstrapper

        Protected Overrides Sub InitializeShell()
            Application.Current.MainWindow = Shell
            Application.Current.MainWindow.Show()
        End Sub

        Protected Overrides Function CreateShell() As DependencyObject
            Return Container.Resolve(GetType(Shell), "Shell")
        End Function

        Protected Overrides Sub ConfigureContainer()
            MyBase.ConfigureContainer()

            Dim moduleCatalog As ModuleCatalog = DirectCast(Me.ModuleCatalog, ModuleCatalog)
            moduleCatalog.AddModule(GetType(ModuleInit))

        End Sub

    End Class

    4 - Shell or main Window - XAML code

    <Window x:Class="Shell"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:prism="http://www.codeplex.com/prism"
        Title="Shell" Height="300" Width="300">
        <DockPanel>
           <ContentControl 
                DockPanel.Dock="Left"
                prism:RegionManager.RegionName="CommandRegion"
                Width="150"/>
            <ContentControl 
                DockPanel.Dock="Right"
                prism:RegionManager.RegionName="MainRegion"/>
        </DockPanel>
    </Window>

     

    AA

    Wednesday, September 10, 2014 1:13 PM

Answers

  • Where is the EnrollView defined?

    If it is defined in the Bootstrapper project, you should register it directly in the ConfigureContainer method of the Bootstrapper class:

    Imports Microsoft.Practices.Prism.UnityExtensions
    ...
    
        Protected Overrides Sub ConfigureContainer()
            MyBase.ConfigureContainer()
    
    
            Container.RegisterType(Of EnrollView)("EnrollView")
        End Sub

    If it is defined in a separate module project, you should register it in the Initialize() method of the Module class:

        Public Sub Initialize() Implements IModule.Initialize
            _container.RegisterType(Of EnrollView)("EnrollView")
            RegisterViewModule()
        End Sub


    ...and you must also add the module to the ModuleCatalog by overriding the ConfigureModuleCatalog() method in the Bootstrapper class and add a reference to the module project from the Bootstrapper project:

        Protected Overrides Sub ConfigureModuleCatalog()
            MyBase.ConfigureModuleCatalog()
    
            Dim type As Type = GetType(ModuleInit)
            Dim mi As New ModuleInfo()
            mi.ModuleName = type.Name
            mi.ModuleType = type.AssemblyQualifiedName
    
            Me.ModuleCatalog.AddModule(mi)
    
        End Sub


    Please remember to mark helpful posts as answer and/or helpful.

     


    • Marked as answer by Ailiam Wednesday, September 10, 2014 4:07 PM
    • Edited by Magnus (MM8)MVP Wednesday, September 10, 2014 9:31 PM
    Wednesday, September 10, 2014 3:04 PM

All replies

  • I'm afraid I find the VB a bit distracting...

    But

    I don't see any resolve for EnrollView.

    It's something quick and easy to try anyhow.

    Stick in the equivalent of:

    IRegionManager regionManager = ...;
    IRegion mainRegion = regionManager.Regions["MainRegion"];
    InboxView view = this.container.Resolve<InboxView>();
    mainRegion.Add(view);

    or

    IRegionManager regionManager = ...;
    regionManager.RegisterViewWithRegion("MainRegion", () =>
                       container.Resolve<InboxView>());


    Hope that helps
    Please don't forget to up vote answers you like or which help you and mark one(s) which answer your question.

    Wednesday, September 10, 2014 1:28 PM
  • Where is the EnrollView defined?

    If it is defined in the Bootstrapper project, you should register it directly in the ConfigureContainer method of the Bootstrapper class:

    Imports Microsoft.Practices.Prism.UnityExtensions
    ...
    
        Protected Overrides Sub ConfigureContainer()
            MyBase.ConfigureContainer()
    
    
            Container.RegisterType(Of EnrollView)("EnrollView")
        End Sub

    If it is defined in a separate module project, you should register it in the Initialize() method of the Module class:

        Public Sub Initialize() Implements IModule.Initialize
            _container.RegisterType(Of EnrollView)("EnrollView")
            RegisterViewModule()
        End Sub


    ...and you must also add the module to the ModuleCatalog by overriding the ConfigureModuleCatalog() method in the Bootstrapper class and add a reference to the module project from the Bootstrapper project:

        Protected Overrides Sub ConfigureModuleCatalog()
            MyBase.ConfigureModuleCatalog()
    
            Dim type As Type = GetType(ModuleInit)
            Dim mi As New ModuleInfo()
            mi.ModuleName = type.Name
            mi.ModuleType = type.AssemblyQualifiedName
    
            Me.ModuleCatalog.AddModule(mi)
    
        End Sub


    Please remember to mark helpful posts as answer and/or helpful.

     


    • Marked as answer by Ailiam Wednesday, September 10, 2014 4:07 PM
    • Edited by Magnus (MM8)MVP Wednesday, September 10, 2014 9:31 PM
    Wednesday, September 10, 2014 3:04 PM
  • Thank you Magnus for your feedback. I made the changes as suggested; this time it gives me System.Object into the display Region where the EnrollView should be displayed or visible for the user.   

    AA

    Wednesday, September 10, 2014 3:39 PM
  • How do you define the EnrollView, the view where you want to display it and how do you use RequestNavigate method then?

    Anyway, this is a new question that should be asked in a new thread. Your issue about the exception and how to set up your Bootstrapper and module has been solved. Please remember to mark any helpful post as answer to close the thread and start a new one if you have additional questions.

    Wednesday, September 10, 2014 3:42 PM
  • Thank you Andy...  But I have no problem Registering a View against a region. My issue is ( and even some C# programmers encountered the same issue) is how to display a view using requestNavigate or taking advantage of IregionManager.resquestNavigate

    But Thank you for you input


    AA

    Wednesday, September 10, 2014 3:43 PM