簡體   English   中英

如何使ScrollView在UWP app中支持鼠標滾動?

[英]How to make ScrollView supports mouse scroll in UWP app?

我在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................");
        }
    }
}

我在UWP應用中使用ScrollView。 它支持觸摸滾動,但不支持鼠標滾輪滾動和拖動滾動。

UWP ScrollViewer允許您使用鼠標滾輪滾動內容。 我在上面測試了您的代碼,效果很好。 使用鼠標滾輪時,請將光標懸停在ScrollViewer (Rectangle以上將覆蓋ScrollViewer一部分)。 如果只想水平滾動,則需要禁用垂直滾動。 drag scroll行為僅在觸摸設備上起作用。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM