Today, I finally tried something I should have tried months ago: I simply started a new project to see whether I could prevent the user from closing that app by displaying a prompt. And it worked. On the first try.
That was confusing, given how much time I’ve spent on this task. But it gave me the opportunity to copy and paste the relevant code between this new app and the UWP version of .NETpad. And when I did that, it also worked: I was able to prevent the user from closing the app by displaying a prompt if there was unsaved data.