简体   繁体   中英

How to detect scrolling if view is not changed in ScrollView in C#?

I am developing an UWP app with C#, I wrap an Image in a ScrollView , in the Image I show a page of doc, because the page size is smaller than screen, so if I scroll the screen, view will not change , but I still need to detect the scrolling event, I try to use **ViewChanged** or **ViewChanging** but they are not called, I think maybe it's beacuase of the view is not changed. Anyone know a solution to detect?

Below is part of my xaml file:

<ScrollViewer ViewChanging="ScrollViewChanged">          
    <Image Name="PageImage" SizeChanged="PageImage_SizeChanged"/>
</ScrollViewer>

You may get mouse wheel event. Hope the link will help: https://docs.microsoft.com/en-us/uwp/api/Windows.UI.Xaml.UIElement#Windows_UI_Xaml_UIElement_PointerWheelChanged

For touch/pen you may subscribe to these events:

              PointerPressed="ScrollViewer_PointerPressed"
              PointerMoved="ScrollViewer_PointerMoved"
              PointerExited="ScrollViewer_PointerExited"
              PointerReleased="ScrollViewer_PointerReleased"

And handle them in code behind like this:

    private bool _isPressed;

    private void ScrollViewer_PointerPressed(object sender, PointerRoutedEventArgs e)
    {
        _isPressed = true;
    }

    private void ScrollViewer_PointerMoved(object sender, PointerRoutedEventArgs e)
    {
        if (_isPressed)
        {
            //your logic
        }
    }

    private void ScrollViewer_PointerExited(object sender, PointerRoutedEventArgs e)
    {
        _isPressed = false;
    }

    private void ScrollViewer_PointerReleased(object sender, PointerRoutedEventArgs e)
    {
        _isPressed = false;
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM