• How to prevent Region Navigation from hiding exceptions in Prism

    Published by on August 19th, 2011 10:05 am under Prism, Silverlight, WPF

    No Comments

    There’s a behavior in Prism Region Navigation which might cause some confusion for those that aren’t used to it: a navigation request that leads to an error doesn’t throw an exception, but rather the navigation request stops and the exception apparently vanishes.

    This is because, instead of throwing an exception, the RegionNavigationService exposes a NavigationFailed event which is raised in case an error occurs when attempting to navigate to a view in a region. This can be evidenced by this fragment of the ExecuteNavigate method, which is called inside the NavigationService when a navigation operation is requested:

     private void ExecuteNavigation(
    NavigationContext navigationContext, object[] activeViews,
     Action<NavigationResult> navigationCallback)
            {
                try
                {
                    NotifyActiveViewsNavigatingFrom(navigationContext, activeViews);
    
                    object view = this.regionNavigationContentLoader.LoadContent(this.Region, navigationContext);
    
                    // Raise the navigating event just before activing the view.
                    this.RaiseNavigating(navigationContext);
    
                    this.Region.Activate(view);
    
                    (...)            
            }
                catch (Exception e)
                {
                    this.NotifyNavigationFailed(navigationContext, navigationCallback, e);
                }
            }

    Therefore, a possible way to avoid this from happening is to subscribe to the NavigationFailed event in the corresponding NavigationService instance, and perform the desired operations in response to that error in the handler for that event. The NavigationService instance can be found as a property of the Region involved in the navigation request.

    Additionally, in case this behavior isn’t comfortable for your scenario, you can modify the Prism Library to make it actually throw an exception, in the catch block inside the aforementioned method for example.

    You can find more information about Prism Region Navigation in this post from Karl Shifflett:

    Prism v4 Region Navigation Pipeline

    Tags: ,