【问题标题】:How to Navigate between xaml controls of UWP using mouse wheel?如何使用鼠标滚轮在 UWP 的 xaml 控件之间导航?
【发布时间】:2018-10-26 11:54:44
【问题描述】:

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

更多反馈:

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

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

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

xml代码:

<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 和树莓派中)。

【问题讨论】:

    标签: c# xaml uwp mouseevent mousewheel


    【解决方案1】:

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

    您是如何在代码中注册“鼠标滚轮”事件的?对我来说效果很好。

    请看以下代码示例:

    <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);
        }
    
    }
    

    【讨论】:

      【解决方案2】:

      只是给你一个想法。 首先,您应该处理表单上所有这些项目的 tabIndex 属性并设置它们的顺序。与您一起触发的内容也会随着您的情况下的选项卡一起移动,鼠标滚轮将是“Focused”或“GotFocus”方法。所以需要像“GotFocus”这样的事件。您还需要处理鼠标滚轮的移动(向上或向下)。您可以在 Google 上搜索如何根据需要覆盖 Tab 键到鼠标滚轮的 TabIndex 属性。

      【讨论】:

      • 我与 UWP 不太相似。通常与 windows 窗体一起运行,但您可以尝试在 xaml 中添加的项目的 TabIndex 属性。此选项应默认存在。
      • 在 Windows 表单和 android 端有一个内置的焦点 Enter 和 Leave,我已经检查过即使在那里我们也有 Focused 和 Unfocused 事件。所以这就是你的观点并使用断点
      • 谢谢!我没有在网上找到任何关于使用鼠标滚轮从 tab 键覆盖 TabIndex 属性的信息!!!
      • 我的错。 TabIndex 是按 Tab 时控件顺序的枚举。您可以搜索表单是否有鼠标滚轮事件或如何处理鼠标滚轮事件。如果用户转动滚轮,则使用 SendKey() 方法发送 Tab 键消息以形成。所以我猜你可以达到你的目的
      猜你喜欢
      • 1970-01-01
      • 2017-05-07
      • 1970-01-01
      • 1970-01-01
      • 2021-03-18
      • 2010-10-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多