0

I have an app that needs to get user's attention when user exit the app at least once. So I get the code below to show a messagebox. What I don't know is how do I really exit the app if user has read the message? Because it seems the back key event will always come to the call I setup (OnBackKeyPress) Or what is a good way to handle showing a messagebox without messing around overriding BackKey? Because if have another pop up on screen and user pressed back key, it seems I got some exception if I tried to handle backkey myself.

My ideal situation is the app will close immediately once s/he pressed my Exit button of the messagebox. If pressed cancel, it will go back without exiting. Please help. Thanks!

Something I used... but not working well

private void OnBackKeyPressed(object sender, CancelEventArgs e)
    {
        e.Cancel = true;

        CheckBox checkBox = new CheckBox()
        {
            Content = "Do not ask me again",
            Margin = new Thickness(0, 14, 0, -2)
        };

        TiltEffect.SetIsTiltEnabled(checkBox, true);

        CustomMessageBox messageBox = new CustomMessageBox()
        {
            Caption = "Would you like to stop and exit?",
            Message =
                "If you want to continue listen music while doing other stuff, please use Home key instead of Back key",
            Content = checkBox,
            LeftButtonContent = "Exit",
            RightButtonContent = "Cancel",
        };

        messageBox.Dismissed += (s1, e1) =>
        {
            switch (e1.Result)
            {
                case CustomMessageBoxResult.LeftButton: //Exit
                    return;// What to do here??
                case CustomMessageBoxResult.RightButton: //Cancel
                case CustomMessageBoxResult.None:
                    break;
                default:
                    break;
            }
        };
        messageBox.Show();
    }

}
thsieh
  • 620
  • 8
  • 24

5 Answers5

3

If you really need the button to say "Exit" you would have to use either ColinE or Paul Annetts solution. Personally I would try formulating the message in another way and use the normal message box like this:

protected override void OnBackKeyPress(System.ComponentModel.CancelEventArgs e)
{
   string caption = "Stop music and exit?";
   string message ="If you want to continue listen music while doing other stuff, please use Home key instead of Back key. Do you still want to exit?";
   e.Cancel = MessageBoxResult.Cancel == MessageBox.Show(message, caption, MessageBoxButton.OKCancel);

   base.OnBackKeyPress(e);
}

This will show a message box but with "Ok" and "Cancel" button instead and will exit directly if user presses "Ok".

And for the "Do not ask again", maybe add a settings on a setting page instead. And add a second dialog the first time the user choose between "Ok" and "Cancel" if he or she wants to see the dialog again.

Johan Falk
  • 4,341
  • 2
  • 30
  • 42
1

Unfortunately there is no nice way to achieve the result you are after. Windows Phone does not allow you to programmatically exit the application. The solution most people use in this case is to throw an unhanded exception. See the following:

How to Exit windows phone 7 app? Is there a way to programmatically quit my App? (Windows Phone 7)

Community
  • 1
  • 1
ColinE
  • 68,894
  • 15
  • 164
  • 232
1

I have done the same thing using the custom message box. Make modifications to your code like this:

messageBox.Dismissed += (s1, e1) =>
    {
       switch (e1.Result)
        {
            case CustomMessageBoxResult.LeftButton: //Exit
                App.Current.Terminate(); //Put this line followed by break
                break;
            case CustomMessageBoxResult.RightButton: //Cancel
                break;
            default:
                break;
        }
    };

When the users presses Exit your app will terminate, When the press close, the default action of the "left" button is to close the message box. This should work provided you put the line

break;

in the right places

Hope this helps

T J
  • 659
  • 1
  • 8
  • 18
  • Was just doing some further testing on this. The only problem here is that if the back button is pressed and the CustomMessageBox opens, if the user presses the back button again, the app will crash. Will try to figure it out and solve it. – T J Feb 12 '13 at 08:35
0

For WP8 you can call Application.Current.Terminate() to programmatically exit the app. For WP7 this is not available and you'd have to pick one of the options Colin gave.

However you should be careful to meet the certification requirements of the Microsoft Store if you go this route as your app could get rejected if it doesn't behave as expected.

Paul Annetts
  • 9,554
  • 1
  • 27
  • 43
0

You can simply cancel the event of back key pressed first and then based on the button clicked on the message box you can take the appropriate action as follows:

private void BackKey_Pressed(object sender, System.ComponentModel.CancelEventArgs e)
{
        e.Cancel = true;
        MessageBoxResult messageBoxResult = MessageBox.Show("Text", "Caption", MessageBoxButton.OKCancel);
        if (messageBoxResult == MessageBoxResult.OK)
        {
            Application.Current.Terminate();
        }
}
prince
  • 1,129
  • 1
  • 15
  • 38