Frage

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

War es hilfreich?

Lösung

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.

Andere Tipps

I've checked your code and it works just fine - without any problems both on Emulator and Device - MessageBox persists and doesn't dissapear.

Maybe you have other MessageBox in Navigating events or somewhere where time is limited to 10 seconds:

The events described in this section give you opportunities to save and restore state as your application enters and leaves the foreground. However, the recommended practice is to save state data as the data changes. For example, the results of a web request can be saved to disk or the application state dictionary (or both) at the moment it arrives from the network. You should not wait until the Deactivated event occurs to store this data. Remember that all application lifecycle events enforce a limit of 10 seconds for an application to complete any tasks.

I've met same problem with my Windows Phone 8.1 XAML/C# app. In my case it was HardwareButtonsOnBackPressed event and setting BackPressedEventArgs.Handled property to true before showing MessageBox did the trick.

private void HardwareButtonsOnBackPressed(object sender, BackPressedEventArgs e)
    {
        e.Handled = true;

            var dlg = new MessageDialog("Are you shure you want to quit?", "Exit application?");
            dlg.Commands.Add(new UICommand("Yes", c =>
            {
                Application.Current.Exit();
            }));
            dlg.Commands.Add(new UICommand("No"));
            dlg.ShowAsync();
        }
    }
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top