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
Related
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;
}
I'm trying to close my Wp7 app when the back is pressed in emulator.I have tried the code on that page from which I would close the whole app.the code is given below:
protected override void OnBackKeyPress(System.ComponentModel.CancelEventArgs e)
{
base.OnBackKeyPress(e);
if (NavigationService.CanGoBack)
{
while (NavigationService.RemoveBackEntry() != null)
{
NavigationService.RemoveBackEntry();
}
}
}
Help needed !
Thanks in advance!
First of all, There is no way to close wp7 application programatically.
And to do this you need not override back key press. When in the first page(ideally the home page or main page), pressing back key automatically closes the application.
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;
};
};
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;
}
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