如何使用鼠标滚轮在 UWP 的 xaml 控件之间导航?



我正在开发一个UWP应用程序,其中有几个XAML控件(按钮,文本块,复选框等(。理想情况下,此应用的最终用户应该能够仅使用鼠标滚轮在这些控件之间导航(这是一个医疗设备 UI,其中只有鼠标滚轮在显示器顶部可用(。现在我的问题是,如何强制此应用程序使用鼠标滚轮作为控件之间导航的主要来源?

更多反馈:

1.现在,当我在Visual Studio中运行我的应用程序时,我只看到鼠标指针,当然按钮对鼠标单击很敏感,但是为了启动事件,我必须将鼠标悬停在该元素上并单击。默认情况下,鼠标滚轮不工作以导航并选择控件。

2.当我在树莓派设备上旁加载此 UWP 应用程序并在那里运行应用程序时,在控件之间导航的唯一方法是使用连接的键盘(可以使用它导航和选择控件(。再次连接的鼠标滚轮在这里不起作用。

  1. 我在代码中使用的控件示例如下:

XAML 代码:

<Button x:Name="button1" Grid.Column="0" Grid.Row="0" Content="Button1" Click="button1_click" />
<Button x:Name="button2" Grid.Column="1" Grid.Row="0" Content="Button2" Click="button2_click" />
<Button x:Name="button3" Grid.Column="2" Grid.Row="0" Content="Button3" Click="button3_click" />

C# 代码:

private void button1_click(object sender, Windows.UI.Xaml.RoutedEventArgs e)
{
//do sth
}
private void button2_click(object sender, Windows.UI.Xaml.RoutedEventArgs e)
{
//do sth
}
private void button3_click(object sender, Windows.UI.Xaml.RoutedEventArgs e)
{
//do sth
}

在上面的代码中,无法使用鼠标滚轮(在Visual Studio和Raspberry Pi中(在三个按钮之间导航。

再次连接的鼠标滚轮在这里不起作用。

您是如何在代码中注册"鼠标滚轮"事件的?它对我这边效果很好。

请参阅以下代码示例:

<StackPanel x:Name="root" >
<Button x:Name="button1"  Grid.Column="0" Grid.Row="0" Content="Button1" Click="button1_click" />
<Button x:Name="button2"  Grid.Column="1" Grid.Row="0" Content="Button2" Click="button2_click" />
<Button x:Name="button3"  Grid.Column="2" Grid.Row="0" Content="Button3" Click="button3_click" />
</StackPanel>
public MainPage()
{
this.InitializeComponent();
Window.Current.CoreWindow.PointerWheelChanged += CoreWindow_PointerWheelChanged;
}
private async void CoreWindow_PointerWheelChanged(Windows.UI.Core.CoreWindow sender, Windows.UI.Core.PointerEventArgs args)
{
Debug.WriteLine(args.CurrentPoint.Properties.MouseWheelDelta);
UIElement element;
if (args.CurrentPoint.Properties.MouseWheelDelta > 0)
{
element = FocusManager.FindNextFocusableElement(FocusNavigationDirection.Up);
if (element == null)
{
element = FocusManager.FindLastFocusableElement(root) as UIElement;
}
var result = await FocusManager.TryFocusAsync(element, FocusState.Keyboard);
Debug.WriteLine((element as Button).Content.ToString() + " focused: " + result.Succeeded);
}
else
{
element = FocusManager.FindNextFocusableElement(FocusNavigationDirection.Down);
if (element == null)
{
element = FocusManager.FindFirstFocusableElement(root) as UIElement;
}
var result = await FocusManager.TryFocusAsync(element, FocusState.Keyboard);
Debug.WriteLine((element as Button).Content.ToString() + " focused: " + result.Succeeded);
}
}

只是为了给你一个想法。 首先,您应该处理表单上所有这些项的tabIndex属性并设置它们的顺序。此外,在您的情况下,触发的内容将随着选项卡移动,鼠标滚轮将是"聚焦"或"GotFocus"方法。所以需要像"GotFocus"这样的活动。您还需要处理鼠标滚轮移动(向上或向下(。您可以根据需要在Google上搜索如何将TabIndex属性从选项卡键覆盖到鼠标滚轮。

最新更新