Search code examples
c#windows-phone-8messagebox

Wonder action from MessageBox in windowsphone


In my windows phone 8 application I need a messageBox before closing the app using BackKey Press 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);
}

from HERE

For my case it is working fine..

  • If you click ok it will close the application using Terminate()
  • If it is cancel the app is in activated mode
  • The problem with " If you leave the MessageBox alone without selecting any option the is going to close after 10 seconds"

    What I am missing here.. Let me know If I missing anything


Solution

  • message box with somthing being unselected for sometime automatically triggers the cancel option .

    What best you can do here is declare e.Cancel=true; and then pop the messagebox

    protected override void OnBackKeyPress(System.ComponentModel.CancelEventArgs e)
    {   e.Cancel=true; 
        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?";
        if(MessageBox.Show(message, caption, MessageBoxButton.OKCancel)==MessageboxResult.Ok)
        {
           //exit;
        }
        else
        {
          //do what ever you like
        }
        base.OnBackKeyPress(e);
    }
    

    Moreover you should end up by making a custom message box instead of calling Messagebox.Show(); as Pressing lock screen button while the message is opened would also make it invisible.