C# 如何使鼠标滚轮成为UWP中用户交互的主要来源?

C# 如何使鼠标滚轮成为UWP中用户交互的主要来源?,c#,xaml,uwp,mouseevent,mousewheel,C#,Xaml,Uwp,Mouseevent,Mousewheel,我有一个UWP应用程序,有很多按钮、复选框等。在这个应用程序中,我想让鼠标滚轮成为用户交互的主要来源。换句话说,一旦应用程序运行,理想情况下,用户应该能够仅使用鼠标滚轮在xaml控件(按钮、复选框等)中导航。这怎么可能 注1:默认情况下,当应用程序运行时,会出现鼠标光标,并且可以使用鼠标在UI中导航。对此不感兴趣 注2:默认情况下,“键盘”选项卡无法导航 我看不出有什么方法不是“哈奇”,我会制作一个事件,捕捉鼠标左/右点击并立即返回 对于每个用户难以处理的控件,您可以使用命令管理器绑定一个命令,

我有一个UWP应用程序,有很多按钮、复选框等。在这个应用程序中,我想让鼠标滚轮成为用户交互的主要来源。换句话说,一旦应用程序运行,理想情况下,用户应该能够仅使用鼠标滚轮在xaml控件(按钮、复选框等)中导航。这怎么可能

注1:默认情况下,当应用程序运行时,会出现鼠标光标,并且可以使用鼠标在UI中导航。对此不感兴趣


注2:默认情况下,“键盘”选项卡无法导航

我看不出有什么方法不是“哈奇”,我会制作一个事件,捕捉鼠标左/右点击并立即返回

对于每个用户难以处理的控件,您可以使用命令管理器绑定一个命令,以仅接受鼠标中键输入,或在用户单击鼠标中键时创建一个事件

如果你不懂一些东西,我很乐意举一些例子

让我们查看主窗口并在窗口层次结构的顶层添加事件

<Window x:Class="test.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:test"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800"

        <!-- This event captures every mouse down action that is clicked 
             Inside the window -->
        PreviewMouseDown="Window_PreviewMouseDown">

    <Grid>

    </Grid>
</Window>

PreviewMouseDown=“窗口\u PreviewMouseDown”
主窗口的代码隐藏

        /// <summary>
        /// This event will also capture any event, But this time you can check if the mouse button clicked is the middle mouse.           /// For now we will just return out of the method
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Window_PreviewMouseDown(object sender, MouseButtonEventArgs e)
        {
             // If the pressed mouse button is either the left or right button
            if(e.ChangedButton == MouseButton.Left || e.ChangedButton == MouseButton.Right)
            {
                // Exit out of the method
                return;
            };
        }

/// <summary>
        /// This event will capture every mouse down event, You can add any logic to it.
        /// For now we will just return out of the method
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
    private void Button_MouseWheel(object sender, MouseButtonEventArgs e)
        {
            // If the pressed mouse button is the middle
            if(e.ChangedButton == MouseButton.Middle)
            {
                // do stuff...
            };
        }
//
///此事件还将捕获任何事件,但这次您可以检查单击的鼠标按钮是否为鼠标中键。//现在,我们将从方法中返回
/// 
/// 
/// 
私有无效窗口\u预览鼠标向下(对象发送器,鼠标按钮Ventargs e)
{
//如果按下的鼠标按钮是左键或右键
if(e.ChangedButton==鼠标按钮.左| | e.ChangedButton==鼠标按钮.右)
{
//退出方法
回来
};
}
/// 
///此事件将捕获每个鼠标按下事件,您可以向其添加任何逻辑。
///现在,我们将从方法中返回
/// 
/// 
/// 
专用无效按钮\鼠标滚轮(对象发送器,鼠标按钮Ventargs e)
{
//如果按下的鼠标按钮位于中间
if(e.ChangedButton==MouseButton.Middle)
{
//做些事情。。。
};
}
不同控制

<!-- This is what you normally do -->
    <Button Click="Button_Click"/>
    <!-- Or -->
    <Button Command="{Binding MyComamnd}"/>

    <!-- This you will need to do for every interactable control -->
    <Button MouseDown="Button_MouseWheel"/>
    <!-- Or -->
    <Button>
        <!-- Bind to an MVVM input command -->
        <Button.InputBindings>
            <MouseBinding Command="{Binding MyCommand}"
                          MouseAction="MiddleClick"/>
        </Button.InputBindings>
    </Button>

