locked
ScrollViewer not updating quickly enough

    Question

  • I have several extension methods for the ScrollViewer to simplify calculating VerticalOffset and HorizontalOffset. These have worked perfectly in the past, but in my current Windows 8.1 app my ScrollViewers don't seem to be getting updated fast enough. What happens is first it gets one value, then if I call my extension method again, it gets a closer, but not quite correct, value. Once I call my extension method several times, I get the correct value. I think this is due to certain values not being updated when I call my extension method, causing it to calculate an incorrect value. Before calling the ChangeView() method, I call the UpdateLayout() and InvalidateScrollInfo() methods of the ScrollViewer. How can I make sure everything is updated before trying to set the VerticalOffset and HorizontalOffset properties? Thanks.

    Nathan Sokalski njsokalski@hotmail.com http://www.nathansokalski.com/

    Tuesday, August 5, 2014 8:20 PM

All replies

  • Hi Nathan,

    We have API that can get the correct offset by ScrollViewerViewChangingEventArgs class,(ViewChanging event) from which, a ScrollViewerView class contains offset is returned.

    --James


    <THE CONTENT IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, WHETHER EXPRESS OR IMPLIED>
    Thanks
    MSDN Community Support

    Please remember to "Mark as Answer" the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.


    Wednesday, August 6, 2014 2:16 AM
    Moderator
  • That sounds like it would work, so I would like to try to use it, but I'm not exactly sure what should be put where. I have attached my extension methods module so that you can see what that looks like. I am not sure if the ViewChanging event should go inside my extension methods or if I should somehow put it in my MainPage.xaml.vb. Can you help me here, or are there any good examples for the ViewChanging event? Thanks.

    Nathan Sokalski njsokalski@hotmail.com http://www.nathansokalski.com/

    Wednesday, August 6, 2014 7:59 PM
  • Well I have some code that can show you how it works.

            <ScrollViewer ViewChanging="ScrollViewer_ViewChanging" Margin="334,284,814,326"
                          HorizontalScrollBarVisibility="Visible" HorizontalScrollMode="Enabled"
                          VerticalScrollBarVisibility="Visible" VerticalScrollMode="Enabled">
                //PUT YOUR CONTENT HERE
            </ScrollViewer>
            <TextBlock x:Name="text" HorizontalAlignment="Left" Height="365" Margin="794,77,0,0" TextWrapping="Wrap" Text="TextBlock" VerticalAlignment="Top" Width="482"/>
    

    backend:

            private void ScrollViewer_ViewChanging(object sender, ScrollViewerViewChangingEventArgs e)
            {
                text.Text = "final x=" + e.FinalView.HorizontalOffset +
                    ",final y=" + e.FinalView.VerticalOffset +
                    ",next x=" + e.NextView.HorizontalOffset +
                    ",next y=" + e.NextView.VerticalOffset +
                    ",finish=" + e.IsInertial;
    
            }

    While scrolling the ScrollViewer, you should be able to see that the TextBlock update the current Scorlling offset.

    --James


    <THE CONTENT IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, WHETHER EXPRESS OR IMPLIED>
    Thanks
    MSDN Community Support

    Please remember to "Mark as Answer" the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.

    Thursday, August 7, 2014 1:43 AM
    Moderator
  • That's great, but all it does is display the values. I need to continue/finish scrolling rather than stop in the middle before the values can be calculated. My extension method basically just calculates an offset and then calls the ChangeView method. Here is what my extension method basically looks like:
    <Extension()> Public Sub ScrollToRequiredVertical(sv As ScrollViewer, required As Rect, dvo As DesiredVerticalOffset, always As Boolean)
    	'Code to calculate the offset is called here
    	'Call ChangeView
    	sv.ChangeView(Nothing, mycalculatedoffset, Nothing)
    End Sub
    I tried using calling my method in both the ViewChanging and ViewChanged events, and the ViewChanged actually seemed to work better, but I'm still not sure if it was doing what I want. I think it might be calculating the offset every time by having me call my method in the event handler multiple times (which could be expensive), because I sometimes notice the ScrollViewer sort of "vibrate" for a couple seconds when I scroll. I'm basically trying to determine when it is safe to call ChangeView so that I can make the ScrollViewer make a smooth scroll from it's current offset to the new one.

    Nathan Sokalski njsokalski@hotmail.com http://www.nathansokalski.com/

    Thursday, August 7, 2014 3:21 AM