Simplifying Prism WPF Navigation – Synchronous Navigation Confirmation
Microsoft patterns & practices Prism 4 library and guidance gives WPF and Silverlight developers a very solid foundation for creating business applications. Adopting Prism patterns and library features guides the developer towards creating applications that: can evolve over time, are not tightly coupled, can be independently developed across a large team, are Blendable, and testable.
New to Prism 4 was the Navigation APIs. The use of the Navigation APIs greatly simplifies application development because the Navigation API takes over the responsibility of object creation and siting objects in the target region.
The Prism library is designed to support applications that can target WPF and Silverlight with good code reuse. While not all developers need this capability, a lot of thought and design decisions were made to fully support this scenario.
One API that took this capability into account while being designed is the Navigation
API; specifically the confirmation of a navigation request. Objects that implement IConfirmNavigationRequest have the option to veto a navigation request. In order to support the limitation that Silverlight does not allow blocking dialog boxes, the IConfirmNavigationRequest.ConfirmNavigationRequest
method had to be written so that Silverlight objects could participate in vetoing a navigation request without a blocking dialog.
Without going into all the details (you can read them here), objects that implement IConfirmNavigationRequest
are required to invoke the callback in the ConfirmNavigationRequest
method arguments. Objects are free to implement any vetoing code they want as long as the callback is invoked. In practice, the navigation request is halted until the callback is invoked. This design enables Silverlight developers to implement a variety UI solutions for prompting the user, for example the Prism Interaction Request.
I’ve found in my WPF development that I had to jump through the above hoops just to have navigation confirmation, when in fact WPF has out of the box support for modal, UI blocking dialogs. In an effort to simplify my WPF applications, I’ve created a replacement for the Prism RegionNavigationService
that allows for synchronous navigation confirmation.
The implementation is very straightforward and only requires that you add one class and one interface to the Prism library and recompile it.
The RegionNavigationSynchronousService
is the replacement for the stock RegionNavigationService
. This class uses the new synchronous (blocking) navigation confirmation interface IConfirmNavigationRequestSynchronous
, listed below:
public interface IConfirmNavigationRequestSynchronous : INavigationAware
{
/// <summary>
/// Determines whether this instance approves being navigated away from.
/// </summary>
/// <param name="navigationContext">The navigation context.</param>
Boolean ConfirmNavigationRequestSynchronous(NavigationContext navigationContext);
}
By default, Prism automatically registers the RegionNavigationService
in the container as part of the bootstrapping pipeline. However, we want to use the WPF friendly synchronous confirmation service, RegionNavigationSynchronousService
.
All that is required is to override the ConfigureContainer
method in your bootstrapper and register RegionNavigationSynchronousService
as I’ve done below:
namespace TestBench {
using System.Windows;
using Microsoft.Practices.Prism.Modularity;
using Microsoft.Practices.Prism.Regions;
using Microsoft.Practices.Prism.UnityExtensions;
using Microsoft.Practices.Unity;
using TestBench.Customers;
class Bootstrapper : UnityBootstrapper {
protected override IModuleCatalog CreateModuleCatalog() {
var catalog = new ModuleCatalog();
catalog.AddModule(typeof (CustomersModule));
return catalog;
}
protected override DependencyObject CreateShell() {
var shell = this.Container.Resolve<ShellView>();
Application.Current.MainWindow = shell;
Application.Current.MainWindow.Show();
return shell;
}
protected override void ConfigureContainer() {
base.ConfigureContainer();
// register the new navigation service that uses synchronous navigation
// confirmation instead of the async confirmation.
this.Container.RegisterType(typeof(IRegionNavigationService),
typeof(RegionNavigationSynchronousService));
}
}
}
Below is a very simple implementation.
PLEASE do not put
MessageBox
code in your view models! This is for demo purposes only and to keep the code simple. Please use a dialog service that abstracts the UI dialog away from the view model.
The below code is from the included download and is in the CustomerMaintenanceViewModel
. The below ConfirmNavigation
property allows the demo to confirm or not but is not part of the Navigation
API. This method returns true
or false
to continue the navigation request or not.
public Boolean ConfirmNavigationRequestSynchronous(NavigationContext navigationContext) {
if (this.ConfirmNavigation) {
if (MessageBox.Show("Close form and navigate?", "Confirm Navigation",
MessageBoxButton.OKCancel, MessageBoxImage.Question) == MessageBoxResult.OK) {
return true;
}
return false;
}
return true;
}
Demo Application
Very simple Prism application that demonstrates confirming navigation requests and region lifetime. The status bar indicates the views in the ContentRegion
on the right. As you open or close views, their name will be displayed; for the Customers, the customer number will be displayed.
Crack the code open, you’ll have this down in a few minutes.
Download
As always, don’t forget to “Unblock” the zip file after downloading from the Internet before you unzip it.
The download includes a sample Prism solution that you can run without modifying your Prism library code. I’ve included a \Lib folder in the solution with pre-built, modified Prism library code.
I’ve also included two files in the \PrismSource folder that you can add to your Prism library. Simply copy these two files into the below folder and recompile Prism. You now have the option to use a simpler navigation confirmation API in your WPF projects.
This only works in WPF and NOT Silverlight. If you have the requirement to share code between WPF and Silverlight, this will not work because Silverlight requires the navigation confirmation to be async.
Have a great day,
Just a grain of sand on the worlds beaches.
Filed under: C#, CodeProject, Navigation, Prism, WPF General

