简体   繁体   中英

UWP Template10 SystemNavigationManager Back button adding GotFocus event

I am trying to to add an OnGotFocus event on Template 10 Back button as:

In PageViewModels.cs:

 public override async Task OnNavigatedToAsync(object parameter, NavigationMode mode, IDictionary<string, object> suspensionState)
    {
        SystemNavigationManager.GetForCurrentView().BackRequested += OnGotFocus;
    }

private async void OnGotFocus(object sender, BackRequestedEventArgs e)
    {
        ....
    }

But that does not work. Can anyone give me any pointers?

What you did in your code snippet is that you wired up a handler for the BackRequested event. This event is fired when the system registers a request to go back in the app. This can be fired by the user tapping the back button in Task bar in Tablet mode on desktop, or by clicking the back button in title bar of your app in Windowed mode or pushing the back button on mobile device.

Either way, this event is fired by the system and the only thing it does is that it calls your method. The name of your method does not matter at all .

I think you should review some basics about event handling in C# to clear up any confusion.

To be able to use the OnGotFocus event, you will have to create your own back button in XAML and add the handler to this button, because only this way you have full control over the control. If you just use the system provided BackRequested event, the system is in control and apart from this event you can't customize anything.

<Button GotFocus="OnGotFocus" Content="My back button" />

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM