Answered by:
how to block the navigation with alert?

Question
-
Hi!
in a special page, in particular situation, it is usefull ask to the user if he agrees to exit from this page.
how to block the navigation with alert?
i think that the right place is OnNavigatingFrom...
Dario
Tuesday, September 25, 2012 12:24 PM
Answers
-
a solution
bool _navigabile = false; protected async override void OnNavigatingFrom(NavigatingCancelEventArgs e) { base.OnNavigatingFrom(e); if (!_navigabile) { e.Cancel = true; var dialog = new MessageDialog("Do you really want to navigate away from this page?", "Please Confirm"); dialog.Commands.Add(new UICommand("OK", null, 0)); dialog.Commands.Add(new UICommand("Cancel", null, 1)); dialog.CancelCommandIndex = 1; dialog.DefaultCommandIndex = 0; var result = await dialog.ShowAsync(); if (Convert.ToInt16(result.Id) != 1) { _navigabile = true; this.Frame.Navigate(e.SourcePageType); } } }
- Marked as answer by Chida Nirio Tuesday, September 25, 2012 8:29 PM
Tuesday, September 25, 2012 8:29 PM
All replies
-
The documentation states:
// Summary: // Invoked immediately after the Page is unloaded and is no longer the current // source of a parent Frame. // // Parameters: // e: // Event data that can be examined by overriding code. The event data is representative // of the navigation that has unloaded the current Page. protected virtual void OnNavigatedFrom(NavigationEventArgs e);
-
Hence that position looks a little too late in the processing. What I'd do:
- e.g. in the Grid App template, break with the debugger in the LayoutAwarePage.OnNavigateFrom(...) override
- find out where you are called from (itemView_ItemClick / GoBack button / other): examine the Call Stack for place of invocation
- place your user interaction there, just before invoking Frame.Navigate(...)Tuesday, September 25, 2012 12:52 PM -
@forinfo: I think there is a misunderstanding, chida is referring to OnNavigatingFrom.
@chida: you are right about OnNavigatingFrom. You can simply override this method. and you are looking for something like:
protected override void OnNavigatingFrom(NavigatingCancelEventArgs e) { var dialog = new MessageDialog("Do you really want to navigate away from this page?", "Please Confirm"); dialog.Commands.Add(new UICommand("OK", null, 0)); dialog.Commands.Add(new UICommand("Cancel", null, 1)); dialog.CancelCommandIndex = 1; dialog.DefaultCommandIndex = 0; var result = dialog.ShowAsync(); if (result.Id == 1) { e.Cancel = true; } else { base.OnNavigatingFrom(e); } }
Can Bilgin
Blog CompuSight- Marked as answer by Chida Nirio Tuesday, September 25, 2012 1:11 PM
- Unmarked as answer by Chida Nirio Tuesday, September 25, 2012 8:16 PM
- Proposed as answer by Can Bilgin Wednesday, September 26, 2012 4:50 AM
- Edited by Can Bilgin Wednesday, September 26, 2012 9:46 AM
Tuesday, September 25, 2012 1:02 PM -
thank you very muchTuesday, September 25, 2012 1:11 PM
-
Of course .. misreadTuesday, September 25, 2012 1:20 PM
-
don't work, there is a problem with e.cancel = false :( and first e.Cancel = true
protected async override void OnNavigatingFrom(NavigatingCancelEventArgs e) { base.OnNavigatingFrom(e); e.Cancel = true; var dialog = new MessageDialog("Do you really want to navigate away from this page?", "Please Confirm"); dialog.Commands.Add(new UICommand("OK", null, 0)); dialog.Commands.Add(new UICommand("Cancel", null, 1)); dialog.CancelCommandIndex = 1; dialog.DefaultCommandIndex = 0; var result = await dialog.ShowAsync(); if ( Convert.ToInt16(result.Id) == 1) { e.Cancel = true; } else { base.OnNavigatingFrom(e); e.Cancel = false; } }
Tuesday, September 25, 2012 8:20 PM -
a solution
bool _navigabile = false; protected async override void OnNavigatingFrom(NavigatingCancelEventArgs e) { base.OnNavigatingFrom(e); if (!_navigabile) { e.Cancel = true; var dialog = new MessageDialog("Do you really want to navigate away from this page?", "Please Confirm"); dialog.Commands.Add(new UICommand("OK", null, 0)); dialog.Commands.Add(new UICommand("Cancel", null, 1)); dialog.CancelCommandIndex = 1; dialog.DefaultCommandIndex = 0; var result = await dialog.ShowAsync(); if (Convert.ToInt16(result.Id) != 1) { _navigabile = true; this.Frame.Navigate(e.SourcePageType); } } }
- Marked as answer by Chida Nirio Tuesday, September 25, 2012 8:29 PM
Tuesday, September 25, 2012 8:29 PM