I am developing a WP 8.1 XAML app. The first page is a login page, the second for example a history page. There will be more pages, but it doesn\'t matter at the moment. Whe
Consider hard if this is what you really want to do. A more user friendly flow would be to save the data and let the user back out of the app and return. Also consider what happens if the user exits in a different way such as the window or camera button.
That said, the code you list will work if that's the only navigation handling that you have (try it in the BlankPage template). I suspect that it's not the only and you're using something like the NavigationHelper from the fuller templates such as BasicPage. In that case the NavigationHelper's GoBackCommand is probably triggering and calling Frame.GoBack.
You can override this behaviour by setting your own RelayCommand on the NavigationHelper's GoBackCommand property:
RelayCommand _checkedGoBackCommand;
public BasicPage1()
{
this.InitializeComponent();
this.navigationHelper = new NavigationHelper(this);
this.navigationHelper.LoadState += this.NavigationHelper_LoadState;
this.navigationHelper.SaveState += this.NavigationHelper_SaveState;
_checkedGoBackCommand = new RelayCommand(
() => this.CheckGoBack(),
() => this.CanCheckGoBack()
);
navigationHelper.GoBackCommand = _checkedGoBackCommand;
}
private bool CanCheckGoBack()
{
return true;
}
private async void CheckGoBack()
{
Debug.WriteLine("CheckGoBack");
MessageDialog dlg = new MessageDialog("Are you sure you want to quit you will loose all your work ?", "Warning");
dlg.Commands.Add(new UICommand("Yes", new UICommandInvokedHandler(CommandHandler1)));
dlg.Commands.Add(new UICommand("No", new UICommandInvokedHandler(CommandHandler1)));
await dlg.ShowAsync();
}
private void CommandHandler1(IUICommand command)
{
var label = command.Label;
switch (label)
{
case "Yes":
{
Application.Current.Exit();
break;
}
case "No":
{
break;
}
}
}