简体   繁体   English

UWP Template10 SystemNavigationManager的“后退”按钮添加GotFocus事件

[英]UWP Template10 SystemNavigationManager Back button adding GotFocus event

I am trying to to add an OnGotFocus event on Template 10 Back button as: 我试图在模板10的“后退”按钮上添加OnGotFocus事件,如下所示:

In PageViewModels.cs: 在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. 您在代码段中所做的是为BackRequested事件连接了处理程序。 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. 我认为您应该复习有关C#中事件处理的一些基础知识,以消除任何混淆。

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. 为了能够使用OnGotFocus事件,您将必须在XAML中创建自己的后退按钮并将处理程序添加到此按钮,因为只有这样,您才能完全控制该控件。 If you just use the system provided BackRequested event, the system is in control and apart from this event you can't customize anything. 如果仅使用系统提供的BackRequested事件,则系统处于控制之中, BackRequested事件外,您无法自定义任何内容。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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