1
Vote

IAsyncNavigationAware: Asynchronous region navigation with continuations

description

The region navigation mechanism of Prism is a very powerful and flexible mechanism for navigating between views in a region.
 
Unfortunately, the region navigation mechanism of Prism only supports synchronous navigation between views. Sometimes however,it is necessary that some asynchronous code is executed during the navigation from one view to another. An example is a wizard where between two wizard steps an asynchronous web service call has to be performed to validate input or to process some data.
 
To overcome this limitation, I've created the IAsyncNavigationAware interface and corresponding ASyncRegionNavigationService. Similar to Prism's IConfirmNavigationRequest interface (which does execute asynchronously), the interface uses continuations to proceed the navigation process:
 
public interface IAsyncNavigationAware
{
void NavigateTo(NavigationContext navigationContext, Action continuationCallback);
bool IsNavigationTarget(NavigationContext navigationContext);
void NavigateFrom(NavigationContext navigationContext, Action continuationCallback);
}
 
As you can see, this interface uses continuations for the two navigation actions. This way, asynchronous code can be executed when navigating to or navigating from a view/viewmodel and when this code completes, calling continuationCallback() will continue the navigation process.
 
I've also created the IConfirmAsyncNavigationRequest interface which is similar to IConfirmNavigationRequest but now derives from IAsyncNavigationAware.
 
I've also created the AsyncNavigationAwareBehavior region behavior which takes care of attaching the RegionAsyncNavigationService to your regions.
 
I've attached a zip file with the source code.
 
Kind regards,
 
Merijn

file attachments

comments

aadami wrote Aug 17, 2012 at 5:04 PM

Hi,

First of all, thanks for sharing your findings with the rest of the community as it could be helpful for others pursuing this kind of scenarios.

I have run some tests with your implementation and it seems an useful approach for adding asynchronous calls functionality to the navigation mechanism.
I believe, that something that should be considered when using this approach, is that when using the IAsyncNavigationAware the IsNavigationTarget property may be ignored, this is because the RegionNavigationContentLoader filters the existing view candidates to perform the navigation in the region depending on the state of the IsNavigationTarget property, based on the default INavigationAware interface, hence in order to support this a new implementation of the RegionNavigationContentLoader that uses the new IAsyncNavigationAware interface should be implemented.

Also, as an alternative for applying your custom implementation of the RegionNavigationService to all the regions instead of using Region Behaviors, you could replace the default Prism Library type, by registering your custom implementation of the RegionNavigationService class into the container through the IRegionNavigationService interface, for example when using Unity you could replace the type during the bootstrapping sequence like this:

protected override void ConfigureContainer()
    {
        RegisterTypeIfMissing(typeof(IRegionNavigationService), typeof(RegionAsyncNavigationService), false);
        base.ConfigureContainer();
    }
You could find more information about this in the following section of the Prism documentation:

Appendix E: Extending Prism, Replacing Default Prism Library Types (http://msdn.microsoft.com/en-us/library/gg430866(v=pandp.40)#sec12)

We are leaving this work item open so the team can analyze you suggestion for future releases.

Thanks!

Agustin Adami
http://blogs.southworks.net/aadami