OnNavigatedTo doesn't fire on the first navigation to

Topics: Prism v4 - Silverlight 4
May 4, 2011 at 5:53 PM

I've got a DelegateCommand wired to a button in a View.  The command executes the RequestNavigate method to load a View into another Region:

public class ModuleAViewTwoTaskButtonViewModel
{ 
    public ModuleAViewTwoTaskButtonViewModel()
    {
        this.ShowModuleAViewTwo = new DelegateCommand(() =>
        {
            // Initialize
            var regionManager = ServiceLocator.Current.GetInstance<IRegionManager>();
 
            // Show Workspace
            regionManager.RequestNavigate("WorkspaceRegion""ModuleAViewTwo");
 
        });
    }
 
    public DelegateCommand ShowModuleAViewTwo { getset; }
}

The ModuleAViewTwo view has a ViewModel that implements the INavigationAware interface.

The above code all works well and loads the View as expected, except that the first time the ModuleAViewTwo View is loaded, the OnNavigateTo method in the Views ViewModel class does not run.  If I click the button again, which causes the command to run again, thus executing the RequestNavigate method again, the OnNavigatedTo method runs.

Any ideas why its not executing the first time the View is navigated to?

Devin

May 4, 2011 at 6:27 PM

Hi Devin,

It will be helpful if you could provide us with a full repro sample of your solution, so that we can help you to identify the cause of this beahvior you are mentioning.

You could upload your solution to SkyDrive and post the link here.

Thanks,

Miguel Bronzovic
http://blogs.southworks.net/mbronzovic

May 4, 2011 at 7:20 PM

OK, nevermind.  I knew I should have created the sample first :)

I was connecting my VMs to my Views wrong, which was causing the problem.  I did it the right way in my sample and everything worked.  Changed it in my real project and everything worked as well.

Devin

 

May 30, 2014 at 10:38 AM
Edited May 30, 2014 at 10:39 AM
Hi Devin.

Not sure if you will get this so many years after, but I try:

I have the exact same problem as you described:
I have two view that will be shown in the same region. A button on each view will navigate to the other view (and it will be replaced in the region).
The first time i do this operation, the OnNavigatedTo will not get fired (but view is shown). If I then in this view navigate back to the original view, the OnNavigatedTo in that ViewModel will fire. And if I then try the first navigation again, that too will fire the OnNavigatedTo.

Could you please show me what you did wrong first and how you fixed it?
(I should add that my code is in WPF).
(I know this is a long time ago, so I understand if you don't have that code..)

Gert
May 30, 2014 at 6:50 PM
Hi ghauan,

If you could provide us with a full repro sample of your solution, we can help you identify the issue and be able to troubleshoot it.

You could upload your solution to OneDrive for example and post the link here.

Regards.
Gabriel Ostrowsky
https://blogs.southworks.net/gostrowsky
May 30, 2014 at 9:42 PM
Hi Gabriel.

Thx for your reply. What would such a full repro sample need to include?
The solution I am working on (inherited) is kind of big, and I am not sure how to wire it all together just like that in a sample solution..

Regards
Gert
May 30, 2014 at 10:39 PM
Hello again.

It seems that I have found a solution, like Devin.

My problems also seem to have with the way the viewmodel was connected to the View.
Originally that connection was made first after the view was loaded, like this (from the code-behind-file):
public partial class BillValidationInvoiceControlView : UserControl
    {
        public BillValidationInvoiceControlView(BillValidationInvoiceControlViewModel model)
        {
            InitializeComponent();

            Loaded += (s, e) =>
                {
                    DataContext = model;
                };           
        }
    }
When I changed it to the following, the OnNavigatedTo will fire also the first time:
 public partial class BillValidationInvoiceControlView : UserControl
    {
        public BillValidationInvoiceControlView(BillValidationInvoiceControlViewModel model)
        {
            InitializeComponent();

            DataContext = model;            
        }
    }
This also seems to have solved some RunTimeBinderExceptions that I have been getting.

Thx anyway.

Regards
Gert