How to save data on Windows Store App Close?

2019-07-22 12:47发布

问题:

i have the following problem. When the user closes the Windows store app, i want a text file with data to be saved.

What method should i write so that when closing the app a file gets saved ?

回答1:

This article describes the application lifecycle of a Windows Store App.

If you look at the very first figure, you can see that there are only 3 events related to application lifecycle:

  1. Activated - Raised when program first starts
  2. Suspended - Raised when program is suspended (i.e. the user returns to the Start Screen or another app)
  3. Resuming - Raised when the program is awakened from its suspended state.

The fourth transition - the one to the "Not Running" state - has no such notification event. The reason is: you don't really know when the app will fully close. Nor should you - Microsoft wants you to perform all of your state-saving logic in the transition from "Running" to "Suspended." In this way, they can free up resources when they deem necessary.

Even if the user forces the program to terminate (by right-clicking it and selecting "Close" from the task menu), you will enter the "Suspended" state for exactly 10 seconds before the program is terminated. So you can rest easy that your state-saving logic will always be executed.



回答2:

you don't really know when the app will fully close. Nor should you...

I don't agree with this - Microsoft are copying this from Apple, and I don't know why, I never liked Apple's implementation either.

If your user makes changes to your App, then closes it using the keyboard or gesture, then restarts it say after 6 seconds, all changes are lost.

I don't see any way as a developer to get around this. Whoever decided that closed events (and exit buttons for that matter) are evil is an idiot.