I need it for their own exit button. Tell me please? I try this: this.Close(); //or Exit dont work(
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.
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.
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.
SystemNavigationManagerPreview.GetForCurrentView().CloseRequested
event is not being invoked. –
Sportsman ApplicationView
is created hence the error. Can you describe when you are getting this exception?? –
Reposeful © 2022 - 2024 — McMap. All rights reserved.