Detect Application closing in UWP
Nearly every application when user closes it using the “X” button on upper right corner prompts something like this
Asking user if he wants to save actual work or cancel closing operation.
Doing this in a UWP application is far from trivial, sounds strange uh?
Despite some options are available (like CoreWindow esposes a Closed event but it never fires) the only available solution sounded to be this one which is unfortunately complex and unreliable.
From Creators update (version 1703) a new class SystemNavigationManagerPreview has been added and this class exposes a CloseRequested event (yeah!)
Ok, let’s see how it works, let’s fire up a brand new UWP app inside Visual Studio and let’s add this code inside MainPage.Xaml codebehind.
Let’s put a breakpoint inside OnCloseRequested method, run the app and try closing the window clicking the upper right “X” button.
The breakpoint doesn’t it right? so What’s wrong with that? nothing indeed, we just miss an important detail: In order to work, you need to add a restricted capability inside package.appmanifest, the one we need is AppClose Confirmation that MSDN describes as: confirmAppClose restricted capability allows apps to close themselves, their own windows, and delay the closing of their app.
Ok, let’s manually edit the manifest file adding following lines:
Inside Package tag: xmlns:rescap="http://schemas.microsoft.com/appx/manifest/foundation/windows10/restrictedcapabilities"
then add rescap to IgnorableNamespacesAttribute: IgnorableNamespaces="uap mp rescap"
Then inside Capabilities tag:
<Capabilities>
<Capability Name="internetClient" />
<rescap:Capability Name="confirmAppClose"/>
</Capabilities>
Very important: confirmAppClose capability must be listed before any optional DeviceCapability otherwise it won’t work.
Let’s now try again and… yes! it works!
You can now do whatever you want inside the OnCloseRequest handler, like showing a dialog and if user denies closing setting SystemNavigationCloseRequestedPreviewEventArgs.Handled=true to prevent app from closing.
Please take care of the note inside capabilities declaration page on MSDN:
Some of these restricted capabilities are almost never approved for apps submitted to the Store, except in very specific and limited circumstances. These capabilities are called out in the table below. We recommend not declaring these capabilities in your app if you plan to distribute it through the Store.
I see no reason why Microsoft should prevent this but take note and don’t forget to mention its use inside portal Submission options page
about me

Welcome! I'm Corrado, an Italian software consultant, Microsoft MVP, trainer and speaker and i use this space to post some thoughts about my daily working experiences. If you like blog contents just subscribe my rss feed.
Corrado's Blog 1.0
where i'm online
latest tweets
tags




- Recent Posts
- Recent Comments
- Ron - Xamarin Novice on Adding a splash screen to Xamarin Forms apps
- msdYqb on Xamarin Forms: CarouselView in action
- msdYqb on Adding a splash screen to Xamarin Forms apps
- Jose Cueva on Adding a splash screen to Xamarin Forms apps
- Ali on Xamarin.Android Status bar color not set
- Archives
- August 2018
- July 2018
- May 2018
- April 2018
- March 2018
- February 2018
- January 2018
- September 2016
- August 2016
- June 2016
- May 2016
- April 2016
- March 2016
- January 2016
- December 2015
- September 2015
- August 2015
- April 2015
- March 2015
- February 2015
- January 2015
- December 2014
- October 2014
- September 2014
- July 2014
- May 2014
- April 2014
- January 2014
- December 2013
- September 2013
- August 2013
- July 2013
- April 2013
- March 2013
- January 2013
- November 2012
- October 2012
- August 2012
- July 2012
- Categories
- Meta
- TagsAndroid Android emulator Binding Blend C# CarouselView Custom controls effects encoding Fiddler FloatingActionButton Genymotion german git Google GridView IDEA internet iOS ipconfig Lottie Material Design MVVM MvvmStack oAuth ParallaxView ScrollViewer Skype SnackBar Typescript UWP VirtualBox Visual Studio WebClient Win2D Windows 8 Windows Phone WinJS WinJS binding list WinRT Xamarin Xamarin.iOS Xamarin Forms XAML Xamrin