RequestNavigate swallowing exceptions and won't call callback

Topics: Prism v4 - WPF 4
Aug 2, 2011 at 8:20 PM

Hello,

I am trying to build a proof-of-concept with WPF 4, Prism 4 + MEF.  The concept I am trying to prove out is to build out a 'dumb shell' that does nothing but hosts two navigation regions and has a ViewModel that contains a collection of modules.  The shell contains two regions, "MainContentRegion" and "MainNavigationRegion".  below the navigation region is a list box that uses an item template to display a series of hyperlinks, one for each module loaded by MEF.  The goal is to have MEF load the modules which would then show a hyperlink in the list box for each module.  When the user clicks the link, the module would load the appropriate navigation view into the MainNavigationRegion.  Each navigation view would have a set of hyperlinks which would in turn load the appropriate view into the MainContentRegion.

Pretty basic stuff.

Anyway, this all works like a champ except for this: when you click a hyperlink in the list box the second time the RequestNavigate call simply goes off into the ether.  NOTE: The first time works perfectly.  The second time navigation does not occur, but neither does the call throw an exception, and if I pass in a callback to check the NavigationResult, the callback is never called.  It's like Prism is swallowing an exception somewhere in the API.

Each module class implements this interface:

    public interface IRQOneModule
    {
        string Title { get; set; }
        string NavigationPaneViewName { get; set; }

        void RegisterViews();

        Type NavigationViewType { get; set; }


    }
Here's a sample of the code-behind for one of the navigation views in a module:
    [Export("OrderEntryNavigationView")]
    public partial class OrderEntryNavigationView : UserControl
    {
        public OrderEntryNavigationView()
        {
            InitializeComponent();
        }

        [Import]
        public OrderEntryNavigationViewModel ViewModel
        {
            set { this.DataContext = value; }
        }

    }
Here's the method from the ViewModel that attempts to call RequestNavigate:
        private void Navigate()
        {
            try
            {
                _regionManager.RequestNavigate(RegionNames.MainNavigationRegion, new Uri(_module.NavigationPaneViewName, UriKind.Relative));
            }
            catch (Exception e)
            {
                string message = e.Message;
            }
        }
Again, when you start the app and click the first hyperlink in the list box, everything works.  I have two modules currently and either one will work if you click it first.  
It's when you click the second one that that the RequestNavigate call just evaporates into the ether.
Any help is appreciated.
Dan

Developer
Aug 3, 2011 at 2:33 PM

Hi Dan,

Currently in Prism, when an error occurs with a navigation request, the error is handled and thus not shown as an unhandled exception.

As for the problem you're experiencing, you could try decorating your views with the PartCreationPolicy attribute set to CreationPolicy.NonShared, so that they are not exported as singleton instances; that might solve your problem. If that doesn't help you fix this undesired behavior, you could debug the call to the RequestNavigate method to find the exact exception that is causing this. Note that you must place a reference to the Prism Library projects instead of the compiled binaries in order to be able to debug that method call.

I hope you find this helpful.

Guido Leandro Maliandi
http://blogs.southworks.net/gmaliandi

Aug 3, 2011 at 7:00 PM

Thanks for the suggestions Guido.  I ended up debugging through the prism code and the root cause was my fault.  I had created a NavigationAwareViewModelBase class that implements IConfirmNavigationRequest but had an empty implementation for ConfirmNavigationRequest.  Thus, the prism framwork was dutifully calling ConfirmNavigationRequest but my code wasn't doing anything with the continuationCallback.  Thus, it just canceled the navigation like it was supposed to.

I don't think I would have found that without debugging through the RequestNavigate call.

Thanks a million!

Dan


Developer
Oct 17, 2011 at 1:25 PM

Dan,

You might find the following blog post useful, which contains more information about this:

How to prevent Region Navigation from hiding exceptions in Prism

I hope you find this helpful.

Guido Leandro Maliandi
http://blogs.southworks.net/gmaliandi