UWP: Back button is everywhere

One more thing which was not available for universal interfaces is the back button. You know that all Windows Phone devices have hardware or software back button. So, when you develop Windows Phone 8.x applications you need to handle that button but in case of Windows 8.x applications you need to create your own back button from scratch. Of course, Microsoft published design guide how to create and handle back button in Windows 8.x applications but the approach was very different compared to the Windows Phone.

Starting with Windows 10 developers can use the same approach everywhere.

If you want to handle the back button in any page you need to implement the following code:

 protected async override void OnNavigatedTo(NavigationEventArgs e)
{
 SystemNavigationManager.GetForCurrentView().AppViewBackButtonVisibility = AppViewBackButtonVisibility.Visible;
 SystemNavigationManager.GetForCurrentView().BackRequested += MainPage_BackRequested;
 base.OnNavigatedTo(e);
}

Thanks to these two lines of code you can activate the back button if it’s not available (in case of a desktop, for example) and apply an event handler for the BackRequested event. BackRequested event handler is all you need in order to handle software or hardware back button. The simplest implementation can look like:

 private void MainPage_BackRequested(object sender, BackRequestedEventArgs e)
{
 if (this.Frame.CanGoBack) this.Frame.GoBack();
} 

Let’s see what happens in the desktop mode if you run code above.

 

You can see that the back button is added to the application title and users can click it like the back button on phone devices.

In order to see how the back button works in the tablet mode (if you don’t have a tablet) you need to open the Settings window, navigate to the Tablet mode menu item there and switch Windows to the tablet mode (or click Notification Hub button and use the shortcut there):

 

If you resize Setting window to the minimum size you can see that the back button is implemented there in the same way.

Once you switch Windows to the tablet mode you can see that the back button is displayed on the taskbar (outside your interface) but it still works fine.

 

So, the back button is everywhere and with the help of the Universal Windows Platform developers can use the same approach to implement the back button user experience.