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. When I press the login button on the login page, the second (history) page appears. If I press the (physical) back button on the history page, it goes back to the login page. But I would like to disable this function. Instead of this I would like when I press the back button a MessageDialog appears and if I press again within a few seconds (for example 3 secs) the app terminates. I tried to find the answer for my questions, but it doesn't work properly. I tried this on the login page:
protected override void OnNavigatedTo(NavigationEventArgs e)
{
HardwareButtons.BackPressed += HardwareButtons_BackPressed;
}
private void HardwareButtons_BackPressed(object sender, BackPressedEventArgs e)
{
if (Frame.CurrentSourcePageType.FullName == "MyAppName.History")
Application.Current.Exit();
}
My app terminates when I press the back button on the second (history) page. But if I try to count the pressing of back button, it will navigate back to the login page. After this when I login again and press the back button the counter will be 2 and the app will exit. Here is the code what I tried:
private void HardwareButtons_BackPressed(object sender, BackPressedEventArgs e)
{
//MessageDialog msgbox = new MessageDialog("This is a popup message");
//await msgbox.ShowAsync();
//e.Handled = true;
backPressCount++;
if (backPressCount==2 && Frame.CurrentSourcePageType.FullName == "MyAppName.History")
Application.Current.Exit();
}
If I uncomment the 2 MessageDialog lines, the message appears, but I will be navigated back to the login page.
And I tried to uncomment the e.Handled = true;
line, but it didn't help.
I think I should disable the back button on history page or override it fully.
Anybody has idea for my problem?
EDIT: I rethink the back button behavivor, because I don't want to use ticker to measure the time between 2 press. Instead of this I would like only a popup message with a question, for example "Do you really want to quit?" and 2 buttons (Yes and No). It is works with the following code:
private async void HardwareButtons_BackPressed(object sender, BackPressedEventArgs e)
{
e.Handled = true;
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;
}
}
}
My problem is I am navigated to login page before I choose an answer on the popup. If I press Yes on it my app exits, and do nothing when I choose No. But I am on the login page at the moment. I tried to use dlg.Show() instead of dlg.ShowAsync() but it is missing. So I can use only async method, but in this case the page navigate to login in the background, because the NavigationHelper is called always, when I push the back button. What is the problem?