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

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

Related

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.

prompt confirmation dialog when exit app

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

How to handle back button in windows phone 7?

I have an application, the main page contains few functions. To explain in detail - I have save, color palette button in my main page. When any of these buttons are clicked, save pop up or color palette appears. How to handle back button of the device, when color palette or save pop up is opened. When back button is pressed at these scenario it should just make them invisible and stay on the main page. When nothing is being performed in the main page, then it should come out of the app. I tried to make their visibility collapsed on back button press. But it still is coming out of the application.
Please, guide me in this. Thanks in advance.
Override PhoneApplicationPage.OnBackKeyPress and then set CancelEventArgs.Cancel to true if you want to stop it from actually going back.
protected override void OnBackKeyPress(CancelEventArgs args)
{
if (PanelIsShowing)
{
HidePanel();
args.Cancel = true;
}
}
The back button behaves as is intended by Microsoft.
If you change its behavior you risk your application not being certified for the Marketplace.
If you want the back button to close the popups, turn the popups into pages so the back button navigates back to the main page..
You need to use
protected override void OnBackKeyPress(CancelEventArgs e)
{
if (_popup.IsOpen)
{
_popup.IsOpen= false;
e.Cancel = true;
}
else
{
base.OnBackKeyPress(e);
}
}
That should do the trick.

Event when PhoneApplicationPage exits

How do I handle the event when a PhoneAppplicationPage (eg. MainPage.xaml) exits?
I tried handling Unloaded event but that doesn't get called when I exit the page.
I think you mean this event :
protected override void OnNavigatedFrom(System.Windows.Navigation.NavigationEventArgs e)
{
base.OnNavigatedFrom(e);
// write exit logic
}
This event is called whenever you navigate away from a page, whether by pressing the back button or the home button. Just paste the above in the code behind class of your page and adjust it to your needs.
What do you mean by exits? You can handle the event when the user presses the back key by subscribing to PhoneApplicationPage.BackKeyPress.
Example:
private void OnBackKeyPress(object sender, System.ComponentModel.CancelEventArgs e)
{
MessageBoxResult messageBoxResult = MessageBox.Show("Are you sure you want to exit?", "Exit?", MessageBoxButton.OKCancel);
if (messageBoxResult != MessageBoxResult.OK)
e.Cancel = true;
}
However when the user exits the application by pressing the home button, search button, a toast notification, incoming call or similar it is called tombstoning. You can handle the Deactivated event on the App to save a state in your application so that you can resume where the user left off the next time the app is started. But you can't "stop" the tombstoning - so that the user cant exit the application.
Read more about tombstoning here:
Architecting WP7 - Part 5 of 10: Tombstoning by Shawn Wildermuth
when a PhoneAppplicationPage ( MainPage.xaml) exits,
this is like form closing event,
MainPage_PointerExited event works in wp8.1.
Even though this thread is 4 years old, I want to answer in case that
may be helpful to others who look for the answer.
Private Sub MainPage_PointerExited(sender As Object, e As PointerRoutedEventArgs) Handles Me.PointerExited
Application.Current.Exit()
'your code goes here
end sub

Resources