制作滚动查看器


实际上,用户应该只能使用鼠标滚轮在xaml控件(按钮、复选框等)中导航。这怎么可能

当然,您可以使用事件来监视当前的
CoreWindow
鼠标滚轮

CoreWindow.GetForCurrentThread().PointerWheelChanged += MainPage_PointerWheelChanged;
然后,您可以从
PointerPointProperties
对象中获取
mouseweelldelta
属性值。剩下的就是繁琐的计算过程。我用下面的代码实现了这一点

public MainPage()
{
    this.InitializeComponent();
    CoreWindow.GetForCurrentThread().PointerWheelChanged += MainPage_PointerWheelChanged;
    this.Loaded += MainPage_Loaded;
}

private void MainPage_Loaded(object sender, RoutedEventArgs e)
{   // RootLayout is Grid name.
    childrenCount = RootLayout.Children.Count;
}

private int childrenCount; // sub items count 
private int index; // index of focus control
private bool IsFirt = true; // first use flag
private void MainPage_PointerWheelChanged(CoreWindow sender, PointerEventArgs args)
{
    //get mouse wheel delta
    var value = args.CurrentPoint.Properties.MouseWheelDelta;

    if (IsFirt)
    {
        switch (value)
        {
            case 120:

                index = childrenCount;
                if (index == 0)
                {
                    index = childrenCount - 1;
                }
                else
                {
                    index--;
                }
                break;

            case -120:

                index = -1;

                if (index == childrenCount - 1)
                {
                    index = 0;
                }
                else
                {
                    index++;
                }
                break;

        }

        IsFirt = false;
    }
    else
    {
        switch (value)
        {
            case 120:

                if (index == 0)
                {
                    index = childrenCount - 1;
                }
                else
                {
                    index--;
                }

                break;

            case -120:

                if (index == childrenCount - 1)
                {
                    index = 0;
                }
                else
                {
                    index++;
                }

                break;
        }

    }
    // focus control with index
    var element = RootLayout.Children[index] as Control;
    element.Focus(FocusState.Keyboard);

}
注2:默认情况下,“键盘”选项卡无法导航

您可以在
Window.Current.Content
订阅的事件记录中禁用选项卡导航。然后确定按下的制表键设置
e.Handled=true

Window.Current.Content.PreviewKeyDown += Content_PreviewKeyDown;

private void Content_PreviewKeyDown(object sender, KeyRoutedEventArgs e)
{        
    e.Handled = e.Key == VirtualKey.Tab ? true : false;
}
上述代码将忽略当前内容中按下的选项卡


这是您可以参考的。

谢谢您的回复!我对这方面很陌生,如果你能和我分享一些例子,我将不胜感激。我找不到任何显示使用鼠标滚轮数据导航按钮的在线示例。假设我们在UI中有两个或三个按钮,我们想使用鼠标滚轮在它们之间导航,一旦鼠标滚轮位于每个元素上,点击按钮就会选择它。这是我想到的最简单的例子。谢谢谢谢你的代码。事实上,我仍然想知道如何使用滚轮启用滚动。您发送的代码是捕获鼠标中键单击。我想使用鼠标滚轮在按钮之间导航。类似这样的场景:1.app运行时,鼠标滚轮位于按钮1的上方2.user点击鼠标滚轮3.control转到按钮2,3,4。。。每次点击鼠标滚轮我都不明白你想说什么,你想让用户用鼠标滚轮滚动并在按钮之间移动吗?你说的是scrollviewer?是的。我想用鼠标WHILI移动按钮之间,如果这个或任何答案已经解决了你的问题,请考虑通过点击复选标记接受它。这向更广泛的社区表明,你已经找到了一个解决方案,并给回答者和你自己带来了一些声誉
Window.Current.Content.PreviewKeyDown += Content_PreviewKeyDown;

private void Content_PreviewKeyDown(object sender, KeyRoutedEventArgs e)
{        
    e.Handled = e.Key == VirtualKey.Tab ? true : false;
}