如何使ScrollView在UWP app中支持鼠标滚动?

时间:2019-06-30 10:43:24

标签: c# xaml uwp

我在UWP应用中使用ScrollView。它支持触摸滚动,但不支持鼠标wheel scroll and drag scroll

经过研究,我发现通过定义PointerWheelChanged事件,它可能会起作用。

XAML代码:

 PointerWheelChanged="ScrollViewer_PointerWheelChanged"

C#代码:

private void ScrollViewer_PointerWheelChanged(object sender, PointerRoutedEventArgs e)
{
    var scv = (ScrollViewer)sender;
    scv.ScrollToHorizontalOffset(scv.HorizontalOffset - e.Delta);
    e.Handled = true;
}

但是我收到一些错误消息:

  

PointerRoutedEventArgs”不包含“ Delta”的定义       并且没有可访问的扩展方法“ Delta”接受       可以找到类型为'PointerRoutedEventArgs'的第一个参数

如何解决?谢谢!

我附上了所有XAML和C#代码供您参考,任何人都可以在VS中对其进行测试。

我可以看到水平滚动条并将其拖动以移动按钮。我可以使用触摸拖动移动的按钮。但是我不能使用鼠标滚轮或用鼠标拖动按钮。

<Page
x:Class="App1.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:App1"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">

<Grid>
    <Canvas>
        <ScrollViewer  
        Width="400" 
        Height="150" 
        PointerWheelChanged="ScrollViewer_PointerWheelChanged"
        HorizontalScrollMode="Enabled" 
        HorizontalScrollBarVisibility="Visible" 
        VerticalScrollBarVisibility="Hidden">
            <StackPanel Orientation="Horizontal">
                <Button Width="200">Data 1
                </Button>
                <Button Width="200">Data 2
                </Button>
                <Button Width="200">Data 3
                </Button>
                <Button Width="200">Data 4
                </Button>
                <Button Width="200">Data 5
                </Button>
                <Button Width="200">Data 6
                </Button>
                <Button Width="200">Data 7
                </Button>
                <Button Width="200">Data 8
                </Button>
                <Button Width="200">Data 9
                </Button>
                <Button Width="200">Data 10
                </Button>
            </StackPanel>
        </ScrollViewer>
        <Rectangle  
        Width="200" 
        Height="135" 
        Canvas.Left="200" 
        Fill="Transparent" 
        Stroke="Red" 
        StrokeThickness="2"/>
    </Canvas>
</Grid>

C#代码:

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;

// The Blank Page item template is documented at https://go.microsoft.com/fwlink/?LinkId=402352&clcid=0x409

namespace App1
{
    /// <summary>
    /// An empty page that can be used on its own or navigated to within a Frame.
    /// </summary>
    public sealed partial class MainPage : Page
    {
        public MainPage()
        {
            this.InitializeComponent();
        }

        private void ScrollViewer_PointerWheelChanged(object sender, PointerRoutedEventArgs e)
        {
            //ScrollViewer scv = (ScrollViewer)sender;
            //scv.ScrollToHorizontalOffset(scv.HorizontalOffset - e.Delta);
            //e.Handled = true;
            Debug.WriteLine("wheel changed................");
        }
    }
}

1 个答案:

答案 0 :(得分:0)

  

我在UWP应用中使用ScrollView。它支持触摸滚动,但不支持鼠标滚轮和拖动滚动。

UWP ScrollViewer允许您使用鼠标滚轮滚动内容。我在上面测试了您的代码,效果很好。使用鼠标滚轮时,请将光标悬停在ScrollViewer上(矩形上方将覆盖ScrollViewer的一部分)。如果只想水平滚动,则需要禁用垂直滚动。而且drag scroll行为仅在触摸设备上有效。

<ScrollViewer
    Width="400"
    Height="150"
    HorizontalScrollBarVisibility="Visible"
    HorizontalScrollMode="Enabled"
    VerticalScrollBarVisibility="Hidden"
    VerticalScrollMode="Disabled"
    >