prompt confirmation dialog when exit app - windows-phone-7

I am working on an app on wp7.
I hope to prompt a confirmation dialog when user exit app (press back button).
Is it possible?
Welcome any comment

Please handle the BackKeyPress button in the Application page to handle the back key press.
In Page.xaml file in the element add this code
BackKeyPress="PhoneApplicationPage_BackKeyPress"
it should look like
<phone:PhoneApplicationPage BackKeyPress="PhoneApplicationPage_BackKeyPress"
..//other attributes .. >
in event handler you write the code as follows
private void PhoneApplicationPage_BackKeyPress(object sender, System.ComponentModel.CancelEventArgs e)
{
MessageBoxResult mb = MessageBox.Show("You want exit the page", "Alert", MessageBoxButton.OKCancel);
if( mb != MessageBoxResult.OK)
{
e.Cancel = true;
}
}

It's possible to catch when the user exits pressing the Back button, but it is not possible to stop the application from being made "dormant" when the user presses the hardware Start button or Search buttons.

You can stop back navigation by set e.Cancel in back key press event.
In MainPage.xaml.cs constructor:
OnBackKeyPress += (s, e) =>
{
if (MessageBox.Show("", "", MessageBoxButtons.OkCancel) == MessageBoxButtons.Cancel)
{
e.Cancel = true;
};
};

Related

Close a form when FormClosing cancels the event

I am building an application that minimizes to tray when the user clicks the close button (the cross on the upper right corner) using this code:
private void FrmTest_FormClosing(object sender, FormClosingEventArgs e) {
e.Cancel = true;
WindowState = FormWindowState.Minimized;
}
however, if I want to actually close the window on a button using Close() this handler is called and the form doesn't close.
How can I close my form / application now?
I fixed it (thought I checked this, but apparently, something went amiss while debugging).
You can use the CloseReason property to find out why the event is called. When the user clicks on the close button on the window, e.CloseReason is UserClosing, otherwise it is ApplicationExitCall:
private void FrmTest_FormClosing(object sender, FormClosingEventArgs e) {
if (e.CloseReason == CloseReason.UserClosing) {
e.Cancel = true;
WindowState = FormWindowState.Minimized;
}
}
Update when using Close() to close the form, the CloseReason will be the same and you should use a boolean as Hans Passant mentions in the comments. When using Application.Exit(), this CloseReason solution works.

Windows Phone 7 Back Key Press event change?

I want to do some work if user presses back key on the phone with navigating back How to do it?
With following code it cancel navigation to back page & do some stuff but i want to do stuff while going back please help...
protected override void OnBackKeyPress(System.ComponentModel.CancelEventArgs e)
{
// do some stuff...
e.Cancel = true;
}

WP7 PhoneGap application back button exiting application

I am working on WP7 Phonegap app. I have used below code to handle back button, but whenever I click back button , back button event is not cancelled and application gets exited.
void OnBackKeyPressed(object sender, CancelEventArgs e)
{
var result = MessageBox.Show("Dof fd fd you want to exit?", "Attention!",
MessageBoxButton.OKCancel);
if (result == MessageBoxResult.OK)
{
// Do not cancel navigation
return;
}
e.Cancel = true;
}
Please helpme on same.
Thanks in advance.
One of the lead devs for the Windows Phone implementation of PhoneGap recently wrote about correctly handling the back button.
See his article at http://www.risingj.com/archives/493

back button handling wp7

My problem is that I have a list. When I long press a particular item in the list then it opens a context menu and when I click on a menu item inside context menu then it opens a popup,so on pressing the hardware back button i want that i again go back to the list.
so for doing this my code is:
protected override void OnBackKeyPress(object sender,System.ComponentModel.CancelEventArgs e)
{
if (calendarDescripton.Visibility == Visibility.Visible)
{
calendarDescripton.Visibility = Visibility.Collapsed;
e.Cancel = true;
}
}
After using this code when I click the button that opens the list,the application exits,it does not open list also.
I think first the Navigation should be canceled, before making any other changes. Try this
protected override void OnBackKeyPress(object sender,System.ComponentModel.CancelEventArgs e)
{
if (calendarDescripton.Visibility == Visibility.Visible)
{
e.Cancel = true;
calendarDescripton.Visibility = Visibility.Collapsed;
}
}
If this doesn't help, place a break piont at the if condition and check if it is entering inside the if or not
If the break point doesn't hit, means there is something wrong with your Navigation approach.
If you are using NavigationService.Navigate() method for page navigation, it should work.
Otherwise, if you are using,
App.Current.RootVisual = new MyPage();, then BackKey cannot be overridden.

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

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.
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;
}

Resources