Suspending Unloading

You may want to perform some operation such as saving while the PdfView is unloading. However, Unloaded events are fired too late to interact with the PdfView. What you can do is suspend unloading until you are finished performing any required operations. To achieve this, add an event handler for PdfView.OnOnSuspendUnloading.

Note that it is very important to finally call Deferral.Complete() or the unloading will be blocked:

Copy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
{
    // Subscribe to the event handler.
    PdfView.OnSuspendUnloading += PdfViewOnOnSuspendUnloading;
}

private async void PDFView_OnOnSuspendUnloading(PdfView sender, Deferral deferral)
{
    try
    {
        // Save our PDF to a new file.
        var savePicker = new FileSavePicker {SuggestedStartLocation = PickerLocationId.DocumentsLibrary};
        savePicker.FileTypeChoices.Add("Portable Document Format", new List<string> {".pdf"});
        savePicker.SuggestedFileName = "New Document";

        var file = await savePicker.PickSaveFileAsync();
        if (file != null)
        {
            using (var stream = await file.OpenAsync(FileAccessMode.ReadWrite))
            using (var dataWriter = new DataWriter(stream))
            {
                await PDFView.Document.ExportToDataWriterAsync(dataWriter);
            }
        }
    }
    finally
    {
        deferral.Complete();
    }
}