Windows Phone 8: How to show messagebox right before exiting the App if pressed Back key?

China☆狼群 提交于 2020-01-25 06:57:05

问题


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();
    }

}

回答1:


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.




回答2:


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)




回答3:


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




回答4:


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.




回答5:


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();
        }
}


来源:https://stackoverflow.com/questions/13898772/windows-phone-8-how-to-show-messagebox-right-before-exiting-the-app-if-pressed

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!