7

I'm trying to prevent the app from being closed by clicking the Close box on the App Window. For example, having a text editor with unsaved changes, upon pressing Close Box, I would first display, "Do you want to save changes before exiting?"

How can I detect app wanting to close and prevent that from happening?

I'm using C++, and this needs to be for Windows 10 Universal Apps UWP. I already know how to do this for Win32.

Stefan Wick MSFT
  • 13,600
  • 1
  • 32
  • 51
Esenthel
  • 499
  • 4
  • 17
  • 1
    I don't think that you can do this, you can only react to the app suspending event that's being called at the time. – Tamás Deme Nov 13 '15 at 09:16
  • 2
    This is possible, as Microsoft Edge browser is a UWP app, and it supports this behavior in some cases. For example in Facebook Chat, type some message without pressing enter (send) and try to close browser. This means it is possible, I just don't know how. Any help? – Esenthel Jan 10 '16 at 09:08
  • Edge is a 1st party app - MS can use APIs that no one else can. https://msdn.microsoft.com/en-us/library/windows/apps/mt243287.aspx clearly says that there's no application closing event. – Tamás Deme Jan 10 '16 at 19:22

1 Answers1

7

The comments are correct. There is currently no way for a regular Store app to do this.

However, with the Creators Update (and corresponding SDK) we have included a preview API that you can now check out for this functionality:

The Windows.UI.Core.Preview.SystemNavigationManagerPreview class provides a CloseRequested event that an app can mark as handled. For the event to work the app will need to declare the restricted 'confirmAppClose' capability per: https://learn.microsoft.com/en-us/windows/uwp/packaging/app-capability-declarations

Please let us know your feedback.

Thanks, Stefan Wick - Windows Developer Platform

Stefan Wick MSFT
  • 13,600
  • 1
  • 32
  • 51
  • Thanks for the info that's exciting. I'll wait until the API is finalized – Esenthel Apr 19 '17 at 04:34
  • 'ConfirmAppClose' capability not found in 16299 sdk – Vincent Nov 08 '17 at 02:21
  • @Snekithan: yes, this has been available since the Creators Update as mentioned in the answer. Here is the MSDN link: https://learn.microsoft.com/en-us/uwp/api/windows.ui.core.preview.systemnavigationmanagerpreview.closerequested – Stefan Wick MSFT Jun 07 '18 at 21:33
  • Is there a way to trigger the SystemNavigationManagerPreview.GetForCurrentView().CloseRequested() event from a Page view button in order to cause the same followup that clicking the standard issue X in to right corner causes? I tried "Window.Current.Close();" and it generates a "System.Runtime.InteropServices.COMException: 'A method was called at an unexpected time. Closing main window is not allowed.'" – myusrn Nov 10 '18 at 02:38
  • CloseRequested is only fired for user initiated close gestures (such as clicking the X button). If you want to shutdown the app programmatically you can call Application.Shutdown(). If you want to ask the user for confirmation you can do so before making that call. – Stefan Wick MSFT Nov 11 '18 at 04:50