uwp – Application Lifecycle – "Suspending" state handling

When moving to "Suspened" state there is special handler connected with this event: Open "App.xaml.cx" class and see "App" constructor – there is event handler:

public App()
        //Handle suspending operation with event handler:
        this.Suspending += OnSuspending;

Now you can handle suspension event:

private Dictionary<string, object> _store = new Dictionary<string, object>();
private readonly string _saveFileName = "store.xml";
private async void OnSuspending(object sender, SuspendingEventArgs e)
        var deferral = e.SuspendingOperation.GetDeferral();
        _store.Add("timestamp", DateTime.Now);
        await SaveStateAsync();
        //TODO: Save application state and stop any background activity
        //Here you can use  await SuspensionManager.SaveAsync();
        //To read more about saving state please refer to below MSDN Blog article:

    private async Task SaveStateAsync()
        var ms = new MemoryStream();
        var serializer = new DataContractSerializer(typeof(Dictionary<string, object>));
        serializer.WriteObject(ms, _store);

        var file = await ApplicationData.Current.LocalFolder.CreateFileAsync(_saveFileName, CreationCollisionOption.ReplaceExisting);

        using (var fs = await file.OpenStreamForWriteAsync())
            //because we have written to the stream, set the position back to start
            ms.Seek(0, SeekOrigin.Begin);
            await ms.CopyToAsync(fs);
            await fs.FlushAsync();

if you want to reproduce, please indicate the source:
uwp – Application Lifecycle – "Suspending" state handling - CodeDay