Post Comment
This is one awesome article. Keep writing.
This is a terrific article. You make sense with your views and I agree with you on many. Some information got me thinking. That as a sign of a great article.
Wow! This can be one particular of the most useful blogs We have ever arrive across on this subject. Actually Great. I am also a specialist in this topic so I can understand your effort.
Received the letter. I agree to exchange the articles.
This web site truly has all the info I needed concerning this subject and didn at know who to ask.
You are my inhalation , I possess few web logs and very sporadically run out from to brand
Real good information can be found on blog.
You have some really good posts and I believe I would be a good asset. If you ever want to take some of the load off, I ad really like
Looking forward to reading more. Great article.Much thanks again. Really Great.
You made some good points there. I did a search on the topic and found most people will go along with with your blog.
You have made some good points there. I checked on the net to learn more about the issue and found most people will go along with your views on this web site.
Just discovered this site thru Bing, what a pleasant shock!
I think this is a real great post.Really thank you! Cool.
Muchos Gracias for your blog article.Thanks Again. Great.
Write more, thats all I have to say. Literally, it seems as
of course we of course we need to know our family history so that we can share it to our kids a
I truly appreciate this blog post. Will read on...
Ridiculous story there. What happened after? Thanks!
Major thanks for the article post.Much thanks again. Much obliged.
Pity the other Pity the other Paul cannot study on him or her seriously.
Remarkable! Its actually remarkable post, I have got much clear idea on the topic of from this post.
Some really wonderful blog posts on this internet site , regards for contribution.
This is a topic that as close to my heart Best wishes! Exactly where are your contact details though?
Really informative blog article. Much obliged.
This very blog is really interesting additionally diverting. I have picked a bunch of handy stuff out of this source. I ad love to go back again soon. Thanks!
using? Can I get your affiliate link to your host? I wish my website
Very good article post.Really thank you! Fantastic.
Wow! This could be one particular of the most beneficial blogs We have ever arrive across on this subject. Basically Magnificent. I am also an expert in this topic so I can understand your effort.
Looking forward to reading more. Great blog.Really thank you! Really Great.
Many thanks for sharing this good write-up. Very inspiring! (as always, btw)
I think this is a real great article. Awesome.
Thanks for the article.Thanks Again. Much obliged.
Thanks-a-mundo for the blog post.Really looking forward to read more. Great. sex photos
You made some decent points there. I did a search on the issue and found most persons will approve with your website.
Wow! This can be one particular of the most useful blogs We ave ever arrive across on this subject. Actually Magnificent. I am also an expert in this topic therefore I can understand your hard work.
This website was how do I say it? Relevant!! Finally I have found something which helped me. Many thanks!
I will right away grab your rss as I can at find your e-mail subscription link or e-newsletter service. Do you ave any? Kindly let me know in order that I could subscribe. Thanks.
safe power leveling and gold I feel extremely lucky to have come across your entire web pages and look forward to plenty of more exciting minutes reading here
I recommend to you to visit a site on which there are many articles on this question.
Im obliged for the article post.Really looking forward to read more.
Regards for all your efforts that you have put in this. Very interesting information.
Thanks for sharing, this is a fantastic article.Really thank you! Want more.
Very informative blog article.Really looking forward to read more. Really Cool.
Studying this write-up the donate of your time
we prefer to honor numerous other online sites on the internet, even though they aren
Just wanna state that this is very helpful , Thanks for taking your time to write this.
Thanks for sharing, this is a fantastic post. Cool.
Pretty! This was a really wonderful article. Thanks for supplying these details.|
Wow, superb weblog format! How lengthy have you been blogging for? you made running a blog glance easy. The overall glance of your website is fantastic, let alone the content material!
This website was how do I say it? Relevant!! Finally I ave found something which helped me. Thanks!