Control press “back button” and disable close the application using a dialog for confirm - wp7

China☆狼群 提交于 2019-12-10 14:55:08

问题


I need show a simple dialog with the question : 'Do you want to exit the application?' YES or NO. This dialog will be shown when the user presses the back button of the device.

I know how I can show this dialog , but I don't know how to disable the back action: close app.

It's always closed.


回答1:


If I understand you correctly, you want to display a confirmation dialog when the user clicks the back button on the main page of your app to ask whether they really want to exit. If the user selects Yes the app exits, otherwise you cancel the back navigation. To do this, in the MainPage class constructor hook up an event handler

MainPage()
{
  BackKeyPress += OnBackKeyPressed;
}

void OnBackKeyPressed( object sender, CancelEventArgs e )
{
  var result = MessageBox.Show( "Do you want to exit?", "Attention!", 
                                MessageBoxButton.OKCancel );

  if( result == MessageBoxResult.OK ) {
    // Do not cancel navigation
    return;
  }
  e.Cancel = true;
}


来源:https://stackoverflow.com/questions/7741939/control-press-back-button-and-disable-close-the-application-using-a-dialog-for

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