如何使用 Xbox 控制器在 ListView 中的多个 GridView 之间导航项目?

How to navigate items between several GridViews in a ListView using Xbox controller?

我是 xbox 上的 运行 uwp,但在使用方向键或左摇杆处理导航时遇到一些问题。

我的情况是这样的: 一个ListView,里面包含很多GridViews。每个 GridView 都是使用 xaml 代码定义的一行。就像下图一样。

当按下键Down/Up时,最近的项目将被聚焦。例如,如果当前焦点在 Item1 of GridView1 上,则焦点在 Atem1 in GridView2 上。

如果GridView2已滚动到Atem11、Atem12、Atem13、Atem14。 Focus 将从 Item1 of GridView1 转移到 Atem11 in GridView2

那么,如何处理呢?

在 Xbox One 上,焦点通过使用 XY 导航系统移动,在 Xbox One 上,UWP 应用程序默认启用鼠标模式 运行。要禁用鼠标模式并启用 XY 焦点导航,请设置 Application.RequiresPointerMode=WhenRequested.

请检查XY focus navigation and interaction

XY 导航可能无法按您预期的方式工作的三个常见原因:

  1. The IsTabStop or Visibility property is set wrong.

  2. The control getting focus is actually bigger than you think—XY navigation looks at the total size of the control (ActualWidth and ActualHeight), not just the portion of the control that renders something interesting.

  3. One focusable control is on top of another—XY navigation doesn't support controls that are overlapped.

If XY navigation is still not working the way you expect after fixing these issues, you can manually point to the element that you want to get focus using the method described in Overriding the default navigation.

[2019/5/23更新]

I can't use XY focus, because all the GridViews(One row enabled) are generated dynamically. So XY focus can't handle from one item in GridView1 to another item in GridView2.

Suppose the focus is on item 1 in GridView1, press down button, the focus goes to item 1 in GridView2. By default, when pressing down button, the focus goes to item 2 in GridVew1.

根据您的具体要求,您可以尝试监视keydown事件并检查是否在事件处理程序中按下了游戏手柄向下键。请参阅 Nick Kramer [MSFT] 对此 case 的回复。

There's two things going on here. First, you'll never get a XAML KeyDown with e.Key == GamepadA/B/left/right/up/down -- they are all translated into their nearest keyboard equivalent. Instead, you can use the e.OriginalKey property to get the gamepad key before it's been translated into the keyboard equivalent.

之后,您需要检查当前获得焦点的控件是否为GridView1。您可以使用 FocusManager.GetFocusedElement method. If the current focused control is GridView1, then you could use FocusManager.TryFocusAsync 方法使 GridView2 聚焦并设置 SelectedIndexSelectedItem 来选择特定项目。