Search code examples
windowsuwpwindows-10-universalwindows-10-desktopuwp-navigation

How to close main window of uwp programmatically


Unlike wpf, uwp apps can't close their mainpage programatically. Window.Current.Close() fails when executed on mainpage of the app. What is the correct way of closing the mainpage in uwp??


Solution

  • Executing ApplicationView.GetForCurrentView().TryConsolidateAsync() in main window, closes it properly as if you have closed it by clicking close button in title bar. If app has only one window per instance this is preferred over Application.Exit() as this method suspends the app while Application.Exit() closes app abruptly. Also closing app by this method your app's previous position and size is remembered unlike for Application.Exit().