INavigationAware not being called

Topics: Prism v4 - Silverlight 4
Mar 15, 2012 at 3:37 PM

Hi All

I'm having a problem in one of my apps navigating to the correct view, I've set a breakpoint on IsNavigationTarget and its not even being hit.  I've attached a repro and would appreciate if someone could have a look see if you could find where I'm going wrong.

Basically I have a shell that will load buttons related to modules in the app.  When the user clicks a button the main view of that module should be loaded into the 'MainRegion'.  The repro has 2 modules 'ModuleA' and 'ModuleZ'.  When the button is clicked the the view is loaded but the breakpoint is not hit so INavigationAware interface is not recoginized?

Repro can be found here!138&parid=3CD5BDC4E5B0165A!128



Mar 15, 2012 at 6:29 PM

Hi Paul,

We analyzed your repro-sample application and found that you are setting the view models as the DataContext of the Grids inside the views.

For a view model to participate in the navigation process of its view, it must be set as the DataContext of the view itself (in this case, the DataContext of the UserControls) rather that as the DataContext of its child elements.

In this case, as the DataContext property of the view does not contain its view model, Prism cannot find it and the IsNavigationTarget method is never invoked.

For example, you can set the view model of your views doing something like this:

<UserControl x:Class="ModuleZ.Views.ViewZ"
    <Grid x:Name="LayoutRoot" Background="White" HorizontalAlignment="Center" VerticalAlignment="Center">
        [ . . . ]

You can check that, after applying the aforementioned changes, the IsNavigationTarget is now being properly invoked (remember to clean your solution beforehand.)

You can find more information about this in the following sections of the Prism documentation:

I hope you find this useful,

Damian Cherubini

Mar 20, 2012 at 11:30 PM

Thanks Damian for the detailed answer, overlooked the datacontext on the usercontrol.  All works fine now.