3
votes

In my windows phone app, i have to delete some files when user navigates away from a page. I have handled this on OnBackKeyPress event. This works perfectly fine. The issue is newer updates of windows phone 8 and windows phone 8.1 also have close button on the top right corner. I have not found a way to handle this event. Could anyone please let me know how can i handle this event and delete the files.

1

1 Answers

1
votes

Rather than using OnBackKeyPress, you should use OnNavigatedFrom. This event is triggered whenever the user leaves the page, no matter how.


If you need to handle only the specific case when the app is suspended/terminated, you can subscribe to the Deactivated and Closing events on the PhoneApplicationService.

PhoneApplicationService.Current.Deactivated += yourHandler;

Note that is the your event handler is a non-static method declared in your page, then you must make sure to unsubscribe the event when leaving the page, to avoid a memory leak. To know when to unsubscribe, you can stick to your OnBackKeyPress logic.