简体   繁体   中英

ScrollViewer's ScrollToHorizontalOffset not working in Windows 8 app

I have a Windows Store app with a ScrollViewer and an Image in it. When i double tap on the ScrollViewer I want it to zoom the Image to its width. This part is not a problem but I also want the Image to be centered after it has beed zoomed in.

I tried calling the ScrollToHorizontalOffset method on the ScrollViewer but It does not seem to work with any number I give it. What is the problem?

Perhaps the offset only works for the non-zoomed view where your image fills the ScrollViewer completely and thus can't be scrolled. You could try setting the image dimensions so that it is larger than the ScrollViewer, but set original ZoomFactor, so that it fills the ScrollViewer at first. Then zooming and scrolling might work.

Assign a SizeChangedEvent in the scrollviewer.

<ScrollViewer SizeChanged="OnSizeChange"></ScrollViewer>

like this. Then it is better to place your image inside a canvas. So your code will be probably like this.

<ScrollViewer SizeChanged="OnSizeChange" x:Name="scrl">
   <Canvas RenderTransformOrigin="0.5,0.5" x:Name="main">
       <Image source="" Canvas.Top="insert desire double value here", Canvas.Left="Same goes here"/>
   </Canvas>
</ScrollViewer>

then in the code behind you can change the height and width of your canvass depending on the scroll viewer

Main.Width = scrl.ViewPortWidth; Main.Height = scrl.ViewPortHeight;

You can experiment on the size of the canvass while adding a double tap event to it. Changing the size of the canvas can zoom in or out the image because the image is inside the canvass

Try with 'ChangeView' instead of 'ZoomToFactor'

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