问题
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.
回答1:
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.
回答2:
Have a look at this link: http://www.silverlightshow.net/items/Working-with-Prism-4-Part-4-Region-Navigation.aspx
As an alternative, if you're using the RegionManager, maybe you can try putting some boolean logic around the RegionManager.RequestNavigate() method. Something like this:
if(isDirty)
{
RegionManager.RequestNavigate(RegionConstants.ShellMainRegion, new Uri(typeof(YourView).Name, UriKind.Relative));
}
else
{
// Don't navigate
}
One final thought. If you're using the EventAggregator, then you can publish a navigation event and respond accordingly.
来源:https://stackoverflow.com/questions/13494981/how-can-a-view-cancel-a-navigation-request-in-prism