0
votes

I am struggling with passing navigation parameters between 2 views in a WPF PRISM application.

The basic structure of my application is as follows: I have a viewmodels folder, inside that I have a folder for every "section" of my application simply to help structure things logically ie. Orders, Projects

The same applies for Views.

I am navigating from a view in the orders folder to one in the Projects folder.

The actual navigation is working and the application hits a breakpoint in my constructor for the second viewmodel, however the INavigationAware interface methods are never called.

I am not sure if this has something to do with the views/viewmodels existing in different namespaces due to the folders?

Here is the important code snippets I use:

Bootstrapper

 Container.RegisterType(typeof(Object), typeof(ConfirmOrders), "Orders.ConfirmOrders");
 Container.RegisterType(typeof(Object), typeof(QuotedCosts), "Projects.QuotedCosts");

Navigation To QuotedCosts from ConfirmOrders

 NavigationParameters par = new NavigationParameters();
 par.Add("ProjectID", ProjID);
 _regionManager.RequestNavigate("ContentRegion", "Projects.QuotedCosts", par);

QuotedCosts viewmodel whos Navigation Events never get called

 public class QuotedCostsViewModel : ViewModelBase, INavigationAware
    {
        private IProjectService _projectService;

        private int ProjectID { get; set; }
        public mProject CurrProject { get; protected set; }

        public QuotedCostsViewModel(IProjectService projectService)
        {
            _projectService = projectService;
            CurrProject = _projectService.GetProjectDetails(ProjectID);
        }

        public void OnNavigatedTo(NavigationContext navigationContext)
        {
            ProjectID = int.Parse(navigationContext.Parameters["ProjectID"].ToString());
        }

        public bool IsNavigationTarget(NavigationContext navigationContext)
        {
            return true;
        }

        public void OnNavigatedFrom(NavigationContext navigationContext)
        {

        }

    }
1
Could you please post code from where you are sending the parameters i.e Uri Query you are using..stylishCoder
It is in the second code block, I am not using the Uri Query and rather using the navigation parameters overloadThatChris
Code looks ok @ThatChris need to identify by replacing namespaces ..otherwise its a hit & trial issuestylishCoder
Thanks for checking I just figured it out. . .didn't realise that the OnNavigatedTo event is called after the constructor. Which makes perfect logical sense if you think about it.ThatChris

1 Answers

0
votes

So i just realised that the problem was due to my misunderstanding of the order that the different methods in the viewmodel was being called.

I was setting a parameter in the NavigatedTo method that was to be used in the constructor to populate my viewmodel, not realising that the constructor is called before the event.

Moving the offending code out of the constructor and into OnNavigatedTo has solved the problem