SHIFT +滚动到ScrollViewer UWP中水平滚动

问题描述 投票:1回答:1

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

我从here那里了解到PointerWheelChanged事件是我正在寻找的事件。然而,处理它不起作用,因为ScrollViewer在内部处理它,所以我的处理程序永远不会被调用。为了解决这个问题,我使用了AddHandler方法,如"Routed Events Overview"文章中所述。

这有效...但似乎在ScrollViewerruns其内部代码后运行我的代码。结果是ScrollViewer内容垂直平移,然后水平平移。它们似乎按顺序发生,设置e.Handled = true并没有阻止它。

有没有办法“拦截”滚动,所以我可以用我自己的逻辑处理它,从而允许ScrollViewer如果按下SHIFT水平平移?我最近问了一个类似的问题(涉及拦截对控件的输入,所以我可以用我自己的逻辑处理它)here,其中答案涉及处理不同的事件,其中该事件发生在控制运行其自己的逻辑之前。对于指针滚动,我没有看到类似的“发生事件”事件。

我的代码如下。请注意,ScrollViewer可以水平和垂直滚动BOTH,以及缩放:

<!-- 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;
    }
}
c# xaml uwp event-handling scrollviewer
1个回答
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;
        }
    }

}
© www.soinside.com 2019 - 2024. All rights reserved.