Urgent question: How to let scrollviewer scroll by itself

hxy 46 Reputation points
2019-12-31T05:09:48.393+00:00

Urgent question: How to let scrollviewer scroll by itself in UWP?

Universal Windows Platform (UWP)
{count} votes

Accepted answer
  1. Richard Zhang-MSFT 6,936 Reputation points
    2019-12-31T06:33:01.563+00:00

    Hello,​

    Welcome to our Microsoft Q&A platform!

    As @Barry Wang said, you can use ScrollViewer.ChangeView with DispatcherTimer to make ScrollViewer scroll automatically, this is a simple example:

    XAML

    <ScrollViewer x:Name="TestScrollViewer">

    <Rectangle Width="300" Height="3000" Fill="Gray"/>

    </ScrollViewer>

    XAML.cs

    private DispatcherTimer _scrollTimer;  
    private double EveryScrollHeight = 20;  
    private double TotalScrollHeight = 0;  
      
    public MainPpage()  
    {  
        this.InitializeComponent();  
        _scrollTimer = new DispatcherTimer();  
        _scrollTimer.Interval = TimeSpan.FromSeconds(0.1);  
        _scrollTimer.Tick += ScrollTimer_Tick;  
        _scrollTimer.Start();  
    }  
      
    private void ScrollTimer_Tick(object sender, object e)  
    {  
        double scrollHeight = TestScrollViewer.ScrollableHeight;  
        if (scrollHeight==0)  
            return;  
        TotalScrollHeight += EveryScrollHeight;  
        TestScrollViewer.ChangeView(0, TotalScrollHeight, 1);  
    }  
    

    EveryScrollHeight determines the height of each scroll. TotalScrollHeight records the total scroll height. ScrollViewer.ScrollableHeight is the remaining scrollable height of the ScrollViewer. In the ScrollTimer_Tick method, determine if the remaining scrollable height is 0, then stop trying to scroll.

    Thanks.

    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful