samwize
samwize

Reputation: 27353

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.

Upvotes: 2

Views: 3362

Answers (3)

user3738267
user3738267

Reputation: 1

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

Upvotes: 0

Amr H. Abd Elmajeed
Amr H. Abd Elmajeed

Reputation: 1521

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.

Upvotes: 6

mikeesouth
mikeesouth

Reputation: 1640

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

Upvotes: 2

Related Questions