How to exit or close an UWP app programmatically? (Windows 10)
Asked Answered
H

3

37

I need it for their own exit button. Tell me please? I try this: this.Close(); //or Exit dont work(

Hbomb answered 20/9, 2015 at 9:0 Comment(0)
E
75

You can use the CoreApplication class. It provides a static exit method:

public void CloseApp()
{
    CoreApplication.Exit();
}

However, the documentation states the following:

Note Do not use this method to shut down an app outside of testing or debugging scenarios.

Sadly, the reason behind that is left unkown.


Further more, you can use the old-fashioned Application.Exit method (non-static):

public void CloseApp()
{
    Application.Current.Exit();
}

Here you should also take a look in the remarks:

Use this method to provide UI that enables users to exit your app. Normally, however, you should not provide this UI because the system automatically manages app lifetime and terminates suspended apps as needed to free resources.

tl;dr: Both Exit methods will terminate the app, rather than suspending it. You should ask yourself if this really is the action you want to do.

Ejection answered 20/9, 2015 at 9:27 Comment(4)
The reason is it's against the UX guide lines to close the app programmaticallyCatchall
Application.Current.Exit() can cause a catastophic failure.Murrell
The UX guidelines are inadequate if they don't support closing an app programmatically. That particular clause would single handedly reject the design of every game running on Windows.Murrell
@GavinWilliams could you clarify what you mean with "catastrophic"? Could you name a few examples or provide a few catchwords to search for? Couldn't find anything on this.Preselector
G
20

This is the supported way of exiting a UWP app:

Application.Current.Exit();

It is however relatively rare that you should use it. Consider carefully the UI experience related to the scenario where you would use this method. For example it may be justified to programmatically exit an application if some account has expired or security permissions managed remotely were revoked. It is rare that you have your own "Exit" button sitting in the middle of your screen without contravening Windows guidelines.

Glazer answered 20/9, 2015 at 9:4 Comment(3)
"It is rare that you have your own "Exit" button sitting in the middle of your screen ..." - This is actually very common, as almost without exception, every game ever made has a quit button!Murrell
How else would you exit a desktop app that only runs fullscreen?Dylan
(1) The method doesn't work. It seems to destroy the main window, but the debugger tells me the program is still running. (2) What else are you going to do with the application if the user doesn't have login credentials? You're going to let the screen sit there forever?Stillbirth
R
10

If you want to suspend the app instead of terminating try to use ApplicationView.TryConsolidateAsync(). For example, if app implements only one ApplicationView try calling ApplicationView.GetForCurrentView().TryConsolidateAsync() to close the app.

The obvious benefit of this method is app is closed just as you will do by pressing close button in titlebar, the closing is graceful, animation is the same and app is suspended instead of abruptly exiting.

Also, when you launch your app again after closing by this method, app starts in the same position and size as you closed it before while using Application.Current.Exit() and CoreApplication.Exit() doesn't start the app in the same position and size.

Reposeful answered 24/6, 2020 at 13:5 Comment(6)
+1. Didn't know about this method. In the documentation it says "This method is a programmatic equivalent to a user initiating a close gesture for the app view.", so it seems to be better than the other methods.Thirtytwo
Yes it is the recommended method to use.Reposeful
This works really well. I'm wondering why the SystemNavigationManagerPreview.GetForCurrentView().CloseRequested event is not being invoked.Sportsman
It might not invoke if the window is main window, if you are manually closing why not add your function before that?? Can you describe why you need to subscribe to this event??Reposeful
ApplicationView.GetForCurrentView().TryConsolidateAsync() can throw an exception - element not found.Murrell
@Gavin Williams I couldn't find the documentation about "Element not found" exception, but it seems that you are trying to call the method before even ApplicationView is created hence the error. Can you describe when you are getting this exception??Reposeful

© 2022 - 2024 — McMap. All rights reserved.