Shift +滚动以在ScrollViewer UWP中水平滚动

时间:2018-12-14 02:27:09

标签: c# xaml uwp event-handling scrollviewer

我想在滚动水平滚动ScrollViewer的同时按住键盘上的SHIFT键。

我从here得知PointerWheelChanged事件是我正在寻找的事件。但是,处理该操作不起作用,因为ScrollViewer是在内部进行处理的,因此永远不会调用我的处理程序。为了解决这个问题,我使用了"Routed Events Overview"文章中所述的AddHandler方法。

这可以工作...但是在ScrollViewer运行其内部代码之后,似乎正在运行我的代码。结果是ScrollViewer内容垂直平移,然后水平平移。它们似乎按照该顺序发生,并且设置e.Handled = true并不会阻止它。

是否有一种“拦截”滚动的方法,因此我可以使用自己的逻辑来处理它,从而如果按下SHIFT键,则允许ScrollViewer水平平移?我最近问了一个类似的问题(涉及拦截控件的输入,以便我可以用自己的逻辑处理它)here,其中答案涉及处理另一个事件,该事件发生在控件运行自己的逻辑之前。我没有看到指针滚动出现类似的“正在发生的事”事件。

我遵循的代码。请注意,ScrollViewer既可以水平和垂直滚动,也可以缩放:

<!-- Contained in Grid in a UserControl, if that's relevant -->
<ScrollViewer Name="MyCanvasScrollViewer"
              VerticalScrollBarVisibility="Auto"
              HorizontalScrollBarVisibility="Auto"
              ZoomMode="Enabled"
              ZoomSnapPointsType="Optional"
              PointerWheelChanged="MyCanvasScrollViewer_PointerWheelChanged">
    <!-- Content to pan -->
</ScrollViewer>

隐藏代码:

// Constructor for the user contol.
public MyControl()
{
    // Add the scroll wheel event handler and force it to run.
    this.MyCanvasScrollViewer.AddHandler(ScrollViewer.PointerWheelChangedEvent, new PointerEventHandler(this.MyCanvasScrollViewer_PointerWheelChanged), true);

    // Other un-related stuff omitted here...
}

// Event handler for the Pointer Wheel Changed event.
private void MyCanvasScrollViewer_PointerWheelChanged(object sender, PointerRoutedEventArgs e)
{
    // If SHIFT is pressed...
    var keyState = CoreWindow.GetForCurrentThread().GetKeyState(VirtualKey.Shift);
    if ((keyState & CoreVirtualKeyStates.Down) == CoreVirtualKeyStates.Down)
    {
        // Get the amount to scroll.
        PointerPoint pointer = e.GetCurrentPoint(this.WallCanvasScrollViewer);
        double scrollWheelDelta = pointer.Properties.MouseWheelDelta;

        // Change the view in the scroll viewer.
        this.MyCanvasScrollViewer.ChangeView(scrollWheelDelta, null, null, true);

        // Mark event as handled.
        e.Handled = true;
    }
}

1 个答案:

答案 0 :(得分:1)

您只需在VerticalScrollMode keydown 中禁用shift,然后在 keyup 上启用它即可。无需pointerwheelchanged本身。效果很好。

Xaml

<ScrollViewer ZoomMode="Enabled" x:Name="MyScrollViewer"  HorizontalScrollMode="Enabled" HorizontalScrollBarVisibility="Visible" >
   <Image Height="600" Width="500" Source="/Assets/1.jpg"></Image>
</ScrollViewer>

// C#代码

public sealed partial class MainPage : Page
{
    public MainPage()
    {
        this.InitializeComponent();
        CoreWindow.GetForCurrentThread().KeyDown += MainPage_KeyDown;
        CoreWindow.GetForCurrentThread().KeyUp += MainPage_KeyUp; ;
    }

    private void MainPage_KeyUp(CoreWindow sender, KeyEventArgs args)
    {
        if (args.VirtualKey == VirtualKey.Shift)
        {                
            MyScrollViewer.IsScrollInertiaEnabled = true;
            MyScrollViewer.VerticalScrollMode = ScrollMode.Enabled;
        }
    }

    private void MainPage_KeyDown(CoreWindow sender, KeyEventArgs args)
    {
        if (args.VirtualKey == VirtualKey.Shift)
        {
            MyScrollViewer.IsScrollInertiaEnabled = false;
            MyScrollViewer.VerticalScrollMode = ScrollMode.Disabled;
        }
    }

}