Event when PhoneApplicationPage exits
How do I handle the event when a PhoneAppplicationPage (eg. MainPage.xaml) exits?
I tried handling Unloaded event but that do开发者_如何学Goesn'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
精彩评论