Previously I’ve written about how Prism for the Windows Runtime can be used to save and restore view state. This is achieved by overriding the SaveState and LoadState methods in a page that derives from the VisualStateAwarePage class, and then saving the desired state. I extended the sample photo viewer app so that the user will see the page content scrolled to the exact location it was at prior to termination or navigation, regardless of whether the page orientation has changed in between termination and reactivation. However, I didn’t document how the scroll position is obtained from a control, and re-applied when the window size changes.
In this blog post I’ll explain how the scroll position is obtained from a control, and re-applied when the window size changes. The approach is to capture the ScrollViewer’s offset value as a proportion of its total size. Then, once the user rotates the device or changes the size of the frame (thus effectively resizing the window), the offset proportion is restored to the control. This means that if a GridView control has enough content to pan, and you pan to 80% of the content, you can rotate the device, or switch to MinimalLayout, and the scroll bar should be vertical but still at 80% of the content. In addition, if you navigate forward and then back, the scroll bar should still be at 80% of the content.
This technique was devised as an intuitive user experience for the AdventureWorks Shopper reference implementation.
When the page loads the ScrollViewer embedded in the GridView is captured by the Loaded event handler for the page. The Loaded event fires when the control has been constructed and added to the object tree, meaning that it’s ready for interaction.
This method uses the GetVisualChild method in the VisualTreeUtilites class (not shown here). In this usage the method walks the visual tree to obtain the ScrollViewer instance located in the GridView instance.
The LayoutUpdated event handler is then used to capture the ScrollViewer’s scroll position. The LayoutUpdated event fires when the layout of the visual tree changes, due to layout-relevant properties changing value or some other action that refreshes the layout.
This method simply uses the GetScrollViewerOffsetProportion in the ScrollViewerUtilites class to calculate the offset proportion.
The offset proportion is calculated as the maximum of either the HorizontalOffset divided by ScrollableWidth or the VerticalOffset divided by the ScrollableHeight. This value can then be restored when the user rotates their device or resizes the frame, in order to scroll to the same location.
After the page size changes the ScrollViewer can be set with the offset proportion. However, we must first wait for the ScrollViewer to finish rendering its items. One way to determine this is to examine the ComputedHorizontalScrollBarVisibility and ComputedVerticalScrollBarVisibilty properties in the SizeChanged event handler. The SizeChanged event fires when either the ActualHeight or ActualWidth property value changes on the page.
Here, if the ComputedHorizontalScrollBarVisibility and ComputedVerticalScrollBarVisibility both equal Visibility.Visible then the ScrollViewer can be scrolled by calling the ScrollToProportion method of the ScrollViewerUtilities class. If the two properties are not set to Visibility.Visible, then the DependencyPropertyChangedHelper class is used to listen for changes in the two properties. The ScrollBarVisibilityChanged event handler is executed when either of the two properties change.
This event handler checks if either of the two properties have changed to Visibility.Visible. If they have, the ScrollViewer is updated by calling the ScrollToProportion method of the ScrollViewerUtilities class.
In the ScrollToProportion method the offset is calculated as the offset proportion multiplied by the ScrollableHeight, and the offset proportion multiplied by the ScrollableWidth (one for landscape, one for portrait). Then the ChangeView method is called on the ScrollViewer instance to load a new view into the viewport using the offsets.
If the user navigates to another page or the app is terminated, the offset proportion is lost. Therefore, the SaveState and LoadState methods are used to preserve this value. For more information about this see one of my previous blog posts on this topic.
In this blog post I’ve explained how the scroll position is obtained from a control, and re-applied when the window size changes. The approach is to capture the ScrollViewer’s offset value as a proportion of its total size. Then, once the user rotates the device or changes the size of the frame, the offset proportion is restored to the control
The sample app can be downloaded here.