The UWP Notepad Project (Redux): App Close Confirmation (Premium)

I think I’ve finally found a reliable way to prompt the user to save an unsaved document if they try to manually close the app. As I noted in the previous installment---which was the final installment when I originally published this series back in April---I had been struggling to get this very obvious functionality to work. And all of my prior attempts had failed, despite the fact that Microsoft documents how to do it.

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.

Interesting.

Over the past three programming projects that I’ve documented on the site---the WinForms, WPF, and UWP versions of .NETpad, respectively---I’ve always created the app at least twice, the second time being when I documented the process. So I did the same here, since we’re moving past the codebase as its sat since April. After getting app close confirmation to work on one version of the app, I booted up Visual Studio on a second PC, loaded the original version of the project, and added the new code there too. And … it works.

And that’s why I wrote “I think” above. I think this will work for you too. So let’s give it a shot.

The first step is to edit Package.appxmanifest. Normally, you just double-click this file in the Solution Explorer, and it displays in a visual designer. But we need to edit this file’s XML code directly. So don’t double-click it. Instead, select it, then right-click it, and then choose View Code.

You should see something that looks like this.

We have to make two changes to this file. First, replace the entire <Package> block with the following:
<Package
  xmlns="http://schemas.microsoft.com/appx/manifest/foundation/windows10"
  xmlns:mp="http://schemas.microsoft.com/appx/2014/phone/manifest"
  xmlns:uap="http://schemas.microsoft.com/appx/manifest/uap/windows10"

xmlns:rescap="http://schemas.microsoft.com/appx/manifest/foundation/windows10/restrictedcapabilities"
  IgnorableNamespaces="uap mp rescap">
Next, scroll down until you find a <Capabilities> block. It should currently look like so:
<Capabilities>
    <Capability Name="internetClient" />
</Capabilities>
Edit this so that the line <rescap:Capability Name="confirmAppClose" /> appears above the internetClient capability. (This is very important.) When you’re done, this block will look like so:
<Capabilities>
    <rescap:Capability Name="confirmAppClose" />
    <Capability Name...

Gain unlimited access to Premium articles.

With technology shaping our everyday lives, how could we not dig deeper?

Thurrott Premium delivers an honest and thorough perspective about the technologies we use and rely on everyday. Discover deeper content as a Premium member.

Tagged with

Share post

Please check our Community Guidelines before commenting

Windows Intelligence In Your Inbox

Sign up for our new free newsletter to get three time-saving tips each Friday

"*" indicates required fields

This field is for validation purposes and should be left unchanged.

Thurrott © 2024 Thurrott LLC