question

hxy-4479 avatar image
0 Votes"
hxy-4479 asked ·

Urgent question: How to let scrollviewer scroll by itself

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

windows-uwp
· 1
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

The question is not clear, what do you actually want? May be you need this method?


0 Votes 0 ·

1 Answer

RichardZhang-MSFT avatar image
1 Vote"
RichardZhang-MSFT answered ·

Hello,​

Welcome to our Microsoft Q&A platform!

As @barrywang0526 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 · Share
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Thanks. I will try.

0 Votes 0 ·