Win10 development: process the backend key on the PC.

Source: Internet
Author: User

Win10 development: process the backend key on the PC.

We know that there is a backend key on win10 and tablet, so what should we do on PC? It doesn't matter.

If you are a loyal user of UWP, you will surely see the following back key.

How can we do this ?,

First, open the App. xaml. cs file. In the OnLaunched method, after the Frame object is initialized, subscribe to the Navigated event. After the Frame navigation, You need to determine whether to display the backend key (because the homepage does not need to display the backend key ).

rootFrame.Navigated += RootFrame_Navigated;

Then the back key is displayed in the RootFrame_Navigated method.

 1         private void RootFrame_Navigated(object sender, NavigationEventArgs e) 2         { 3             if ((Window.Current.Content as Frame).BackStack.Any()) 4             { 5                 SystemNavigationManager.GetForCurrentView().AppViewBackButtonVisibility = AppViewBackButtonVisibility.Visible; 6             } 7             else 8             { 9                 SystemNavigationManager.GetForCurrentView().AppViewBackButtonVisibility = AppViewBackButtonVisibility.Collapsed;10             }11         }

We can determine whether an element exists in the Frame navigation stack. If no element exists, it indicates that it is on the home page and there is no need to display the backend key.

Although the backward key is displayed, we have not responded to it. Next we subscribe to the BackRequested event of the SystemNavigationManager object.

1 SystemNavigationManager.GetForCurrentView().BackRequested += App_BackRequested;

In the App_BackRequested method, we perform the back key response.

1         private void App_BackRequested(object sender, BackRequestedEventArgs e)2         {3             var rootFrame = Window.Current.Content as Frame;4             if (!rootFrame.CanGoBack) return;5             rootFrame.GoBack();6         }

Click the back button to return the result. However, when we deploy the app on our mobile phone, we will find another problem. No matter which page we are on, the app will exit after pressing the back key. This is because the App_BackRequested method does not block app exit. You need to add the code e. Handled = true. After the code is changed, the Code is as follows:

1         private void App_BackRequested(object sender, BackRequestedEventArgs e)2         {3             var rootFrame = Window.Current.Content as Frame;4             if (!rootFrame.CanGoBack) return;5             rootFrame.GoBack();6             e.Handled = true;7         }

In this case, both Mobile and PC can process the backend request perfectly. Let's take a look at the effect.

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.