How to cancel navigation in a UWP Template 10 app? We are trying to cancel navigation where there is unsaved data (IsDirty
) in the ViewModel.
public class ViewModel : ViewModelBase
{
public override async Task OnNavigatingFromAsync(NavigatingEventArgs args)
{
if (this.IsDirty)
{
ContentDialog errorDialog = new ContentDialog();
errorDialog.Title = "Save?";
errorDialog.Content = "If you continue you will lose unsaved changes";
errorDialog.PrimaryButtonText = "Cancel";
errorDialog.SecondaryButtonText = "Continue";
ContentDialogResult answer = await errorDialog.ShowAsync();
if (answer == ContentDialogResult.Primary)
args.Cancel = true;
}
await Task.CompletedTask;
}
However navigation is not cancelled. Navigation can be cancelled if we omit the dialog. How do cancel navigation away from the ViewModel in response to a dialog?
Someone had the same issue than you. https://github.com/Windows-XAML/Template10/issues/1307
It's resolved in the 1.1.13 (preview) version. So you need to update the nuget package.