Search code examples
silverlightsilverlight-4.0prism

How can a view cancel a navigation request in PRISM?


I have a view that implements the INavigationAware interface. This interface has the OnNavigationFrom method that is, according to MSDN http://msdn.microsoft.com/en-us/library/microsoft.practices.prism.regions.inavigationaware.onnavigatedfrom(v=pandp.40).aspx,

Called when the implementer is being navigated away from.

Now, I want to make sure that the user hasn't left any unsaved changes and if there are unsaved changes, ask the user if he wants to save them. At this moment I need to be able to cancel that navigation request somehow in case the user wants to stay and continue editing.

The documentation on the INavigationAware interface at MSDN doesn't say anything on how this interface is supposed to be used.

I might be awfully wrong and there is no way to cancel it or this interface isn't meant for that.

Anyways, I appreciate if somebody tell me how I can make the user stay and continue editing one navigation request has been initiated.


Solution

  • I discovered there is another interface that does the trick: "IConfirmNavigationRequest" which inherits from INavigationAware. It has the ConfirmNavigationRequest method that takes a callback with a boolean argument. If I want to cancel the navigation request I call that callback with false, If I want to stay I call it with true:

    continuationCallback(MessageBox.Show("Discard changes?", MessageBoxButton.OKCancel) == MessageBoxResult.Cancel)
    

    So the problem's solved. Thank you for your attention.